By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,705 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,705 IT Pros & Developers. It's quick & easy.

Using JNDI Reference to a mySQL Database

P: n/a
mdh
I am trying to learn the basics of MVC applications using a Tomcat
infrastructure. I'm starting by building a simple application with:

* a login.jsp page for a basic login form with a action
pointing to "loginValidate.jsp"
* a loginValidate.jsp page that references logic to
validate requests
* a loginBean class used to pass info between the JSP
and validation logic
* a usersDAO class used to perform lookups in a USERS
table in a mySQL database
* a mainpage.jsp page for display after a successful login

I have most of the logic working when the validation logic imbeds a
static test for a username / password instead of querying the mySQL
database. However, the code in the loginBean that creates a usersDAO
object fails and returns the following exception:

javax.naming.NamingException: Exception creating DataSource:
org.hsql.jdbcDriver

The database itself and my mySQL driver JARs are OK cuz I have another
command line Java stub program that uses non-pooled, non-JNDI access
to open / query / update / close the DB and that works.

Examples on the web and in my Java / Tomcat books indicate the
preferred approach for DB access within Java is to:

* configure a <ResourceParams> object in the Tomcat server.xml
file that provides the DB name, userid, pw, and driver class
to use
* configure a <resource-ref> object in the application's web.xml
file that provides a JNDI name the application uses to reference
the <ResourceParams> in the server.xml file
* use the root JNDI Context to get the "java:comp/env" Context
then use that to find the DataSource parms for your DB resource
* use that DataSource to access the database

I've tried altering the environment strings passed to the Context
lookup but most of the errors seem to boil down to the JDBC class for
mySQL as requested in the server.xml file not being found and some
other DB driver class being used as a default. The
"org.hsql.jdbcDriver" is referenced with another example Context
defined in Tomcat's initial server.xml file. I've even tried changing
that reference to also use the mySQL driver class but it still seems
to default to this driver and fail.

Any ideas on what I'm missing?
mdh
=================================================
=================================================

OS / JAVA ENVIRONMENT
=====================
OS = SuSE Linux 8.1 Professional Kernal 2.4.19
Java = Java Enterprise Edition 1.4 Beta

PARTIAL CONTENTS OF:
$CATALINA_HOME/conf/server.xml
=================================
<!-- Context for "genapp" application and its database -->
<Context path="/genapp" docBase="genapp" debug="0"
reloadable="true" >
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="genapp_log." suffix=".txt" timestamp="true" />
<ResourceParams name="jdbc/genapp">
<parameter> <name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFact ory</value>
</parameter>
<parameter> <name>username</name>
<value>mdh</value>
</parameter>
<parameter> <name>password</name>
<value>letmein</value>
</parameter>
<parameter> <name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter> <name>url</name>
<value>jdbc:mysql://localhost/genapp</value>
</parameter>
<parameter> <name>maxActive</name>
<value>10</value>
</parameter>
<parameter> <name>maxIdle</name>
<value>30</value>
</parameter>
<parameter> <name>maxWait</name>
<value>10000</value>
</parameter>
</ResourceParams>
</Context>
PARTIAL CONTENTS OF:
$CATALINA_HOME/webapps/genapp/WEB-INF/web.xml
===============================================
<!-- === Database Resource Configuration === -->
<!-- Defines a "reference name" that resolves -->
<!-- to a DataSource which is more completely -->
<!-- defined in the Tomcat server.xml config file. -->

<resource-ref>
<res-ref-name>jdbc/genapp</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
PARTIAL SOURCE OF:
DAO CLASS ACCESSING THE JNDI OBJECT
=======================================

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

public class usersDAO {

// bunch of unrelated variables omitted from here

private Connection myconn;
private PreparedStatement myselect;
private PreparedStatement myupdate;
//-------------------------------------------------------------
// usersDAO.usersDAO -- the constructor / initialization method
//--------------------------------------------------------------
public usersDAO() throws SQLException, NamingException {

Context rootContext = new InitialContext();
Context jndiContext = (Context) rootContext.lookup("java:comp/env");
// javax.sql.DataSource ds = (DataSource) jndiContext.lookup("jdbc");
DataSource ds = (DataSource) jndiContext.lookup("jdbc/genapp");
myconn = ds.getConnection();
}
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.