Re: Newbie connection blues
Quote:
I’ve tried (no kidding) 10 tutorials/demos or so and I’m ashamed to say I’m almost ready to give up
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.
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 */ }
}
}
}
}
Re: Newbie connection blues
First, please wrap your code in the code tags - it becomes readable to human eyes as a result. Second:"
Code:
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
This gives you little information as to what the exception is....add an e.printStackTrace() call and post the full stack trace you get.
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)
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).
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...
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
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>
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 ...