JNDI不用你的ConnectionPool通常用DataSource类,看看java.sql.DataSource.
/**
* A Simple DataSource sample without using JNDI.
*/// You need to import the java.sql package to use JDBC
import java.sql.*;
import javax.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.OracleDataSource;public class DataSource
{
public static void main (String args [])
throws SQLException
{
// Create a OracleDataSource instance explicitly
OracleDataSource ods = new OracleDataSource(); // Set the user name, password, driver type and network protocol
ods.setUser("scott");
ods.setPassword("tiger");
ods.setDriverType("oci8");
ods.setNetworkProtocol("ipc"); // Retrieve a connection
Connection conn = ods.getConnection();
getUserName(conn);
// Close the connection
conn.close();
conn = null;
} static void getUserName(Connection conn)
throws SQLException
{
// Create a Statement
Statement stmt = conn.createStatement (); // Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select USER from dual"); // Iterate through the result and print the employee names
while (rset.next ())
System.out.println ("User name is " + rset.getString (1)); // Close the RseultSet
rset.close();
rset = null; // Close the Statement
stmt.close();
stmt = null;
}
}
/**
* A Simple DataSource sample without using JNDI.
*/// You need to import the java.sql package to use JDBC
import java.sql.*;
import javax.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.OracleDataSource;public class DataSource
{
public static void main (String args [])
throws SQLException
{
// Create a OracleDataSource instance explicitly
OracleDataSource ods = new OracleDataSource(); // Set the user name, password, driver type and network protocol
ods.setUser("scott");
ods.setPassword("tiger");
ods.setDriverType("oci8");
ods.setNetworkProtocol("ipc"); // Retrieve a connection
Connection conn = ods.getConnection();
getUserName(conn);
// Close the connection
conn.close();
conn = null;
} static void getUserName(Connection conn)
throws SQLException
{
// Create a Statement
Statement stmt = conn.createStatement (); // Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select USER from dual"); // Iterate through the result and print the employee names
while (rset.next ())
System.out.println ("User name is " + rset.getString (1)); // Close the RseultSet
rset.close();
rset = null; // Close the Statement
stmt.close();
stmt = null;
}
}
* A Simple DataSource sample with JNDI.
* This is tested using File System based reference
* implementation of JNDI SPI driver from JavaSoft.
* You need to download fscontext1_2beta2.zip from
* JavaSoft site.
* Include providerutil.jar & fscontext.jar extracted
* from the above ZIP in the classpath.
* Create a directory /tmp/JNDI/jdbc
*/// You need to import the java.sql package to use JDBC
import java.sql.*;
import javax.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.OracleDataSource;
import javax.naming.*;
import javax.naming.spi.*;
import java.util.Hashtable;public class DataSourceJNDI
{
public static void main (String args [])
throws SQLException, NamingException
{
// Initialize the Context
Context ctx = null;
try {
Hashtable env = new Hashtable (5);
env.put (Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put (Context.PROVIDER_URL, "file:/tmp/JNDI");
ctx = new InitialContext(env);
} catch (NamingException ne)
{
ne.printStackTrace();
} do_bind(ctx, "jdbc/sampledb");
do_lookup(ctx, "jdbc/sampledb"); } static void do_bind (Context ctx, String ln)
throws SQLException, NamingException
{
// Create a OracleDataSource instance explicitly
OracleDataSource ods = new OracleDataSource(); // Set the user name, password, driver type and network protocol
ods.setUser("scott");
ods.setPassword("tiger");
ods.setDriverType("oci8");
ods.setNetworkProtocol("ipc"); // Bind it
System.out.println ("Doing a bind with the logical name : " + ln);
ctx.bind (ln,ods);
} static void do_lookup (Context ctx, String ln)
throws SQLException, NamingException
{ System.out.println ("Doing a lookup with the logical name : " + ln);
OracleDataSource ods = (OracleDataSource) ctx.lookup (ln); // Retrieve a connection
Connection conn = ods.getConnection();
getUserName(conn);
// Close the connection
conn.close();
conn = null;
} static void getUserName(Connection conn)
throws SQLException
{
// Create a Statement
Statement stmt = conn.createStatement (); // Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select USER from dual"); // Iterate through the result and print the employee names
while (rset.next ())
System.out.println ("User name is " + rset.getString (1)); // Close the RseultSet
rset.close();
rset = null; // Close the Statement
stmt.close();
stmt = null;
}
}
* A simple Pooled Connection Sample
*/import java.sql.*;
import javax.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.*;class PooledConnection1
{
public static void main (String args [])
throws SQLException
{ // Create a OracleConnectionPoolDataSource instance
OracleConnectionPoolDataSource ocpds =
new OracleConnectionPoolDataSource(); // Set connection parameters
ocpds.setURL("jdbc:oracle:oci8:@");
ocpds.setUser("scott");
ocpds.setPassword("tiger"); // 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 ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP"); // Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1)); // 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;
}
}