Results 1 to 6 of 6
  1. #1
    icu222much is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default JDBC Problem - com.mysql.jdbc.Driver

    I am trying to connect to my MySQL DB with JDBC but I am having a problem with my servlet. I have my Data Access Object class which is able to establish a connection with the db. In my servlet init() function, I try to establish a connection using my DAO class but I keep getting the error message "com.mysql.jdbc.Driver". I do not understand what is happening as this worked fine in the DAO class.


    Servlet.java
    Java Code:
    private Dao dao = new Dao();
    
    public void init() {
     	dao.connectToDB();   	
    }
    Dao.java
    Java Code:
    public  void connectToDB() {
        try {
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (databaseURL, userName, password);
            System.out.println ("Database connection established");           
        } catch (Exception e) {
            System.err.println ("Cannot connect to database server");
            System.err.println ("Error message: " + e.getMessage());
        }  finally  {
            if (conn != null) {
                try {
                    conn.close ();
                    System.out.println ("Database connection terminated");
                } catch (Exception e) { }
            }
        }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: JDBC Problem - com.mysql.jdbc.Driver

    Always produce a complete stacktrace, even if only to a logfile. That is a "ClassNotFoundException". You do not have the dirver jarfile on your classpath.

  3. #3
    icu222much is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default Re: JDBC Problem - com.mysql.jdbc.Driver

    How do I do a complete trace stack?

    Can you further elaborate on "You do not have the dirver jarfile on your classpath"? Dao class worked by itself, and it is in the same project as my Servlet class.

  4. #4
    icu222much is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default Re: JDBC Problem - com.mysql.jdbc.Driver

    Problem is solved. Didn't realize that for a servlet you need to put the JAR file inside the web-inf/lib/ folder.

    I solved the problem.
    java - MySQL jdbc driver and Eclipse: ClassNotFoundexception com.mysql.jdbc.Driver - Stack Overflow

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: JDBC Problem - com.mysql.jdbc.Driver

    ClassNotFound is ClassNotFound. I have no idea what you did to come out with the statement "Dao class worked by itself", so I can only assume that whatever it was has nothing to do with the current situation. Also, "driver not on your classpath" is fairly self explanatory to me. See the documentation for your web container as to where you need to place jarfiles (sometimes a different place for driver jarfiles) and place it there.

  6. #6
    icu222much is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default Re: JDBC Problem - com.mysql.jdbc.Driver

    Sorry, I am still fairly new at this so I wasn't exactly sure what "ClassNotFound" exactly meant.

    What I meant by "Dao class worked by itself" was that when I ran the connectToDB() method from Dao.java (which is placed in the same project as Servlet.java), I did not get any error. That is what confused me.

    Thank you for your help though

Similar Threads

  1. MySQL JDBC Driver ClassNotFound
    By Levian in forum New To Java
    Replies: 4
    Last Post: 06-17-2011, 09:37 AM
  2. ClassNotFoundException: com.mysql.jdbc.Driver
    By tBKwtWS in forum New To Java
    Replies: 9
    Last Post: 06-15-2011, 07:01 PM
  3. com.mysql.jdbc.Driver Exception !
    By RealSilhouette in forum New To Java
    Replies: 6
    Last Post: 06-10-2011, 09:30 AM
  4. com.mysql.jdbc.Driver
    By uthpalaw in forum Eclipse
    Replies: 2
    Last Post: 10-14-2010, 05:09 AM
  5. Replies: 4
    Last Post: 03-31-2010, 12:08 PM

Posting Permissions

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