java database connection failed

Hi started yesterday with java but i am not able to connect to my sql server !?

i copyes some stuff together what i guessing would be right relating to my c++ experience .....

here is the code:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.*;

//package mysql;

/**

*

* @author wayne

*/

publicclass Main{

/** Creates a new instance of Main */

public Main(){

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String[] args){

try{

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/test","root","");

// Statment st=con.createStatement();

System.out.println("CON: " + con);

}

catch(Exception exc){

//System.out.println("Failed to get connection");

exc.printStackTrace();

}

}

}

and the errors:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:169)

at Main.main(Main.java:25)

Probably a easy to solve problme for a more advances user...

Thanks a lot

Martin

[2708 byte] By [-Martin-a] at [2007-11-26 16:14:07]
# 1

Yes, you need to put the MySQL JDBC driver JAR in your CLASSPATH.

If you're coming from a C++ background, think of the JAR as a .dll or .so that you need to add to your link library path.

Download the MySQL JAR here:

http://www.mysql.com/products/connector/j/

Put that JAR in your CLASSPATH at runtime using the -classpath option on java.exe.

%

duffymoa at 2007-7-8 22:36:54 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

oh sry i forgot i did that... i can connect from runtime with my database

edit: i checkt it wasnt in like u said ... i added it and i get an additional error:

ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2

JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:820]

Message was edited by:

-Martin-

-Martin-a at 2007-7-8 22:36:54 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> oh sry i forgot i did that... i can connect from

> runtime with my database

Not making any sense here. What was that original exception from? Either your first post was wrong OR this one is. Which is it?

>

> edit: i checkt it wasnt in like u said ... i added it

> and i get an additional error:

>

> ERROR: JDWP Unable to get JNI 1.2 environment,

> jvm->GetEnv() return code = -2

> JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):

> [../../../src/share/back/util.c:820]

> Message was edited by:

> -Martin-

No idea what you're doing now.

%

duffymoa at 2007-7-8 22:36:54 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
sry my english is not that good, i added the mysql-connect to my libraries in the project, the connection with the database in runtime works but i still get the same error
-Martin-a at 2007-7-8 22:36:54 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

> sry my english is not that good, i added the

> mysql-connect to my libraries in the project,

How did you do this? CLASSPATH environment variable? Are you using an IDE like NetBeans?

> the connection with the database in runtime works

What runtime do you mean? The admin client for the database itself? If so, that says the database is up and available for requests. Necessary, but not sufficient.

> but i still get the same error

Meaning the ClassNotFoundException? If so, you haven't added the JAR to the CLASSPATH properly.

%

duffymoa at 2007-7-8 22:36:54 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

i use netbeans but only because i read its recommendet if u can suggest a better develop envirement i willed to listen ... runtime in netbeans - not an admin program for my database - .... i dont know netbeans well anough to say what it realy is i also added the drivers for mysql in the runtime window...furthermore i added the mysql module under all projekt->source packages -> -> properties->libaries -> compile, run, ect build projekt on clathpass is aktivated

the additional error accured only when i didnt add the mysql-connect to all libaries (compile, run ect)

at the moment i just get:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:169)

at Main.main(Main.java:25)

-Martin-a at 2007-7-8 22:36:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
ok found the error ... thank u very much
-Martin-a at 2007-7-8 22:36:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

> oh sry i forgot i did that... i can connect from

> runtime with my database

>

> edit: i checkt it wasnt in like u said ... i added it

> and i get an additional error:

>

> ERROR: JDWP Unable to get JNI 1.2 environment,

> jvm->GetEnv() return code = -2

> JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):

> [../../../src/share/back/util.c:820]

> Message was edited by:

> -Martin-

How did you get rid of this JDWP error?

joepaigejra at 2007-7-8 22:36:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...