Koristim:
XP operativni sistem,
jdk1.6.0_23,
MySQL Community Server 5.5.8
Jar file od jconnectora je sacuvan na sledecoj lokaciji:
C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar
U komand promptu sam podesio classpath na sledeci nacin:
set CLASSPATH="C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar;"
Kod koji sam koristio za testiranje je sledeci:
import java.sql.*;
public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = "root";
String password = "password";
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 */ }
}
}
}
}
Program se bez problema kompajlira sa komandom: javac Connect.java
Greska koja mi se stalno pojavljuje kada pokusam da izvrsim: java Connect
je sledeca:
Exception in thread "main" java.lang.NoClassDefFoundError: Connect
Caused by: java.lang.ClassNotFoundException: Connect
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: Connect. Program will exit.
Ono sto ja ocekujem jeste da mi se pojavi: Database connection established
Izgubio sam dosta vremena pokusavajuc ovo da resim. Unapred sam vam zahvalan na svakoj pomoci.