Hi i am trying to connect with the data base with the following code but it is giving me an error.
CODE:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dbaccess;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author elainevss
*/
public class DBConnector {
private String username;
private String password;
private String host;
private String dbName;
private Connection dbConnection;
public DBConnector() {
username = "root";
password = "";
host = "localhost";
dbName = "telephonedirectory";
}
public Connection getDbConnection() {
return dbConnection;
}
public void connect() {
String url = "jdbc:mysql://" + host + "/" + dbName;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
dbConnection = (Connection) DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Problem occurred!");
}
}
public void disconnect() {
try {
if (dbConnection != null) {
dbConnection.close();
}
} catch (SQLException e) {
e.printStackTrace();
// JOptionPane.showMessageDialog(null, "Problem occurred!");
}
}
}
Error
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at dbaccess.DBConnector.connect(DBConnector.java:42)
at dbaccess.DatabaseOperations.<init>(DatabaseOperations.java:23)
at presentation.DirectoryTester.initialize(DirectoryTester.java:37)
at presentation.DirectoryTester.main(DirectoryTester.java:26)
Exception in thread "main" java.lang.NullPointerException
at dbaccess.DatabaseOperations.idExists(DatabaseOperations.java:33)
at presentation.DirectoryTester.initialize(DirectoryTester.java:39)
at presentation.DirectoryTester.main(DirectoryTester.java:26)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)