You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Scott Purcell <sp...@vertisinc.com> on 2005/04/13 23:07:36 UTC

DBCP switch JNDI to DriverManager

Hello,

I am building a class to help encapsulte some basic JDBC calls, and originally I configured my Tomcat to use JNDI. So in my wisdom, I figured I would create a DataSource once in a singleton class. Then later on just call the ds.getConnection() when I need a connection. Is this proper usage (see below code)?


I ended up putting a main in the class for testing, but got shot down. When I try and run from command line, it says:
##
Need to specify class name in environment or system property, or as an applet pa
rameter, or in an application resource file:  java.naming.factory.initial null
##

So how can I change this snippet from JNDI DataSource to DriverManager and keep pooling working?

Sorry for all this, but once complete, I will update the Wiki with all I have learned.

Scott



##### code here #######

public class DBHandle {

  private static DBHandle me = null;

  private DataSource ds = null;

  private Statement stmt = null;
  private Connection conn = null;
  private String driverName = "";

  private DBHandle(String driverName) {
    this.driverName = driverName;
	// need to change from JNDI to DriverManager?
    try {
      Context ctx = new InitialContext();
      if(ctx == null )
        throw new Exception("Boom - No Context");

      ds = (DataSource)ctx.lookup("java:comp/env/jdbc/" + driverName);

      System.out.println("ds created.");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }


  public static void main(String[] args) {
    DBHandle dh = DBHandle.getInstance("unique");
    String query = "select foo from testdata";
    String data = dh.getString(query);
    System.out.println("data is: " + data);
    System.exit(-1);
  }


  public static DBHandle getInstance(String driverName) {
    if (me == null) {
      me = new DBHandle(driverName);
    }
    return me;
  }


  public String getString(String query) {
    return getString(query, new String [] {});
  }

  public String getString(String query, String s) {
    return getString (query, new String [] {s});
  }

  public String getString(String query, String [] params) {

    Connection dbconn = null;
    PreparedStatement stmt = null;
    String ret = null;
    try {
      dbconn = ds.getConnection();
      stmt = dbconn.prepareStatement(query);

      if (params != null)
        for (int i = 1; i <= params.length; i++) {
          stmt.setString(i, params[i - 1]);
        }

      ResultSet rs = stmt.executeQuery();
      ResultSetMetaData rsmd = rs.getMetaData();
      if (rs.next()) {
        ret = rs.getString(1);
      }
      rs.close();
    }
    catch(Exception e) {
      System.out.println(e.getMessage());
    }
    finally {
      try {
        if (stmt != null)
          stmt.close();
      }
      catch(SQLException e) {
        System.out.println(e.getMessage());
      }
      try {
        dbconn.close();
      } catch (SQLException e) {
        System.out.println(e.getMessage());
      }
    }

    return ret;
  }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org