eexcel
asked on
JNDI DataSource unable to work !!
Hi guys,
/usr/local/tomcat5/webapps /Advanced/ WEB-INF/we b.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.DataSo urce</res- type>
<res-auth>Container</res-a uth>
</resource-ref>
</web-app>
========================== ========== ========== ========== ===
/usr/local/tomcat5/conf/se rver.xml
========================== ========== ========== ========== ===
.............
<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.cata lina.logge r.FileLogg er" 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.Basic DataSource Factory</v alue>
</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</nam e>
<value>org.gjt.mm.mysql.Dr iver</valu e>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1. 3.57:3306/ books</val ue>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
========================== ========== ========== ========== ===
/usr/local/tomcat5/webapps /Advanced/ connect.js p
========================== ========== ========== ========== ===
<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/cl asses/com/ wrox/libra ry/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("ja va:comp/en v/jdbc/Boo ksDB");
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 !!
/usr/local/tomcat5/webapps
==========================
<?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-type>javax.sql.DataSo
<res-auth>Container</res-a
</resource-ref>
</web-app>
==========================
/usr/local/tomcat5/conf/se
==========================
.............
<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.cata
suffix=".txt" timestamp="true"/>
<ResourceParams name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource
<parameter>
<name>factory</name>
<value>org.apache.commons.
</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</nam
<value>org.gjt.mm.mysql.Dr
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1.
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
==========================
/usr/local/tomcat5/webapps
==========================
<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
==========================
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("ja
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 !!
ASKER
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("ja va:comp/en v/jdbc/Boo ksDB");
stat = "4";
Pls help !!
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("ja
stat = "4";
Pls help !!
Are u using any IDE like JBUilder or IntelliJ etc...
ASKER
Hi Apple,
I m using mysql-4.0.14b-win.zip installed under Windows 2K Server and mysql-connector-java-3.0.8 -stable-bi n.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.cata lina.logge r.FileLogg er" 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.Basic DataSource Factory</v alue>
</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</nam e>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeo ut</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</nam e>
<value>org.gjt.mm.mysql.Dr iver</valu e>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1. 3.139:3306 /books?aut oReconnect =true</val ue>
</parameter>
</ResourceParams>
</Context>
and I still can't get it to connect. I still stucked at
ds == NULL
Pls..................help. .......... ........
I m using mysql-4.0.14b-win.zip installed under Windows 2K Server and mysql-connector-java-3.0.8
1) commons-dbcp.jar
2) commons-pool.jar
3) commons-collections.jar
to /usr/local/tomcat5/common/
I have also modified server.xml file as follow:
<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.cata
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.
</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</nam
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeo
<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</nam
<value>org.gjt.mm.mysql.Dr
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1.
</parameter>
</ResourceParams>
</Context>
and I still can't get it to connect. I still stucked at
ds == NULL
Pls..................help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
is your sql server running now. I will try to connect
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!
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!
oops i forgot
check if you have a databse called "books" in your MySQL databse
check if you have a databse called "books" in your MySQL databse
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!
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!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 !!
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 !!
>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?
is it is a user for the database or a user in the database?
if you choose a comment for an answer. This question would move to the solved section helping all
:)
Cheers!
:)
Cheers!
always check you tomcat log, those kind of error, like user/password is not correct, should show up in you tomcat log.
ASKER