{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/MusicPlayer"); // line 139
conn = ds.getConnection( );
conn.setCatalog("MusicPlayer");
}
My web.xml is
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<resource-ref>
<description>
Music Player database
</description>
<res-ref-name>
jdbc/MusicPlayer
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
</web-app>
And I've added the following to server.xml:
<Context path="/myapp" docBase="myapp">
<Resource name="jdbc/MusicPlayer"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/MusicPlayer">
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>driverName</name>
<value>jdbc:mysql://localhost:3306/MusicPlayer</value>
</parameter>
<parameter>
<name>user</name>
<value>player</value>
</parameter>
<parameter>
<name>password</name>
<value>reyalp</value>
</parameter>
</ResourceParams>
</Context>
When I execute the JSP, the exception I get (copying from my call
on) is:
root cause
javax.naming.NamingException: Cannot create resource instance
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:167)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
at org.apache.naming.NamingContext.lookup(NamingContext.java:834)
at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
at org.apache.naming.NamingContext.lookup(NamingContext.java:194)
at com.hankbarta.music.MusicPlayerDB.(MusicPlayerDB.java:139)
.
.
.
And I also have the mysql jar file in the common/lib directory for
tomcat. I haven't been able to figure out why this doesn't work
but it appears to be a naming problem. I've tried several other
solutions gleaned from this NG but have not been able to get them
to work, though some of them did seem to go as far as to try
to load the mysql JDBC driver. (I thought I'd return to this
example since it seems to be the simplest and as near as I can
tell, should work. The other solution involved the use of factory
org.apache.commons.dbcp.BasicDataSourceFactory.)
Versions of what I'm using include:
java (j2ee) 1.4.2
Tomcat 4.0.4 (Debian package)
mysql jdbc 3.0.9
mysql 4.0.16 (Debian package)
thanks,
hank