Showing posts with label jndi. Show all posts
Showing posts with label jndi. Show all posts

Saturday, November 4, 2006

JDBC via JNDI

// define sql query
String sql = "select firstname from users";

// initialize database connection objects
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

// get JNDI JDBC connection
InitialContext ctxt = new InitialContext();
DataSource ds = (DataSource) ctxt.lookup("jdbc/TrackIt");
conn = ds.getConnection();

// run sql objects
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

// retrieve results
while (rs.next()) {
rs.getInt("firstname"));
}