Results 1 to 20 of 22
Thread: Error loading driver
- 01-17-2011, 09:34 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Error loading driver
Hi
Trying to run Eclipse and MySQL. But I keep getting this error:
Error loading driver: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
1.) I tried adding the jar to WEB-INF dir.
2.) I have set the CLASSPATH variable:
.;C:\apache-tomcat-6.0.16\lib\servlet-api.jar;C:\NCCWorkspace\IDEGTest1\WebContent\WEB-INF\lib\mysql-connector-java-5.1.14-bin.jar
3.) I also tried setting:
-Java Build Path
-Libraries
-Add External JARs.
Is there something else I should be doing?
Thanks!
g
- 01-18-2011, 12:07 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Kindly show us your code in connecting database. Did you add the MySql's .jar to your library?
- 01-18-2011, 02:37 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Hi,
I added Jave-Build-Path -> Libraries -> Add External JAR.
My code looks like this (trying to connect in a servlet):
Java Code:import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class TestServlet extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //start response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType); out.println("<HTML>"); out.println("<HEAD><TITLE>IDEG</TITLE></HEAD>"); out.println("<BODY BGCOLOR=\"#FDF5E6\">"); //------------------------------ try { // Load database driver if it's not already loaded. // Establish network connection to database. Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection( "jdbc:mysql:idegschema", "root", "root"); //end code -------
Not sure why it is not working. So I hope you can tell me what I am doing wrong.
Also, is the url "jdbc:mysql:idegschema" correct? Do I point at the schema, schema and table, or just the table?
Thanks!
Moderator Edit: Code tags addedLast edited by Fubarable; 01-18-2011 at 02:39 AM. Reason: Moderator Edit: Code tags added
- 01-18-2011, 03:27 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Oh ... I also wrote a plain Java class and I can access the database. So that is working but when I copy it into a servlet, it just fails.
I also figured out the url should be "jdbc:mysql://localhost:3306/idegschema".
- 01-18-2011, 05:09 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I don't see any error now in connecting your database after changing
"jdbc:mysql:idegschema" to "jdbc:mysql://localhost:3306/idegschema".
Is post#3 is your latest code? if not kindly post again the latest and
the complete error message that gives you. If it has no error mmessage,
How did you know that it is/it is not working?
- 01-18-2011, 03:47 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Other than the change in the url, post #3 is my code. It works as a regular java class but not as an HttpServlet. Not sure why?
Also, I am thinking my web-inf is wrong. I don't see it in the project in Eclipse. I only copied the jar file to the directory and added it in the Libraries property. Is that the correct way? How do I add that jar to WEB-INF?
Thanks for the help!
g
- 01-18-2011, 05:08 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Referring to point 2 in your OP, which CLASSPATH variable?
The jars on which your app depends (which the MySQL driver is one) do not need to be on any CLASSPATH for Tomcat.
Which brings me to point 1 on your OP. Any dependent jars should be in WEB-INF/lib. This is where Tomcat picks up the webapp specific jars. If you only placed it in the WEB-INF directory then it will not be found.
Now, why Eclipse isn't packaging this correctly I can't say. I haven't used Eclipse for a year or so now (no reason, just that my clients in the past year haven't used it), so I can't remember what setting you are probably missing.
- 01-18-2011, 11:01 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Hi Tolls,
The CLASSPATH I set in Windows Environment variables. Can I remove that? I was afraid Tomcat would not pick up the jar.
For WEB-INF/lib, is that supposed to be in my project or in Apache? I think I am a little confused on this.
However, I did play around and seemingly got it to work. I added MySQL as an external plugin in my project and the driver loads and queries now! Yay!
But then it chokes when I try to insert an image:
with:Java Code:String fname = "g.jpg"; out.println("<img src=\"" + fname + "\">");
Thanks again!Java Code:java.lang.ClassNotFoundException: g.jpg
- 01-19-2011, 08:36 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Tomcat doesn't use that CLASSPATH for the webapps.
Actually, I don't think it uses that full stop.
How are you deploying this?
Your .war file should contain a WEB-INF directory, which shold have a classes directory inside which all your classes have been stuck, and a lib directory inside which is all your dependent jar files.
As I say, I can't remember how Eclipse handles all this.
Anyway, presuming you now have that sorted...
You'll have to show some more code, since that line looks OK from here.
Also the full stack trace.
It's possible your mapping is wrong in your web.xml.
- 01-19-2011, 03:10 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
OK I will remove it from my CLASSPATH. Thanks!
Deploying: I actually have not thought that far ahead. It is my first project; read **real** project. It was only ever mentioned in class but not demonstrated. So I have to look up how to package a war file and what to include.
Do I have to do anything with my web.xml? I never touched it.
Thanks for the help!
g
- 01-19-2011, 03:14 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Well, it might not be the web.xml, but it seems strange to me that Tomcat is looking for a class called g.jpg.
What does your final HTML look like (show source on your browser on that page) for that img tag?
- 01-19-2011, 03:37 PM #12
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
I am trying to store an image name in a DB and retrieving it and then referring to it on the webpage. Maybe I put the image in the wrong location?!?
I don't know why it calls the image a "class".
Here is the html:
Java Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>IDEG</TITLE></HEAD> <BODY BGCOLOR="#FDF5E6"> <img src="g.jpg"> link somewhere5 </BODY></HTML>
- 01-19-2011, 03:46 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
If you had a g.jpg at the top of your webapp (top directory) can you access it directory, using <your web app url>/g.jpg?
- 01-19-2011, 04:42 PM #14
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Hi,
I am confused. I tried spraying the file all over the place but it still doesn't show up on the webpage. I did rename "g.jpg" to "//localhost:8080/IDEGTest1/g.jpg" and now I do not get the class error.
I don't see my servlet in the Tomcat directory. Should I be copying files over?
I tried copying into:
serverand
C:\apache-tomcat-6.0.16\webapps
C:\apache-tomcat-6.0.16\webapps\ROOT
C:\apache-tomcat-6.0.16\webapps\ROOT\WEB-INF
project
C:\Workspace\IDEGTest1
C:\Workspace\IDEGTest1\WebContent
But the image still doesn't resolve. I guess you have me thinking about deployment and where the files should actually go. Esp since it is linked to the database.
Thanks! I thought I knew [enough] about this stuff but I guess I don't! :D
g
- 01-19-2011, 05:01 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
It depends how you're running this.
If you are testing it via Eclipse then it may (I can't remember) not deploy to Tomcat liek normal.
I prefer to have my Tomcat setup normally (with the debug hook setup in Catalina.bat), so I can physically deploy the Eclipse war file as if I was deploying to a production server. Then all Eclipse is doing is creating the war file, and connecting its debugger to my external (to Eclipse) Tomcat.
Of course, I suspect that's all Greek to you.
:)
Anyway, how is your project set up in Eclipse?
Is it a webapp project?
- 01-19-2011, 07:17 PM #16
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Right now I am just testing, using Eclipse, basic functionality of what I am trying to accomplish.
Haha ... so true ... give me a few weeks and it might mean less Greek to me! :D I think I just have to get the test code up and running with images, complete the coding, and then learn how to build the war files.
The project is setup as a dynamic web project in Eclipse.
g
- 01-19-2011, 10:35 PM #17
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Hi Tolls,
You got me thinking about deploying the servlet. So I thought I would try it. I copied some files over to Apache.
I placed the servlet in the ROOT "C:\apache-tomcat-6.0.16\webapps\ROOT\WEB-INF" and then created a project dir "C:\apache-tomcat-6.0.16\webapps\IDEGTest1\WEB-INF" and it still works. It's probably not the kosher way to deploy! But it works so I am happy! :D
But it still doesn't display the jpg though. It's like the simplest of things but I can't get it to work. :(
g
- 01-20-2011, 08:39 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That's an odd one then.
The normal structure is:
<tomcat directory>/webapps/<my web app name>
Under which will be:
The usual html stuff (image directory, any straight html files, etc etc).
WEB-INF directory, under which is:
classes, lib, jsp directories, along with the web.xml. Essentially the Java stuff.
Strictly speaking I would have said that that set up should not work, unless you've set up Tomcat in a strange way.
- 01-20-2011, 07:22 PM #19
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Hi I finally got it to work by turning off Eclipse and just using Tomcat and a browser. I guess Eclipse doesn't like images or starts Tomcat with some wierd configuration.
Here is my current structure:
C:\apache-tomcat-6.0.16\webapps\IDEGTest1 (for reg html)
C:\apache-tomcat-6.0.16\webapps\IDEGTest1\images (for images)
C:\apache-tomcat-6.0.16\webapps\IDEGTest1\WEB-INF\classes (for servlets)
C:\apache-tomcat-6.0.16\webapps\IDEGTest1\WEB-INF\lib (for sql jar)
Is this the correct way to do it? Do I deploy or generate a war file like this?
Thanks for all the help! You guys are awesome! My "project" is probably only 10% done but this gets me off to a good start. Now to look up file manipulation and PDFs. My friend better appreciate all this work; either that or I shot my mouth off too much! :DLast edited by Gary20; 01-20-2011 at 07:25 PM.
- 01-21-2011, 08:41 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
There are other standard directories (ie jsp) but that's the basic layout (plus the web.xml).
A war file is essentially that IDEGTest1 directory zipped up. Dropping a war file into the webapps directory results in Tomcat spotting it and deploying it as an app (or redeploying it if it already exists).
There's probably some setup thing for your Eclipse project that's gone wrong, but I wouldn't know which bit to flag. I work with different IDE's every few months so I tend to avoid remembering the ins and outs of any I'm not currently working with.
Similar Threads
-
Problem with loading driver
By b01g in forum New To JavaReplies: 7Last Post: 07-15-2011, 06:41 PM -
Loading of JDBC Driver
By Riyaz.hk77 in forum Advanced JavaReplies: 11Last Post: 11-11-2010, 08:53 AM -
No suitable driver found exception error
By Crazy Caveman in forum NetworkingReplies: 1Last Post: 09-01-2010, 12:03 AM -
Compile error in Driver class, help plz
By Battlefeldt in forum New To JavaReplies: 3Last Post: 11-25-2009, 11:30 PM -
Database Driver Error.....Plz Help!
By sayan751 in forum NetBeansReplies: 0Last Post: 02-19-2009, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks