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]

