JNDI DataSource unable to work !!

Hi guys,


/usr/local/tomcat5/webapps/Advanced/WEB-INF/web.xml
===========================================================
<?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>
<description>mySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/BooksDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
===========================================================

/usr/local/tomcat5/conf/server.xml
===========================================================
.............

<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="Wrox_Advanced_log."
suffix=".txt" timestamp="true"/>
<ResourceParams name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
<parameter>
<name>username</name>
<value>booksuser</value>
</parameter>
<parameter>
<name>password</name>
<value>bookspass</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1.3.57:3306/books</value>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
===========================================================

/usr/local/tomcat5/webapps/Advanced/connect.jsp
===========================================================
<html>
<head>
<title>Connection Test</title>
</head>
<body>

<%
com.wrox.library.Connect con = new com.wrox.library.Connect();
con.init();
%>

<h2>Connection Result</h2>
<%= con.getstat() %>
</body>
</html>
===========================================================

/usr/local/tomcat5/webapps/Advanced/WEB-INF/classes/com/wrox/library/Connect.java
===========================================================
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class Connect {

String stat = "Not Connected";

public void init() {
try {
stat = "1";
Context ctx = new InitialContext();
stat = "2";
if(ctx == null )
throw new Exception("Oops - No Context");
stat = "3";
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");
stat = "4";
if (ds != null) {
Connection conn = ds.getConnection();
stat = "5";
if(conn != null) {
stat = " Got Connection "+conn.toString();
conn.close();
}
}
} catch(Exception e) {
e.printStackTrace();
}
}

public String getstat() {
return stat;
}
}
===========================================================

When I tried to display connect.jsp, what I got display on IE6 is as follow:
=================================================
Connection Results
4
==================================================

Which means that the Connect.class unable to get the DataSource, for which the DataSource is NULL. Pls help !!
eexcelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eexcelAuthor Commented:
pls help guys .....thanks !!
0
eexcelAuthor Commented:
Hi applekanna,

    So what's wrong with my codes above?? Why can't it get connected ?
Why
ds == NULL at the code below ?

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");
stat = "4";


Pls help !!
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

applekannaCommented:
Are u using any IDE like JBUilder or IntelliJ etc...
0
eexcelAuthor Commented:
Hi Apple,

    I m using mysql-4.0.14b-win.zip installed under Windows 2K Server and mysql-connector-java-3.0.8-stable-bin.jar (placed under /usr/local/tomcat5/common/lib) and I also put
1) commons-dbcp.jar
2) commons-pool.jar
3) commons-collections.jar

to /usr/local/tomcat5/common/lib

I have also modified server.xml file as follow:

<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="Wrox_Advanced_log."
       suffix=".txt" timestamp="true"/>
<Resource name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/BooksDB">
<parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<parameter>
                <name>maxActive</name>
      <value>100</value>
</parameter>
<parameter>
      <name>maxIdle</name>
      <value>30</value>
</parameter>
<parameter>
      <name>maxWait</name>
      <value>10000</value>
</parameter>
<parameter>
                <name>removeAbandoned</name>
                <value>true</value>
</parameter>
<parameter>
               <name>removeAbandonedTimeout</name>
               <value>180</value>
</parameter>
<parameter>
                        <name>logAbandoned</name>
                        <value>true</value>
</parameter>
<parameter>
      <name>username</name>
      <value>booksuser</value>
</parameter>
<parameter>
      <name>password</name>
      <value>bookspass</value>
</parameter>
<parameter>
      <name>driverClassName</name>
                <value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
      <name>url</name>
      <value>jdbc:mysql://192.1.3.139:3306/books?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>

and I still can't get it to connect. I still stucked at
ds == NULL

Pls..................help...................
0
kennethxuCommented:
can you check your tomcat log and see if you get any error when startup?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
applekannaCommented:
is your sql server running now. I will try to connect
0
applekannaCommented:
eexcel.

I used your code om my machine and it works fine. These are the differences in my system.
I use com.mysql.jdbc.Driver.
I use my db.

Other than that no changes to your code except db changes.

I suspect the following

1. you might have not granted access to your database use "bookuser"

2. Extract from MySQL website
> "Versions of MySQL and the mm.mysql JDBC driver when have been reported to work:
>
>MySQL 3.23.47, MySQL 3.23.47 using InnoDB, MySQL 4.0.1alpha
>mm.mysql 2.0.14 (JDBC Driver)
>Please let us know if you have tested the new MySQL mm.mysql 3.0 driver."

Tyr using the older veriosn of the driver.

3. go back to
A step by step tutorial
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/printer/jndi-datasource-examples-howto.html#MySQL%20DBCP%20Example

and do that on ur machine. It will definately work

Does this help !
Cheers!


0
applekannaCommented:
oops i forgot
check if you have a databse called "books" in your MySQL databse
0
applekannaCommented:
I also tried connecting to your database

I get the following error
java.sqlException. BorrowFailed. Is there a MySQLserver running on the machine.

Check if you have started the mysql server

to start mysql server

C:\mysql\bin\mysqld
   
will start the server.

Cheers!
0
applekannaCommented:
Becoz of the error stated above this is what I get on my JSP
I get

Connection Result
4

which I guess is what you get :)
0
eexcelAuthor Commented:
Yeah, it works now !!
I have changed

<parameter>
     <name>username</name>
     <value>root</value>
</parameter>
<parameter>
     <name>password</name>
     <value>13467982</value>
</parameter>

The question is I m very sure that there is booksuser/bookspass in mysql db. I suppose that as long as there is a user "booksuser" in mysql table (user), I can connect in. Why is that I used booksuser/bookspass and can't even connect ? Thanks for u guys help !! Thanks a lot !!
0
applekannaCommented:
>The question is I m very sure that there is booksuser/bookspass in mysql db.

is it is a user for the database or a user in the database?
0
applekannaCommented:
if you choose a comment for an answer. This question would move to the solved section helping all

:)
Cheers!
0
kennethxuCommented:
always check you tomcat log, those kind of error, like user/password is not correct, should show up in you tomcat log.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.