PDA

View Full Version : Connecting to mysql database Netbeans


Blade_runner
12-18-2006, 08:40 AM
Hi i am trying to connect to mysql database in netbeans,i can view the tables from my runtime, but my connection script is not working this is my code.

package javaapplication2;

import java.sql.*;
import java.sql.Connection;
public class MyDatabaseConnection
{ public static void main(String[] args)
{ DB db = new DB();
Connection conn=db.dbConnect("jdbc:mysql://localhost:3306/bus", "root", "");
}

}
class DB
{
public DB() {
}
public Connection dbConnect(String db_connect_string,String db_userid, String db_password)
{ try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection( db_connect_string,db_userid,db_password);
System.out.println("connected");
return conn;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}


Now these are the errors that i get.

init:
deps-jar:
Compiling 1 source file to C:\Project\JavaApplication2\build\classes
compile-single:
run-single:
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:268)
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:164)
at javaapplication2.DB.dbConnect(MyDatabaseConnection.java:22)
at javaapplication2.MyDatabaseConnection.main(MyDatabaseConnection.java:14)

Has anybody ever had this problem before and solved it please help.
thank u

ess
12-18-2006, 12:11 PM
Hello there,

First of all, you need to do either:
1- add mysql-jdbc driver to your class path, or
2- add mysql-jdbc driver to your project.

If you have not done so already, you need to download jdbc driver for mysql from
http://www.mysql.com/products/connector/j/index.html

Adding mysql-jdbc driver to your class path in Windows XP
1- right-mouse click on "My Computer" in your desktop and click on properties
2- Click on the "Advanced" tab and then click on "Environment Variables"
3- Look in "System Variables" and see if you have an entry for "CLASSPATH". If not, create a new one.
4- Assuming that you have jdbc driver for mysql in C:\jdbc\mysql.jar, add the following line as a "Variable Value" for "CLASSPATH":
.;C:\jdbc\mysql.jar;%CLASSPATH%
5- Restart your computer

Add MySql driver to your project
Please read the tutorial from netbeans
http://www.netbeans.org/kb/50/using-netbeans/dbconn.html#99779

Let's know how you get on.

Good luck.
ess