Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Newbie Question: Problem with Tomcat and MySql

Thread view: 
asd - 31 Mar 2007 04:36 GMT
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
Andrew Thompson - 31 Mar 2007 06:28 GMT
..
> 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.
Lew - 31 Mar 2007 16:11 GMT
asd <nos...@nospam.com> wrote:
>> Copied "mysql-connector-java-5.0.5-bin.jar" file under
>> TOMCAT_DIR//common/lib

Why the double slash?

> 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.

asd wrote:
> 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?

asd wrote:
> out.println(docType +
>             "<HTML>\n" +

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

-- Lew
asd - 31 Mar 2007 19:50 GMT
>>> Copied "mysql-connector-java-5.0.5-bin.jar" file under
>>> TOMCAT_DIR//common/lib
>
> Why the double slash?

Typo :-)


> asd wrote:
>> 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.

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.
Andrew Thompson - 31 Mar 2007 19:57 GMT
...
> Thank you so much Lew and Andrew for your comments.

Just happy I could encourage one of the
web-app. gurus to 'wade into the fray'
(answer the question).   ;-)

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.