Problem with Tomcat 6.0 / connector/j / jdk1.6 /JDBC

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codeninja
    New Member
    • Sep 2007
    • 2

    Problem with Tomcat 6.0 / connector/j / jdk1.6 /JDBC

    Hello,
    I'm trying to write a jsp page that calls a java class, which accesses a MYSQL database, and for some reason i get "java.lang.Clas sNotFoundExcept ion: com.mysql.jdbc. Driver" while trying to establish the connection. I tried putting the connector .jar in the WEB-INF\lib, I tried setting the classpath variable to the connector's location, and nothing seems to fix it. Am I using the wrong class for the driver, or is it something else.
    Here is the connection code:

    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
    import com.sun.rowset. JdbcRowSetImpl;

    public class DatabaseApp
    {
    static Connection connection;
    static Statement statement;

    static final String JDBC_DRIVER = "com.mysql.jdbc .Driver";
    static final String DATABASE_URL = "jdbc:mysql ://localhost/mydatabase?user =blah&password= blahblah";

    public DatabaseApp()
    {
    connection = null;
    statement = null;

    //connect to the database
    try
    {
    Class.forName(J DBC_DRIVER);
    connection = DriverManager.g etConnection(DA TABASE_URL);

    //if connected to database, output String
    if (!connection.is Closed())
    System.out.prin tln("CONNECTED TO THE DATABASE");
    }
    catch ( SQLException e ) //exception in SQL query
    {
    e.printStackTra ce();
    } // end catch

    catch ( ClassNotFoundEx ception e ) //exception in Class connection
    {
    e.printStackTra ce();
    } // end catch

    /*catch ( InstantiationEx ception e ) //exception in Class connection
    {
    e.printStackTra ce();
    } // end catch

    catch ( IllegalAccessEx ception e ) //exception in Class connection
    {
    e.printStackTra ce();
    } // end catch*/
    }
    ...

    Any ideas?
  • shegoj
    New Member
    • Sep 2007
    • 1

    #2
    Hi,

    Drop the connector .jar in your $JAVA_HOME/jre/lib/ext directory, then retry your code. If the code works , then the problem has to do with setting your class path, else it could be that you are using bad/wrong .jar.

    Originally posted by codeninja
    Hello,
    I'm trying to write a jsp page that calls a java class, which accesses a MYSQL database, and for some reason i get "java.lang.Clas sNotFoundExcept ion: com.mysql.jdbc. Driver" while trying to establish the connection. I tried putting the connector .jar in the WEB-INF\lib, I tried setting the classpath variable to the connector's location, and nothing seems to fix it. Am I using the wrong class for the driver, or is it something else.
    Here is the connection code:

    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
    import com.sun.rowset. JdbcRowSetImpl;

    public class DatabaseApp
    {
    static Connection connection;
    static Statement statement;

    static final String JDBC_DRIVER = "com.mysql.jdbc .Driver";
    static final String DATABASE_URL = "jdbc:mysql ://localhost/mydatabase?user =blah&password= blahblah";

    public DatabaseApp()
    {
    connection = null;
    statement = null;

    //connect to the database
    try
    {
    Class.forName(J DBC_DRIVER);
    connection = DriverManager.g etConnection(DA TABASE_URL);

    //if connected to database, output String
    if (!connection.is Closed())
    System.out.prin tln("CONNECTED TO THE DATABASE");
    }
    catch ( SQLException e ) //exception in SQL query
    {
    e.printStackTra ce();
    } // end catch

    catch ( ClassNotFoundEx ception e ) //exception in Class connection
    {
    e.printStackTra ce();
    } // end catch

    /*catch ( InstantiationEx ception e ) //exception in Class connection
    {
    e.printStackTra ce();
    } // end catch

    catch ( IllegalAccessEx ception e ) //exception in Class connection
    {
    e.printStackTra ce();
    } // end catch*/
    }
    ...

    Any ideas?

    Comment

    • codeninja
      New Member
      • Sep 2007
      • 2

      #3
      It is already there. Perhaps it is a problem with the classpath then. What should it be set to? I set it to the directory that the connector .jar file is in (along with the servlet -api and jsp-api .jar files)

      Comment

      • ajos
        Contributor
        • Aug 2007
        • 283

        #4
        Originally posted by codeninja
        It is already there. Perhaps it is a problem with the classpath then. What should it be set to? I set it to the directory that the connector .jar file is in (along with the servlet -api and jsp-api .jar files)
        hi, this is how i set it in the environment variables(Class Path)-
        ;C:\Program Files\mysql-connector-java-5.1.2-beta\mysql-connector-java-5.1.2-beta-bin.jar....begi nning with a semicolon(;)

        Comment

        Working...