Tomcat database configuration


Web tier: servlets, JSP, Web frameworks: Tomcat database configuration

  1. Tomcat database configuration (2 messages)


    This is the sample code :
    Context initContext = new InitialContext();
    Context envContext = (Context)initContext.lookup("java:/comp/env");
     DataSource ds = (DataSource)envContext.lookup("dbJNDIRef");
        Connection connection = ds.getConnection();
        Statement statement=connection.createStatement();
        ResultSet result=statement.executeQuery(Sql);
    I have added dbJNDI as JNDI name for a datasource thru the Datasource link of admin area of Tomcat. The property Data Source URL has been set to jdbc:odbc:test( here, test refers to the dsn for a SQLServer database).

                      Next , I created a resource link with the properties set as name:dbJNDIRef, global :dbJNDI , type :javax.sql.DataSource in the admin area .

    After deployment, this is the error shown in the stdout.log file:

    Plz help meeee.

  2. Tomcat database configuration[ Go to top ]

    Hi SK-

    You don't need to add a resource reference. Just add a JNDI name for the data source and you can look it up via:

    String TOMCAT_JNDI_CONTEXT = "java:comp/env/"
    String myDataSourceJNDIName = "dbJNDI";

    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup(TOMCAT_JNDI_CONTEXT + myDataSourceJNDIName);

    Hope that makes sense. You should also check out the Service Locator pattern to save yourself some tedious lookup operations.


  3. Tomcat database configuration[ Go to top ]

    hai Paul ,
         Thank u for the help. It worked.