473,245 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 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 6233

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Jon Dellaria | last post by:
I have been using MySql as the database using JSP's and JavaBeans but recently I have wanted to start using the database connection pooling mechanism built into TomCat. I think I am having a...
by: slurper | last post by:
i have problems understanding jndi. i'm this far: i deployed the sun application server and i'm working through the examples (Duke bookstore, ...). Sometimes i see code which does a lookup to a...
by: sk | last post by:
I just intalled the j2ee 5 and trying to run the pet store example. I have deployed. When I go to http://localhost:8080/petstore the top page apears. However, when i tried to populate the database...
by: Pallavi Kadam | last post by:
Tell Me Fullform of Jndl Tellme Jndi Is Releated To Topics Jndi Imp role In j2ee
by: dmjpro | last post by:
how do i do JNDI program in local machine.... plze give me JNDI programming free tutorial with examples .... plz help me .... thanx
by: sajithamol | last post by:
<env-entry> <env-entry-name>com/speedRate</env-entry-name> <env-entry-type>java.lang.Integer<env-entry-type> < env-entry-value>10</env-entry-value> </env-entry>
by: choukse | last post by:
Hi All, I am trying to bind to ADAM instance with a windows user through JNDI and it keeps failing. My ADAM and AD is running on same Windows 2k3 server. But, through LDP I am able to bind with...
by: zalek | last post by:
Hello, I wrote a program that is working OK which access DB2 using the following code: String w_conn = "jdbc:db2:DB2MAIN; Class.forName("COM.ibm.db2.jdbc.app.DB2Driver"); Connection db2Conn...
by: dmjpro | last post by:
I thought that when the connection gets closed then all resources get closed . I also included the ResultSet and Statement ;) . But if the Statement closed then only ResultSet closed. But now i got...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.