找不到合适的驱动程序错误

时间:2013-01-09 22:37:43

标签: java mysql connection

经过很长时间但没有用hibernate尝试,我决定使用本机mysql连接完成我的项目。 我找到了“找不到合适的驱动程序”错误,这是我代码的必要部分:

    static public Connection DatabaseConnection() {
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/mobil";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "diobookbla";

    try {
        Class.forName(driver).newInstance();
        conn = DriverManager
                .getConnection(url , userName, password);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
}

我已将mysql连接器添加到项目的构建路径中。 在这部分代码中,我得到null值,这导致空指针异常; conn = DriverManager.getConnection(url , userName, password);

Stacktrace =

java.sql.SQLException: No suitable driver found for jdbc:msql://localhost:3306/mobil
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at DAO.DatabaseConnection.DatabaseConnection(DatabaseConnection.java:19)
    at DAO.LogIn.SaveDatabase(LogIn.java:48)
    at DAO.LogIn.RegisterUsers(LogIn.java:18)
    at Test.main(Test.java:14)
Exception in thread "main" java.lang.NullPointerException
    at DAO.LogIn.SaveDatabase(LogIn.java:63)
    at DAO.LogIn.RegisterUsers(LogIn.java:18)
    at Test.main(Test.java:14)
java.lang.NullPointerException

0 个答案:

没有答案