GregMacken
asked on
Java file won't connect to DB
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.Driv er").newIn stance();
System.out.println("connec ted");
con = DriverManager.getConnectio n("jdbc:my sql://loca lhost/traf fic");
Statement statement = con.createStatement();
System.out.println("connec ted");
statement.executeUpdate("i nsert 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?
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.
System.out.println("connec
con = DriverManager.getConnectio
Statement statement = con.createStatement();
System.out.println("connec
statement.executeUpdate("i
}
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?
ASKER
The output is...........
java.lang.ClassNotFoundExc eption: org.gjt.mm.mysql.Driver
at java.net.URLClassLoader$1. run(URLCla ssLoader.j ava:199)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(UR LClassLoad er.java:18 7)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:289)
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :274)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:235)
at java.lang.ClassLoader.load ClassInter nal(ClassL oader.java :302)
at java.lang.Class.forName0(N ative Method)
at java.lang.Class.forName(Cl ass.java:1 41)
at work.main(work.java:19)
java.lang.ClassNotFoundExc
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
at java.lang.Class.forName0(N
at java.lang.Class.forName(Cl
at work.main(work.java:19)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I copied mysql-connector-java-3.0.1 0-stable-b in.jar into all lib folders in the tomcat directory and in still doesn't work.
I thought you were trying to run this as an app? If a webapp, put it into your WEB-INF/lib
put your jar in <webapp>/WEB-INF/lib
ASKER
there seems to be no lib folder in webapps/web-inf
There should be. It's WEB-INF/lib of your webapp
> webapps/web-inf
not webapps, the WEB-INF folder inside your web application root directory.
not webapps, the WEB-INF folder inside your web application root directory.
(i.e. of each individual webapp)
ASKER
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?
It's the lib/ext of your Java runtime and SDK that i mean not Tomcat
(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?
>>when I use the same code in a servlet it worls perfectly. Any idea why it won't work as a normal java file?
> Should I be setting the classpath?
yes you can add the jar file to your CLASSPATH.
yes you can add the jar file to your CLASSPATH.
ASKER
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-bi n.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-b in
but still it gives the same error
Here's what I did.......
I moved this java file to c:\
I placed the mysql-connector-java-3.0.6
and set the classpath as follows......
set classpath=C:\j2sdk1.4.2_03
but still it gives the same error
It's lib\ext
not lib
not lib
It should also go in lib\ext of the runtime too
> set classpath=C:\j2sdk1.4.2_03 \lib\mysql -connector -java-3.0. 6-stable-b in
set classpath=C:\j2sdk1.4.2_03 \lib\mysql -connector -java-3.0. 6-stable-b in.jar
and open a new command window
set classpath=C:\j2sdk1.4.2_03
and open a new command window
ASKER
it worked....... I placed the jar file in jre\lib\ext as you suggested
thanks.. sorry I'm slow on the uptake
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.
>>thanks.. sorry I'm slow on the uptake
No problem.
No problem.
ASKER
thanks all: -}
An alternative would be to set the classpath to the jar (inclusive) as an *environment* variable.
>>e.toString();
to
e.printStackTrace();
and let us know the output