Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

Name jdbc is not bound in this Context error

U K Laxmi

2005-02-19

Replies:

I could able to get thro' JarperException. Thanks for
replies.

Now facing new problem.

I'm getting "NameNotFoundException" - Name jdbc is
not bound in this Context

Environment what i'm using is: Apache 2, Tomcat 5.5,
Netscape 7.2, MsAccess 2003 on windows 2000 machine.

I integrated tomcat and apache as apache doesn't
support jsps. I wrote a JSP called test-db.jsp which
in turn calls java program TestSQLLoad.java. This
TestSQLLoad.java performs dattabase operation, fetches
the data from table and that data is displayed on
Netscape thro' jsp.

1. test-db.jsp resides ina directory
TOMCAT_HOME/webapps/web/JSP. Contents are as follows:

<%@(protected)"
import="testpkg.TestSQLLoad"%>

 <html>
   <head>
    <title>DB Test</title>
   </head>
   <body>

   <%
    TestSQLLoad tst = new TestSQLLoad();
    tst.init();
   %>

   <h2>Results</h2>
    User -> <%= tst.getUser() %><br/>
    Pwd -> <%= tst.getPassword() %><br/>
    Id -> <%= tst.getID() %>

   </body>
  </html>

2. TestSQLLoad.java is residing in following
directory. TOMCAT_HOME/webapps/web/classes/testpkg
direcotry. Contents of this file are as follows:

 package testpkg;

 import javax.naming.*;
  import javax.sql.*;
  import java.sql.*;

  public class TestSQLLoad
  {
   
  String user = "Not Connected";
  String pwd = "no pwd";
  int id = -1;
 
  public void init() {
   try{
    Context ctx = new InitialContext();
    if(ctx == null ) {
    throw new Exception("Boom - No Context");
    }

   Context envCtx = (Context)
ctx.lookup("java:comp/env");
    DataSource ds = (DataSource)
envCtx.lookup("jdbc/db1");

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

    if (ds != null) {
    Connection conn = ds.getConnection();
   
    if(conn != null) {
     user = "Got Connection "+conn.toString();
     Statement stmt = conn.createStatement();
     String q = "select name, password, id from
user";
     ResultSet rst = stmt.executeQuery(q);
     if(rst.next()) {
      user=rst.getString(1);
      pwd=rst.getString(2);
      id = rst.getInt(3);
     }
     conn.close();
    }
    }
   }catch(Exception e) {
    e.printStackTrace();
   }
  }

  public String getUser() {
   return user;
  }
 
  public String getPassword() {
   return pwd;
  }
 
  public int getID()
  {
   return id;
  }
  }

3. I created a jar file using testpkg directory & put
it in TOMCAT_HOME/common/lib as well as
TOMCAT_HOME/webapps/web/WEB-INF/lib directory.

4. I created MsAccess database called db1.mdb and put
it in TOMCAT_HOME/WEBAPPS/WEB/db1 directory. I created
a table called user with fields name, password and ID.
ID is the primary key.

5. I included following in the server.xml of
TOMCAT_HOME/conf directory.

<Context path="/db1" docBase="C:/tomcat/webapps/db1"
       debug="5" reloadable="true"
crossContext="true">

<GlobalNamingResources>
<Resource name="jdbc/db1" auth="Container"
  type="javax.sql.DataSource"

factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
  driverClassName="sun.jdbc.odbc.JdbcOdbcDrive"
  url="jdbc:odbc://localhost/db1"
 maxActive="20" maxIdle="10" maxWait="-1" />
 </GlobalNamingResources>
</Context>

6. I included following in context.xml of
TOMCAT_HOME/conf directory.

<ResourceLink global="jdbc/db1" name="jdbc/db1"
type="javax.sql.DataSource"/>

7. I included following in web.xml of
TOMCAT_HOME/webapps/web/WEB-INF directory.

 <resource-ref>
    <res-ref-name>jdbc/db1</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

8. I copied commons-collections-2.1.1.jar,
commons-dbcp-1.2.1.jar and commons-pool-1.2.jar into
TOMCAT_HOME/common/lib directory.

9. I started apache, tomcat and then opened a browser
and typed http://localhost/web/JSP/test-db.jsp.

10. I get following on the browser.

"Results
User -> Not Connected
Pwd -> no pwd
Id -> -1"

11. On the tomcat window where tomcat is running i'm
getting following error.

javax.naming.NameNotFoundException: Name jdbc is not
bound in this Context
    at
org.apache.naming.NamingContext.lookup (NamingContext.java:769)
    at
org.apache.naming.NamingContext.lookup (NamingContext.java:152)
    at
org.apache.naming.factory.ResourceLinkFactory.getObjectInstance(Resou
rceLinkFactory.java:96)
    at
javax.naming.spi.NamingManager.getObjectInstance(Unknown
Source)
    at
org.apache.naming.NamingContext.lookup (NamingContext.java:792)
    at
org.apache.naming.NamingContext.lookup (NamingContext.java:139)
    at
org.apache.naming.NamingContext.lookup (NamingContext.java:780)
    at
org.apache.naming.NamingContext.lookup (NamingContext.java:152)
    at
testpkg.TestSQLLoad.init(TestSQLLoad.java:22)
    at
org.apache.jsp.JSP.test_002ddb_jsp._jspService(org.apache.jsp.JSP.tes
t_002ddb_jsp:54)
    at
org.apache.jasper.runtime.HttpJspBase.service (HttpJspBase.java:99)
    at
javax.servlet.http.HttpServlet.service (HttpServlet.java:802)
    at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:325)
    at
org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:2
95)
    at
org.apache.jasper.servlet.JspServlet.service (JspServlet.java:245)
    at
javax.servlet.http.HttpServlet.service (HttpServlet.java:802)
    at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
    at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
    at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
    at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
    at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
    at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
    at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
    at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
    at
org.apache.jk.server.JkCoyoteHandler.invoke (JkCoyoteHandler.java:306)

    at
org.apache.jk.common.HandlerRequest.invoke (HandlerRequest.java:385)
    at
org.apache.jk.common.ChannelSocket.invoke (ChannelSocket.java:745)
    at
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.ja
va:675)
    at
org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:868)
    at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
    at java.lang.Thread.run(Unknown Source)

11. Actually if i see the source code of TestSQLLoad
it's throwing error on line 22 where it's trying to
get datasource.

I've rpovided all the information that i can. Please
help to solve my problem and connect to database thro'
jsp successfully. Any help in this regard is highly
appreciated.

Thanks in advance.


   
__________________________________
Do you Yahoo!?
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
For additional commands, e-mail: tomcat-user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.