JNDI issue - from Tomcat tutorial

[nobr]I changed names in the tutorial to get a string from mySQL db to the jsp

page. It returned no errors, just a default string - "Foo Not workin" I looked up

the tutorial, and the topics regarding this, it looks like it is jars, and naming

errors. I already put the jars in the common/lib directory - but I don't see any problem, you guys might help looking at the naming directory,

I might make some mistakes in the java file.

Files\netbeans-5.5.1\enterprise3\apache-tomcat-5.5.17\common\lib

commons-collections-3.2

commons-pool-1.3

commons-dbcp-1.2.2

mysql-connector-java-3.1.14-bin.jar

I edited the tomcat server.xml and added the context in <HOST> and </HOST>

<Context path="/refsheets">

<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"

maxActive="20" maxIdle="10" maxWait="-1" name="refsheets"

username="root" password="asdf" type="javax.sql.DataSource"

url="jdbc:mysql://localhost:3306/refsheets" />

</Context>

After that, I checked the server.xml, the context is moved to META-INF, I tried cutting and pasting, it kept moving to that,

I checked up the tomcat tutorial for JDNI - it states it's safe to edit the context.xml rather than editing the server.xml

the web.xml

<resource-ref>

<description>jdbc:mysql://localhost:3306/refsheets</description>

<res-ref-name>jdbc/refsheets</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

<res-sharing-scope>Shareable</res-sharing-scope>

</resource-ref>

the java file to access the db - I validated the sql statement. It should return "Programming".

package com.myapp.struts;

import javax.naming.*;

import javax.sql.*;

import java.sql.*;

publicclass DBTest{

int bar = -1;

String foo ="Not workin";

publicvoid init(){

try{

Connection conn =null;

Statement stmt =null;

Context initContext =new InitialContext();

if(initContext ==null )

thrownew Exception("Boom - No Context");

Context envContext = (Context)initContext.lookup("java:comp/env");

DataSource ds = (DataSource)initContext.lookup("jdbc/refsheets");

if (ds !=null){

conn = ds.getConnection();

if(conn !=null){

foo ="Got Connection "+conn.toString();

stmt = conn.createStatement();

ResultSet rst = stmt.executeQuery("select category from categories where category='programming'");

if(rst.next()){

foo=rst.getString("category");

}

conn.close();

}

}

}catch(Exception e){

e.printStackTrace();

}

}

public String getFoo(){return foo;}

}

Here's the jsp file -

<html>

<head>

<title>DB Test</title>

</head>

<body>

<%

com.myapp.struts.DBTest tst =new com.myapp.struts.DBTest();

tst.init();

%>

<h2>Results</h2>

Foo <%= tst.getFoo() %><br/>

</body>

</html>

I'm trying to figure out why it still is returning a default value.[/nobr]

[5043 byte] By [Meepa] at [2007-11-27 10:43:04]
# 1

nvm - posted in JDBC not JNDI forums.

Meepa at 2007-7-28 19:22:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Edit: I missed some of what you were doing.

Ok, I'm actually pretty puzzled; I'd expect you to get a failure on the lookup. You're not getting a connection back from the datasource, or you've got a null datasource.

I found your explanation of where you were putting the context information quite puzzling btw - bear in mind that not everyone uses NetBeans.

Normally you would put it into your application's META-INF/context.xml file. This will then be copied to the Tomcat server's conf/Catalina/localhost/appname.xml file when you deploy. IF you put entries directly into the server.xml file then changes made to either the META-INF/context.xml OR the appname.xml file will NOT be reflected.

I'm also a bit worried by your use of all-caps in the <host> element.

dcmintera at 2007-7-28 19:22:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

I think this:

name="refsheets"

Should probably be this:

name="jdbc/refsheets"

dcmintera at 2007-7-28 19:22:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

It returned an error - DAO stuff, so I believe it's resolved. thank you for helping.

Meepa at 2007-7-28 19:22:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...