当前位置: 技术问答>java相关
关于连接池的问题:我不知道数据库连接池在那个包里面,我在JDK1.4的API手册中找不到?
来源: 互联网 发布时间:2015-07-17
本文导语: 据我所知关于连接池的类有两个,一个是ConnectionPool类,另一个是PoolManager类,可是我在JDK1.4的API手册中找不到这两个类,请问: 1、这两个类是不是JDK标准? 2、如果不是,在那个包里面,是否需要单独下载和安装? ...
据我所知关于连接池的类有两个,一个是ConnectionPool类,另一个是PoolManager类,可是我在JDK1.4的API手册中找不到这两个类,请问:
1、这两个类是不是JDK标准?
2、如果不是,在那个包里面,是否需要单独下载和安装?
3、我应该import什么,才能使用它?
1、这两个类是不是JDK标准?
2、如果不是,在那个包里面,是否需要单独下载和安装?
3、我应该import什么,才能使用它?
|
拜托,这两个类是自己写的!
J2EE里面有的DATASOURCE类,是现成的连接池!是在javax.sql包下!
J2EE里面有的DATASOURCE类,是现成的连接池!是在javax.sql包下!
|
websphere下的一段JDBC2.0的连接
try {
//Retrieve a DataSource through the JNDI Naming Service
java.util.Properties parms = new java.util.Properties();
parms.setProperty(Context.INITIAL_CONTEXT_FACTORY,com.ibm.websphere.naming.WsnInitialContextFactory”);
//Create the Initial Naming Context
javax.naming.Context ctx = new
javax.naming.InitialContext(parms);
//Lookup through the naming service to retrieve a
//DataSource object
//In this example, SampleDB is the datasource
javax.sql.DataSource ds =
(javax.sql.DataSource)
ctx.lookup("java:comp/env/jdbc/SampleDB");
//Obtain a Connection from the DataSource
java.sql.Connection conn =
ds.getConnection();
//query the database
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet rs =
stmt.executeQuery(“SELECT EMPNO, FIRSTNME, LASTNAME
FROM EMPLOYEE”);
//process the results
while (rs.next()) {
String empno = rs.getString(“EMPNO”);
String firstnme = rs.getString(“FIRSTNME”);
String lastname = rs.getString(“LASTNAME”);
// work with results
}
} catch (java.sql.SQLException sqle) {
//handle SQLException
} finally {
try {
if (rs != null) rs.close();
} catch (java.sql.SQLException sqle) {
//can ignore
}
try {
if (stmt != null) stmt.close();
} catch (java.sql.SQLException sqle) {
//can ignore
}
try {
if (conn != null) conn.close();
} catch (java.sql.SQLException sqle) {
//can ignore
}
}
try {
//Retrieve a DataSource through the JNDI Naming Service
java.util.Properties parms = new java.util.Properties();
parms.setProperty(Context.INITIAL_CONTEXT_FACTORY,com.ibm.websphere.naming.WsnInitialContextFactory”);
//Create the Initial Naming Context
javax.naming.Context ctx = new
javax.naming.InitialContext(parms);
//Lookup through the naming service to retrieve a
//DataSource object
//In this example, SampleDB is the datasource
javax.sql.DataSource ds =
(javax.sql.DataSource)
ctx.lookup("java:comp/env/jdbc/SampleDB");
//Obtain a Connection from the DataSource
java.sql.Connection conn =
ds.getConnection();
//query the database
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet rs =
stmt.executeQuery(“SELECT EMPNO, FIRSTNME, LASTNAME
FROM EMPLOYEE”);
//process the results
while (rs.next()) {
String empno = rs.getString(“EMPNO”);
String firstnme = rs.getString(“FIRSTNME”);
String lastname = rs.getString(“LASTNAME”);
// work with results
}
} catch (java.sql.SQLException sqle) {
//handle SQLException
} finally {
try {
if (rs != null) rs.close();
} catch (java.sql.SQLException sqle) {
//can ignore
}
try {
if (stmt != null) stmt.close();
} catch (java.sql.SQLException sqle) {
//can ignore
}
try {
if (conn != null) conn.close();
} catch (java.sql.SQLException sqle) {
//can ignore
}
}