无法加载驱动程序java.sql.SQLException:找不到适合jdbc的驱动程序

时间:2014-04-18 06:43:33

标签: java mysql dbconnection

我有连接我的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


}

3 个答案:

答案 0 :(得分: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");
    }

答案 1 :(得分: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驱动程序

答案 2 :(得分:0)

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