ojdbc6.jar
tomcat-juli.jar
[Sample Code]
package com.tv.util.db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;
public class JndiConnectionTest {
@Before
public void init() {
try {
// Create initial context
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext ic = new InitialContext();
ic.createSubcontext("java:");
ic.createSubcontext("java:/comp");
ic.createSubcontext("java:/comp/env");
ic.createSubcontext("java:/comp/env/jdbc");
// Construct DataSource
OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
ds.setURL("jdbc:oracle:thin:@ip:port:sid");
ds.setUser("test");
ds.setPassword("test");
ic.bind("java:/comp/env/jdbc/sid", ds);
} catch (NamingException ex) {
ex.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void simpleConnectionTest() {
Connection con = null;
try {
Context initContext = new InitialContext();
Context webContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) webContext.lookup("jdbc/sid");
con = ds.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from userprof where userid = '2000131'");
int cnt = 1;
while (rs.next()) {
System.out.println((cnt++) + ". BAN:" + rs.getString("BAN") + " User:" + rs.getString("USERID") + " BRIEFNAME:" + rs.getString("BRIEFNAME"));
}
rs.close();
st.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
}
}