CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2001
    Posts
    35

    Can't find the jdbc driver class for mySQL

    Hi, can somebody help?

    The following servlet gives me the error message when run on IE.
    Error message is "java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver".
    The same jdbc connection on an application has no problem, only when it runs on a browser. Even if I use jdbc-odbc driver, that would produce the same kinda error. What's wrong? Thanks so much!

    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;

    public class showDB extends HttpServlet
    {
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
    res.setContentType("Text/html");
    ServletOutputStream out = res.getOutputStream();
    out.println("<html>");
    out.println("<head>");
    out.println("</head>");
    out.println("<body>");

    try
    {
    String url = "jdbcdbc:twdb";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection(url);
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from company");
    while(rs.next())
    {
    out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + "<br>");
    }
    rs.close();
    st.close();
    con.close();
    }
    catch(SQLException s)
    {
    out.println("SQL Error: " + s.toString() + " "
    + s.getErrorCode() + " " + s.getSQLState());
    }
    catch(Exception e)
    {
    out.println(e.toString());
    }
    finally
    {
    out.println("</body>");
    out.println("</html>");
    }
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
    doPost(req, res);
    }

    }

    This is my machine:
    1. Java sdk 1.4
    2. Tomcat servlet container
    3. mySQL

  2. #2
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    I assume that you already know that you have to provide a path to your mySql driver:

    but try the following

    Code:
    try
    {
       String url = "jdbc:mysql://localhost/twdb";
       Class.forName("org.gjt.mm.mysql.Driver").newInstance();
       Connection con = DriverManager.getConnection(url);
    //...
    Notice the url and the org.gjt.mm.mysql.Driver

    For more info look at http://mmmysql.sourceforge.net/doc/mm.doc/c106.htm

  3. #3
    Join Date
    Dec 2001
    Posts
    35
    Originally posted by Goodz13
    I assume that you already know that you have to provide a path to your mySql driver:

    but try the following

    Code:
    try
    {
       String url = "jdbc:mysql://localhost/twdb";
       Class.forName("org.gjt.mm.mysql.Driver").newInstance();
       Connection con = DriverManager.getConnection(url);
    //...
    Notice the url and the org.gjt.mm.mysql.Driver

    For more info look at http://mmmysql.sourceforge.net/doc/mm.doc/c106.htm
    It don't work. Why it works with my app but not from servlet? Is it something with the classpath? I have this classpath: Files\NetDirect\JDataConnect\JARs\JData2_0.jar;c:\j2sdk1.4.1_04\lib\mysql-connector-java-3.0.8-stable-bin.jar
    I think I saw some posts that said the jdbc driver used was different for an IE than for an app, is that true?

  4. #4
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    Servlets are server side, so your browser shouldn't make a difference. I'm not sure what to tell you if it works in an application, but not a servlet, unless Tomcat isn't reading the classpath properly.

  5. #5
    Join Date
    Dec 2001
    Posts
    35
    Originally posted by Goodz13
    Servlets are server side, so your browser shouldn't make a difference. I'm not sure what to tell you if it works in an application, but not a servlet, unless Tomcat isn't reading the classpath properly.
    Thanks, buddy. I'll try to find out with Tomcat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured