没有为jdbc找到合适的驱动程序:mysql // localhost:3306 / demo?useSSL = false

时间:2018-03-26 12:00:49

标签: java mysql sql

我正在学习MySQL,我遇到了这个问题:

  

找不到适合jdbc的驱动程序:mysql // localhost:3306 / demo?useSSL = false   我已阅读其他解决方案,但它无法正常工作。   这是我的代码:

package jdbc.test;
import java.sql.*;

public class JdbcInsertDemo {
    public static void main(String[] args) throws SQLException {

        Connection myConn = null;
        Statement myStmt = null;
        ResultSet myRs = null;

        String dbUrl = "jdbc:mysql//localhost:3306/demo?useSSL=false";
                //      jdbc:mysql//localhost:3306/demo?useSSL=false    
        String user = "student";
        String pass = "student";

        try {
            myConn = DriverManager.getConnection("jdbc:mysql//localhost:3306/demo?useSSL=false", user, pass);
            myStmt = myConn.createStatement();
            System.out.println("Inserting a new employee to database\n");

            int rowsAffected = myStmt.executeUpdate("Insert into employees" + "(last_name, first_name, email, department, salary)" + "values" + "('Wright' , 'Eric', 'eric.wright@foo.com', 'HR', 33000.00)");
            myRs = myStmt.executeQuery("slect * from employees order by last_name");

            while(myRs.next()) {
                System.out.println(myRs.getString("last_name") + ", " + myRs.getString("first_name"));
            }
        }

        catch(Exception exc) {
            exc.printStackTrace();
        }

        finally {
            if (myRs != null) {
                myRs.close();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

我想你忘了在lib文件夹中包含jar文件。将驱动器jar文件放在该文件夹中,然后重试。

答案 1 :(得分:0)

有些事情可能是错的。

1:您是否在项目引用中包含了JDBC Jar?

    myConn = DriverManager.getConnection("jdbc:mysql//localhost:3306/demo?useSSL=false", user, pass);

另外,简而言之,因为你有'DbUrl' - MyConn可以简化。

    String dbUrl = "jdbc:mysql//localhost:3306/demo?useSSL=false";
    myConn = DriverManager.getConnection(dbUrl, user, pass);

答案 2 :(得分:0)

你错过了 border-right 在获取连接对象之前,需要添加此行。