class PooledConnection1
{
public static void main (String args [])
throws SQLException
{
// Create a OracleConnectionPoolDataSource instance
OracleConnectionPoolDataSource ocpds =
new OracleConnectionPoolDataSource();
String url = "jdbc:oracle:oci8:@";
try {
String url1 = System.getProperty("JDBC_URL");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}
// Set connection parameters
ocpds.setURL(url);
ocpds.setUser("hr");
ocpds.setPassword("hr");
// Create a pooled connection
PooledConnection pc = ocpds.getPooledConnection();
// Get a Logical connection
Connection conn = pc.getConnection();
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the NAME columns from the EMPLOYEES table
ResultSet rset = stmt.executeQuery
("select FIRST_NAME, LAST_NAME from EMPLOYEES");
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1) + " " + rset.getString (2));
// Close the RseultSet
rset.close();
rset = null;
// Close the Statement
stmt.close();
stmt = null;
// Close the logical connection
conn.close();
conn = null;
// Close the pooled connection
pc.close();
pc = null;
}
}
to 楼上:
谢谢你的回复,还想问一下,InstanceKeyDataSource也是DataSource的实现,像你说的,它们是标准的连接池,那么标准的Datasource的实现与ConnectionPoolDataSource有什么联系和区别,我们在项目中究竟该采用实现哪个接口的连接池,并且我现在还没发现什么驱动或者组件实现过后者,它们二者应该在jdbc驱动中实现还是应该靠dbcp这样第三方的来实现呢,不明白。