Solved

Java file won't connect to DB

Posted on 2004-03-21
24
308 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?
0
Comment
Question by:GregMacken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 7
  • 5
  • +1
24 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645268
Change

>>e.toString();


to

e.printStackTrace();

and let us know the output
0
 

Author Comment

by:GregMacken
ID: 10645278
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)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 120 total points
ID: 10645283
Make sure the driver is in your classpath. Best thing is to put the jar into all your lib/ext directories
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:GregMacken
ID: 10645301
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.  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645310
I thought you were trying to run this as an app? If a webapp, put it into your WEB-INF/lib
0
 
LVL 92

Expert Comment

by:objects
ID: 10645313
put your jar in <webapp>/WEB-INF/lib
0
 

Author Comment

by:GregMacken
ID: 10645317
there seems to be no lib folder in webapps/web-inf
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645321
There should be. It's WEB-INF/lib of your webapp
0
 
LVL 92

Expert Comment

by:objects
ID: 10645330
> webapps/web-inf

not webapps, the WEB-INF folder inside your web application root directory.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645331
(i.e. of each individual webapp)
0
 

Author Comment

by:GregMacken
ID: 10645338
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645340
It's the lib/ext of your Java runtime and SDK that i mean not Tomcat
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645343
(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?
0
 
LVL 92

Expert Comment

by:objects
ID: 10645352
> Should I be setting the classpath?

yes you can add the jar file to your CLASSPATH.
0
 
LVL 92

Expert Comment

by:objects
ID: 10645354
0
 

Author Comment

by:GregMacken
ID: 10645392
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645400
It's lib\ext

not lib
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645405
It should also go in lib\ext of the runtime too
0
 
LVL 92

Expert Comment

by:objects
ID: 10645412
> 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

0
 

Author Comment

by:GregMacken
ID: 10645413
it worked....... I placed the jar file in jre\lib\ext  as you suggested

thanks.. sorry I'm slow on the uptake
0
 
LVL 1

Expert Comment

by:greenlantern0
ID: 10645417
I think if you change the classpath.. you may want to restart tomcat for it to take effect.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645422
>>thanks.. sorry I'm slow on the uptake

No problem.
0
 

Author Comment

by:GregMacken
ID: 10645424
thanks all: -}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645429
An alternative would be to set the classpath to the jar (inclusive) as an *environment* variable.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question