JDBC 连接池(类)

JDBC 连接池(类)

//这个类是通过JNDI来的到与数据库的连接
package dao_sql;

import java.sql.Connection;

import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * @author Administrator
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class Dao_Sql
{
 public Dao_Sql()
 {
  
 }
 public static Connection getConnection() throws ClassNotFoundException, SQLException
 {
  return getConnection("newap");
 }

// 通过JNDI得到连接
 public static Connection getConnection(String dbname)
 {
  try
  {
   Context ctx = new InitialContext();

   DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/"+dbname);
   
   return ds.getConnection();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
  return null;
 }


// public static Connection getConnection(String dbname) throws ClassNotFoundException, SQLException
// {
//  String classname = "com.jnetdirect.jsql.JSQLDriver" ;
//     Class.forName(classname) ;
//     String url = "jdbc:JSQLConnect://localhost/bbs" ;
//     String name = "sa" ;
//     String password = "sa" ;
//     return DriverManager.getConnection(url,name,password) ;
// }
}

你可能感兴趣的:(JDBC 连接池(类))