0

我在连接我的 java 和 mysql 数据库时遇到问题,昨天工作正常,但现在不工作了,这是我的代码。

public static void main(String[] args) {


    try 
    {
        Class.forName ("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e)
    {
        System.out.println ("Could not load the driver");
    }

    String user, pass, host, database;
    user = "Michael";
    pass = "Blaine22";
    host = "localhost";
    database = "maintenance_work";

    Connection conn = DriverManager.getConnection
            ("jdbc:mysql://"+host+":3306/"+database, user, pass);

    conn.setAutoCommit(false);


    //Menu code:
    appSchoolMaintenance newWork = new appSchoolMaintenance();
    newWork.statement1(); // opens the start method


}
4

3 回答 3

3

MySQL JDBC 驱动程序(您可以在此处获取:http: //dev.mysql.com/downloads/connector/j/)添加到应用程序的类路径并删除不必要的代码:

    try 
    {
        Class.forName ("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e)
    {
        System.out.println ("Could not load the driver");
    }
于 2014-04-18T06:48:27.277 回答
1

可以尝试直接用 MySQL JDBC 驱动连接

com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
Connection conn = driver.
            connect("jdbc:mysql://localhost/test?user=root&password=root", null);

如果此代码确实编译您在类路径中缺少 MySQL JDBC 驱动程序

于 2014-04-18T07:29:18.310 回答
0

再次开始工作 - 尝试了所有建议,但又开始工作,没有押韵或理由!谢谢你的帮助

于 2014-04-18T09:40:51.770 回答