tags:

views:

637

answers:

4

Hello, I was using msacces n tomcat6.0 previously for my application testing and was working fine. Now I want to use myswl database through apache tomcat 6.0. I have wamp server installed on my computer so want to connect to same mysql server. I have created database succesfully on it. I have put mysql-connector-java-5.1.9 , mysql-5.0.85-win32 , MS sql driver in lib of tomcat 6.0 as wel as that of my application to ensure it is in classpath.I have following code in my jsp.

Driver d = (Driver)Class.forName("com.mysql.jdbc.Driver").newInstance();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql:3306///tmp/mysql.sock","admin", "admin");

[wampmysqld] port = 3306 socket = /tmp/mysql.sock

I am getting error as

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.apache.jsp.insert_jsp._jspService(insert_jsp.java:78)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:574)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1527)
    at java.lang.Thread.run(Unknown Source)

What changes i have to make in my web.xml file/server manager.

+1  A: 

You have to put the mysql driver JAR file in apache-tomcat/common/lib/ directory

You can download it from http://dev.mysql.com/downloads/connector/j/5.1.html

Get the put the mysql-connector.jar there

Daniel Cukier
I have mysql-connector-java-5.1.9 , mysql-5.0.85-win32 , MS sql driver in both \Apache Software Foundation\Tomcat 6.0\lib and in webapps\application\WEB-INF\lib
Chava
Have you tried to add it to common/lib ?
Daniel Cukier
+1  A: 

Did you download the MySQL JDBC driver from

http://dev.mysql.com/doc/refman/5.0/en/connector-j.html

Is that jar file in your classpath? The error you are showing indicates that the MySQL JDBC Driver IS NOT in your classpath.

Jim Garrison
+1  A: 

I have put mysql-connector-java-5.1.9 , mysql-5.0.85-win32 , MS sql driver in lib of tomcat 6.0 as wel as that of my application to ensure it is in classpath.

First, what are the actual files?

  • mysql-connector-java-5.1.9 - I'm assuming this is the JDBC driver - the actual file should be mysql-connector-java-5.1.9-bin.jar
  • mysql-5.0.85-win32 - what is this? The MySql installer? If so, you don't need this.
  • MS sql driver - I thought you wanted MySQL?

Second - "in lib of tomcat 6.0 as wel as that of my application to ensure it is in classpath." - Don't do this... it needs to be in one or the other - not both.

Third - "jdbc:mysql:3306///tmp/mysql.sock" - isn't a correct JDBC connection URL - it's something like - jdbc:mysql://localhost:3306/databaseName

You should be able to copy mysql-connector-java-5.1.9-bin.jar into either your Tomcat lib directory or packaged in your WAR's lib directory, and then use the correct JDBC connection string to connect to this database in your code.

There's a page on how to set up many databases (including MySQL) as a JNDI DataSource in Tomcat 6.0 - this is above and beyond what you're trying to do here, but the initial steps are the same, and it provides some example configurations.

Nate
I have tried keeping mysql-connector-java-5.1.9-bin.jar in tomcat\lib or application\web-inf\lib but now I am getting error as java.sql.SQLException: No suitable driver found for jdbc:mysql:3306//localhost:3306/project at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source)
Chava
Look at the third point above - the :3306 is the port part of the URL - not part of the protocol.
Nate
I mean I am getting following error. java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/projectIs it a classpath problem??I am using another jar file(Imageio) that is kept in tomcat\lib and it is working fine.I am using following code now.Do u find any error in it. Note: I am using mysql of wamp server.Driver d = (Driver)Class.forName("com.mysql.jdbc.Driver").newInstance();Class.forName("com.mysql.jdbc.Driver");Connection con=DriverManager.getConnection("jdbc:mysql//localhost:3306/project","admin", "admin");
Chava
A: 

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","admin", "admin");

Note there was : missing before local host.

Chava