Newbie Question: Problem with Tomcat and MySql

A

asd

Hello all,

I am trying to develop a servlet that connects to MySql db.

Here is what I did.
Downloaded mysql-connector-java-5.0.5.

Copied "mysql-connector-java-5.0.5-bin.jar" file under
TOMCAT_DIR//common/lib

As instructed on
http://tomcat.apache.org/tomcat-4.1-doc/printer/jndi-datasource-examples-howto.html

I changed my TOMCAT_DIR/webapps/ROOT/WEB-INF/web.xml file to
<?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/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>


and I added following lines to server.xml under conf.



<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">

<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>

<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>

<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>MYUSERNAME</value>
</parameter>
<parameter>
<name>password</name>
<value>MYPASSWORD</value>
</parameter>

<!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>

<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>

</Host>

</Engine>

</Service>

</Server>



I get following error when I run following code.
Here is error:
SQLException: No suitable driver found for
jdbc:mysql://localhost/javatest?user=tayf&password=nothing SQLState: 08001
VendorError: 0


Here is code
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/** Simple servlet for testing the use of packages.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* &copy; 2003 Marty Hall; may be freely used or adapted.
*/

public class HelloServlet2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello (2)</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello asdasdas (2)</H1>\n");


try {
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/javatest?" +
"user=tayf&password=nothing");

// Do something with the Connection

} catch (SQLException ex) {
// handle any errors
out.println("SQLException: " + ex.getMessage());
out.println("SQLState: " + ex.getSQLState());
out.println("VendorError: " + ex.getErrorCode());
}

out.println("</BODY></HTML>");



}
}




I would be more than glad if you could help me figure out this problem.
Sincerely yours.
asd
 
A

Andrew Thompson

Copied "mysql-connector-java-5.0.5-bin.jar" file under
TOMCAT_DIR//common/lib

To the best of my knowledge, archived classes
need to be put in WEB-INF/lib to be accessible
to the web-app (but I am no expert, especially
when it comes to D/B's).

Andrew T.
 
L

Lew

Why the double slash?

Andrew said:
To the best of my knowledge, archived classes
need to be put in WEB-INF/lib to be accessible
to the web-app (but I am no expert, especially
when it comes to D/B's).

common/lib/ and shared/lib/ are two places one can put JARs in Tomcat.
shared/lib/ is where libraries go that any app running on that app server can
use; common/lib/ is accessible to Tomcat itself as well. So one might put,
say, Apache commons libs in shared/lib/ wher all apps can get them, and a JDBC
JAR in common/lib/ so Tomcat itself can perform authentication via the DB.
I changed my TOMCAT_DIR/webapps/ROOT/WEB-INF/web.xml file to

Is your test web app running in the root context? If not, you likely will need
to change the web.xml for the application you are running.

Your posted code did not have a

Class.forName( "com.mysql.jdbc.Driver" );

Did you have that code anywhere that you didn't show us? Did you set up the
load of the driver class in a property file?
out.println(docType +
"<HTML>\n" +

You should start getting in the habit of using lower-case tags and attributes
in HTML.

-- Lew
 
A

asd

Why the double slash?

Typo :)

Is your test web app running in the root context? If not, you likely will need
to change the web.xml for the application you are running.

I am going to put them under ROOT. Everything is ok as long as they work.
Your posted code did not have a

Class.forName( "com.mysql.jdbc.Driver" );

Did you have that code anywhere that you didn't show us? Did you set up the
load of the driver class in a property file?

That was the problem I forgot Class.forName( "com.mysql.jdbc.Driver" );
in my code.

Thank you so much Lew and Andrew for your comments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top