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"));
}

2 comments:

mpegjohn said...

Good, basic setup, Just what I needed.

Anonymous said...

Article writing is also a fun, if you know after that you can write
if not it is complicated to write.

Also visit my webpage legalsoundz.com