Re: Datasource with JBuilder
On Fri, 26 Oct 2007 16:52:26 GMT, mike@nospam.com (Mike) wrote:
Here's what I have. When a jsp calls "getStatement()" a valid
statement is returned and I can do a query. When I run the class from
JBuiler it can't find the datasource.
===========================================
package com_myPackage;
import java.sql.*;
import javax.sql.*;
import javax.naming.InitialContext;
import javax.naming.Context;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DbConnector {
public DbConnector() {
}
InitialContext initCtx = null;
Connection conn = null;
Statement stmt = null;
public static void main(String[] args) {
DbConnector dbconnector = new DbConnector();
Statement stmtt = dbconnector.getStatement();
}
public Statement getStatement() {
try {
initCtx = new InitialContext();
DataSource ds = (DataSource)
initCtx.lookup("java:comp/env/jdbc/foo");
conn = ds.getConnection();
stmt = conn.createStatement();
}catch (Exception E){
E.printStackTrace();
}
return stmt;
}
public void closeItems() {
try {
conn.close();
initCtx.close();
}catch (Exception E) {
}
}
}