Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default 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

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Kindly show us your code in connecting database. Did you add the MySql's .jar to your library?

  3. #3
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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 added
    Last edited by Fubarable; 01-18-2011 at 02:39 AM. Reason: Moderator Edit: Code tags added

  4. #4
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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".

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    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?

  6. #6
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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.

  8. #8
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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:
    Java Code:
         String fname = "g.jpg";
         out.println("<img src=\"" + fname + "\">");
    with:
    Java Code:
         java.lang.ClassNotFoundException: g.jpg
    Thanks again!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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.

  10. #10
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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?

  12. #12
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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>

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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?

  14. #14
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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:
    server
    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
    and
    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

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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?

  16. #16
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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

  17. #17
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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.

  19. #19
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    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! :D
    Last edited by Gary20; 01-20-2011 at 07:25 PM.

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem with loading driver
    By b01g in forum New To Java
    Replies: 7
    Last Post: 07-15-2011, 06:41 PM
  2. Loading of JDBC Driver
    By Riyaz.hk77 in forum Advanced Java
    Replies: 11
    Last Post: 11-11-2010, 08:53 AM
  3. No suitable driver found exception error
    By Crazy Caveman in forum Networking
    Replies: 1
    Last Post: 09-01-2010, 12:03 AM
  4. Compile error in Driver class, help plz
    By Battlefeldt in forum New To Java
    Replies: 3
    Last Post: 11-25-2009, 11:30 PM
  5. Database Driver Error.....Plz Help!
    By sayan751 in forum NetBeans
    Replies: 0
    Last Post: 02-19-2009, 04:59 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
  •