We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Java file won't connect to DB

GregMacken
GregMacken asked
on
Medium Priority
348 Views
Last Modified: 2013-11-24
Hi, when run the follwoing java file it doesn't update the database as desired... here's the code.........

import java.io.*;
import java.text.*;
import java.sql.*;
import java.util.*;

public class work {

 public static void main (String[] args) {


Connection con = null;
   
   
      try
    {
System.out.println("tries to connect");

      Class.forName("org.gjt.mm.mysql.Driver").newInstance();
     
      System.out.println("connected");

      con = DriverManager.getConnection("jdbc:mysql://localhost/traffic");
      Statement statement = con.createStatement();
System.out.println("connected");

 statement.executeUpdate("insert into x   values   ( 'ghjgjh')");


 
      }

    catch (Exception e)
    {
    e.toString();
    }
    finally
    {
      try
      {
        if (con != null)
          con.close();  //try to close the db connection
      }
      catch (SQLException sqle)
      {
       sqle.toString();
      }
    }

}


}



I get the ouput "tires to connect" but I don't get the output "connected so obviusly it's having a problem with the connection code. Btw when I use the same code in a servlet it worls perfectly. Any idea why it won't work as a normal java file?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Change

>>e.toString();


to

e.printStackTrace();

and let us know the output

Author

Commented:
The output is...........


java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:141)
        at work.main(work.java:19)
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
I copied mysql-connector-java-3.0.10-stable-bin.jar into all lib folders in the tomcat directory and in still doesn't work.  
CERTIFIED EXPERT
Top Expert 2016

Commented:
I thought you were trying to run this as an app? If a webapp, put it into your WEB-INF/lib
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
put your jar in <webapp>/WEB-INF/lib

Author

Commented:
there seems to be no lib folder in webapps/web-inf
CERTIFIED EXPERT
Top Expert 2016

Commented:
There should be. It's WEB-INF/lib of your webapp
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> webapps/web-inf

not webapps, the WEB-INF folder inside your web application root directory.
CERTIFIED EXPERT
Top Expert 2016

Commented:
(i.e. of each individual webapp)

Author

Commented:
anyway i did a search for every lib folder in the tomcat directory and any lib folder i came across I pasted the jar file into it. Should I be setting the classpath?
CERTIFIED EXPERT
Top Expert 2016

Commented:
It's the lib/ext of your Java runtime and SDK that i mean not Tomcat
CERTIFIED EXPERT
Top Expert 2016

Commented:
(your original comment said you didn't have a problem with servlets):

>>when I use the same code in a servlet it worls perfectly. Any idea why it won't work as a normal java file?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Should I be setting the classpath?

yes you can add the jar file to your CLASSPATH.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Author

Commented:
You're right i did say it was an app - sorry to confuse you.

Here's what I did.......

I moved this java file to c:\

I placed the mysql-connector-java-3.0.6-stable-bin.jar file in C:\j2sdk1.4.2_03\lib\

and set the classpath as follows......

set classpath=C:\j2sdk1.4.2_03\lib\mysql-connector-java-3.0.6-stable-bin

but still it gives the same error
CERTIFIED EXPERT
Top Expert 2016

Commented:
It's lib\ext

not lib
CERTIFIED EXPERT
Top Expert 2016

Commented:
It should also go in lib\ext of the runtime too
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> set classpath=C:\j2sdk1.4.2_03\lib\mysql-connector-java-3.0.6-stable-bin

set classpath=C:\j2sdk1.4.2_03\lib\mysql-connector-java-3.0.6-stable-bin.jar

and open a new command window

Author

Commented:
it worked....... I placed the jar file in jre\lib\ext  as you suggested

thanks.. sorry I'm slow on the uptake
I think if you change the classpath.. you may want to restart tomcat for it to take effect.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>thanks.. sorry I'm slow on the uptake

No problem.

Author

Commented:
thanks all: -}
CERTIFIED EXPERT
Top Expert 2016

Commented:
An alternative would be to set the classpath to the jar (inclusive) as an *environment* variable.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.