Re: jdbc connection problem
Re: jdbc connection problem
A simple guess is that the database is not running or there is a firewall blocking the connection.
But just to rule out other issues, change your code to this, run it and paste the output in this thread again.
Code :
import java.sql.*;
public class Connect {
public static void main(String[] args) {
Connection conn = null;
try {
String userName = "root";
String password = "kickass";
String url = "jdbc:mysql://localhost:3306/student";
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: " + e.getMessage());
[b]e.printStackTrace();[/b]
} finally {
if (conn != null) {
try {
conn.close();
System.out.println("Database connection terminated");
} catch (Exception e) { /* ignore close errors */
}
}
}
}
}
// Json
Re: jdbc connection problem
Another quess is that the Class.forName() is not finding the class.
Re: jdbc connection problem
Thats a more likely one indeed.
// Json
Re: jdbc connection problem
sorry for late reply, hire is the error.
i don't get this, please help me on this one. tnx
Code :
Cannot connect to database server: com.mysql.jdbc.Driver
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at test.Connect.main(Connect.java:13)
Re: jdbc connection problem
Quote:
ClassNotFoundException: com.mysql.jdbc.Driver
The JVM is not finding the class named in the error message. You need to find the jar file containing that class and add it to the classpath for the JVM, so it can find it.
Re: jdbc connection problem
can you give a step by step procedure, if its ok, im using eclipse IDE, tnx.
Re: jdbc connection problem
Sorry I don't use an IDE.
Re: jdbc connection problem
I had the same problem.
You need to download Connector/J ( MySQL :: Download Connector/J ).
Now add the path of the mysql-connector-java-3.1.14-bin.jar(I downloaded this one) file to CLASSPATH.
set CLASSPATH=%CLASSPATH%;c:\<file location in >\mysql-connector-java-3.1.14-bin.jar;
Re: jdbc connection problem
Just download the connector like described above, put the connector jar file in your applications lib folder, in eclipse right click your project, click properties.
Click "Java Build Path" on the left.
Go to the "Libraries" tab.
Click "Add Jars"
Browse to the projects lib folder and add the connector jar file
Close all dialogs and try running the program in Eclipse like normal.
// Json
Re: jdbc connection problem
use this
driver to connect and use connectorJ Driver and keep the jar file in the class path
this solves ur problem
org.git.mm.mysql.driver