Newbie Question: Problem with Tomcat and MySql



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
.



Relevant Pages

  • mysql and libgcc_s.so.1
    ... I can't get my locally compiled copy of mysql to survive ... After the connection is closed, ... mysqld fails to find this file!? ... gcc 3.3.4 ...
    (Debian-User)
  • Re: Tomcat (thread awakening?)
    ... servlet an SQL query, the servlet uses a connection pool to connect to a MySQL DB. ... I thought perhaps this was merely because MySQL had some parameter within it that made it go to sleep -- but that is not the case here. ... and the browser refresh solution is the clue to what might be wrong. ... Either the session or the connection timed out. ...
    (comp.lang.java.programmer)
  • Re: Empire Servlet Test
    ... > trying to debug a connection problem that I am having to the new Bungy ... > to the blitzes with the servlet. ... > ISP or something else blocking port 6666, ... proxy worked fine. ...
    (rec.games.empire)
  • Re: looking for opinons regarding best practices (jdbc, resultsets, and servlet design)
    ... It allows passing of data entity value objects around without keeping a connection open. ... In a data-access-object layer approach, the DAO objects accept and pass back entities or collections, not ResultSets. ... In the MVC pattern there would be no servlet directly accessing JDBC. ... The controller is supposed to handle only parsing a request, its dispatch to model logic, then navigation to the subsequent view. ...
    (comp.lang.java.programmer)
  • Best place to look up JNDI resource, init db connection, in servlet.
    ... I'm new to Java servlet development. ... JNDI DataSource resource that I've defined in my servlet's context. ... Initialize db connection. ... Store item as class member. ...
    (comp.lang.java.programmer)