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)

Try line 42 without the,

.newInstance();

Hope that helps!
Cleo

Thanks for your help but it did not work :/. do you know where shall i copy the sql connector.jar? i am using netbeans and it seems like that is the problem according to my internet research!


Try line 42 without the,

.newInstance();

Hope that helps!
Cleo

ok i solved the problem now i have another error

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
	at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4004)
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
	at java.sql.DriverManager.getConnection(DriverManager.java:582)
	at java.sql.DriverManager.getConnection(DriverManager.java:185)
	at dbaccess.DBConnector.connect(DBConnector.java:44)
	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

How did you solve it? First thing to do is check that your password is correct since the error message starts with 'access denied'

Well I just rememebered you're actually not using one as password="". Will need more expert advice on this one!

Doesn't your MySQL Workbench request for a User / Password when connecting to a database ? You can use the same credentials in your Java program as well.

BTW hopefully you are just playing with your own system here, as it is not recommended to use the 'root' account for normal day to day activities.

In case you have forgotten your root password but have access to the machine on which your mysql is installed, the following should help:-
http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.