/**
* 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 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 sample to check how many physical connections
* a pool connection will open.
* First one should thrown an exception
*/import java.sql.*;
import javax.sql.*;
import oracle.jdbc.driver.*;
import java.util.Properties;
import oracle.jdbc.pool.*;class PooledConnection2
{
public static void main (String args [])
throws SQLException, java.io.IOException
{ OracleConnectionPoolDataSource ocpds =
new OracleConnectionPoolDataSource(); String url = "jdbc:oracle:oci8:@";
ocpds.setURL(url);
ocpds.setUser("scott");
ocpds.setPassword("tiger");
// Open sysconn and sysstmt
open_sys_conn(); System.out.println("No of Sessions before opening a PooledConnection is " +
count_sessions()); PooledConnection pc = ocpds.getPooledConnection(); System.out.println("No of Sessions before opening first LogicalConnection is " +
count_sessions()); Connection conn1 = pc.getConnection(); System.out.println("No of Sessions before opening Second PooledConnection is " +
count_sessions()); Connection conn2 = pc.getConnection(); System.out.println("No of Sessions after opening Second PooledConnection is " +
count_sessions()); // Create a Statement
Statement stmt = conn2.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(); // Close the Statement
stmt.close(); try {
// This should throw an exception as conn1 is invalid
// as control is stolen by conn2 stmt = conn1.createStatement ();
} catch (SQLException se)
{
System.out.println("Exception expected : " + se.getMessage());
} // Close the connection
conn1.close(); // Close the connection
conn2.close(); // CLose the Pooled Connection
pc.close(); // Close sysconn and sysstmt
close_sys_conn();
}
static Connection sysconn = null;
static Statement sysstmt = null; private static void open_sys_conn ()
throws SQLException
{ Properties prop = new Properties();
prop.put ("internal_logon","test");
sysconn = DriverManager.getConnection("jdbc:oracle:oci8:@", "system",
"manager"); sysstmt = sysconn.createStatement();
} private static int count_sessions ()
throws SQLException
{
ResultSet sysrs = sysstmt.executeQuery("select count(*) from V$SESSION");
sysrs.next();
int cnt = sysrs.getInt(1); sysrs.close();
sysrs = null; return cnt;
} private static void close_sys_conn ()
throws SQLException
{ sysstmt.close();
sysstmt = null;
sysconn.close();
sysconn = null;
}
}