Results 1 to 10 of 10
Thread: Newbie connection blues
- 12-08-2011, 08:45 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Newbie connection blues
Can someone point me to a simple complete working example of modern MYSQL / Java code ?
I’ve tried (no kidding) 10 tutorials/demos or so and I’m ashamed to say I’m almost ready to give up. Wasted 2 full days with this !
I have the Classpath to the MYSQL jar file OK. My code compiles OK
MYSQL 5.1 on Win XP.
I have *some* idea of what I’m doing (years of PHP and MYSQL as well as (shudder) years of MS
SQL Server) I am however just leaning Java and any tips on debugging the issue would be great too.
Many thanks !
- 12-08-2011, 09:35 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Newbie connection blues
Might be more productive if you post what you've tried in the form of an SSCCE, and post any and all compile time errors, runtime exceptions, or misbehavior. This will give your question a lot more foundation to work from.I’ve tried (no kidding) 10 tutorials/demos or so and I’m ashamed to say I’m almost ready to give up
- 12-08-2011, 10:11 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Newbie connection blues
One example I've tried; code compiles fine but
I get "Cannot connect to database server"
import java.sql.*;
public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = "root";
String password = "pw";
String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
- 12-08-2011, 10:17 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Newbie connection blues
First, please wrap your code in the code tags - it becomes readable to human eyes as a result. Second:"
This gives you little information as to what the exception is....add an e.printStackTrace() call and post the full stack trace you get.Java Code:catch (Exception e) { System.err.println ("Cannot connect to database server"); }
- 12-08-2011, 10:51 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Newbie connection blues
C:\dev\sockets>j b5
C:\dev\sockets>javac b5.java
C:\dev\sockets>java -classpath . b5
Cannot connect to database server
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at b5.main(b5.java:14)
(I've played with the classpath , having read various posts suggesting different ideas; most recently putting the file name itself into the path, not just the directory) still no joy)
- 12-08-2011, 11:30 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Newbie connection blues
This is one of the most common problems found when trying to use JDBC - search the forums and you will come up with dozens if not hundreds of this very problem. Make sure the JVM can find the driver class by adding it to the classpath as you are (I don't know where your ConnectorJ driver is located, so can't advice what to place in front of the classpath argument).
- 12-09-2011, 04:43 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Newbie connection blues
I've tested the MYSQL install; was able to get to it from another PC on my LAN via PHP. so that's not the issue. I"ve been over the classpath syntax....I'll try it from another windows machine and if NG either quit or try on a LINUX box. Any other thoughts, anyone , please feel free...
- 12-09-2011, 09:34 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Newbie connection blues
java -classpath . b5
That there implies that the driver jar file is located in the same directory as your code.
If it is not (and the error suggests it isn't) then you need to add it to the classpath:
java -classpath .;<path to jar file, either relative or full> b5
- 12-09-2011, 07:48 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Newbie connection blues
There are few places on this machine where that jar file does NOT exist at this point :)
I tried it both ways, see below.( to be explicit, the jar file is in both my source code directory and in the classpath directory)
-------------------
C:\dev\sockets>j b5
C:\dev\sockets>javac b5.java
C:\dev\sockets>java -classpath . b5
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at b5.main(b5.java:14)
C:\dev\sockets>java b5
Exception in thread "main" java.lang.NoClassDefFoundError: b5
Caused by: java.lang.ClassNotFoundException: b5
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: b5. Program will exit.
C:\dev\sockets>
- 12-09-2011, 10:07 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Newbie connection blues
Success:
This batch file works:
javac %1.java
java -classpath c:\java\mysql-connector-java-5.1.18-bin.jar;c:\dev\sockets %1
Note that I have two parts to the classpath:
1) the FULL path to the jar file including it's name and
2) a path (after the ;) to the working directory where the source code is.
Tolls I think you were telling me this ; many thanks ! I was a little deaf.
Far be it from me, a newbie of the 1st order, to question the Lords of Java , but
you need an explicit path to the working directory ?
I suppose there are lots more educational surprises on this road ...
Similar Threads
-
Java.net.socket connection :connection closed
By veeru541 in forum Advanced JavaReplies: 2Last Post: 06-27-2010, 02:14 AM -
newbie
By neeti in forum IntroductionsReplies: 3Last Post: 01-02-2010, 04:09 PM -
we implement connection pooling ourselves, but why it always out of connection ?
By zengqingyi12 in forum JDBCReplies: 7Last Post: 10-20-2009, 10:34 AM -
I am newbie
By Seoplanner in forum IntroductionsReplies: 0Last Post: 11-11-2008, 01:22 PM -
Newbie
By CSnoob87 in forum IntroductionsReplies: 2Last Post: 02-18-2008, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks