Results 1 to 6 of 6
- 11-21-2011, 08:03 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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
Dao.javaJava Code:private Dao dao = new Dao(); public void init() { dao.connectToDB(); }
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) { } } } }
- 11-21-2011, 09:22 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 11-21-2011, 09:19 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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.
- 11-22-2011, 05:55 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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
- 11-22-2011, 09:40 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 11-22-2011, 04:54 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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
-
MySQL JDBC Driver ClassNotFound
By Levian in forum New To JavaReplies: 4Last Post: 06-17-2011, 09:37 AM -
ClassNotFoundException: com.mysql.jdbc.Driver
By tBKwtWS in forum New To JavaReplies: 9Last Post: 06-15-2011, 07:01 PM -
com.mysql.jdbc.Driver Exception !
By RealSilhouette in forum New To JavaReplies: 6Last Post: 06-10-2011, 09:30 AM -
com.mysql.jdbc.Driver
By uthpalaw in forum EclipseReplies: 2Last Post: 10-14-2010, 05:09 AM -
ClassNotFoundException com.mysql.jdbc.Driver
By Heather in forum JDBCReplies: 4Last Post: 03-31-2010, 12:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks