471,049 Members | 1,278 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,049 software developers and data experts.

Using JNDI Reference to a mySQL Database

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:

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?

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

<!-- 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> <name>username</name>
<parameter> <name>password</name>
<parameter> <name>driverClassName</name>
<parameter> <name>url</name>
<parameter> <name>maxActive</name>
<parameter> <name>maxIdle</name>
<parameter> <name>maxWait</name>
<!-- === Database Resource Configuration === -->
<!-- Defines a "reference name" that resolves -->
<!-- to a DataSource which is more completely -->
<!-- defined in the Tomcat server.xml config file. -->


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
0 6021

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jon Dellaria | last post: by
1 post views Thread by slurper | last post: by
reply views Thread by sk | last post: by
5 posts views Thread by Pallavi Kadam | last post: by
reply views Thread by sajithamol | last post: by
2 posts views Thread by zalek | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.