Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Eclipse+Tomcat+JNDI

 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to execute a simple Servlet program using JNDI and DB2 ON Eclipse platform. There is no problem in executing the problem without using Eclipse. On using Eclipse I get the following error:

Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1150)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at TestDB.doGet(TestDB.java:20)
................

I know this query has been posted many times but I am very much confused as none of the solutions proposed have worked for me hence posting this once again.

The name of my application is TestDB which resides in the context directory TomcatProject.

Here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<servlet>
<servlet-name>TestDB</servlet-name>
<servlet-class>TestDB</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestDB</servlet-name>
<url-pattern>/testdb</url-pattern>
</servlet-mapping>

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

I have created a separate META-INF folder along with the WEB-INF folder where I have placed Context.xml file which is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="\TomcatProject" docBase="C:\eclipse-SDK\TomcatProject">
<Resource name="jdbc/abc" auth="Container"
type="javax.sql.DataSource" username="db2admin" password="db2admin"
driverClassName="com.ibm.db2.jcc.DB2Driver" url="jdbc:db2:SAMPLE" maxActive="8" maxIdle="4" />
</Context>


Here is the sample code:

Context ctx = new InitialContext();
Context env = (Context)ctx.lookup("java:comp/env");

DataSource ds = (DataSource) env.lookup("jdbc/abc");
System.out.println("DataSource found successfully ... ");

Connection conn = ds.getConnection();
System.out.println("Connection established successfully ... ");

Please let me know if there is any solution to get around this problem. If anybody has executed any JNDI code using Eclipse platform and Tomcat then please let me know the steps to be executed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic