找不到适用于jdbc的驱动程序:mysql // localhost / sakila

时间:2019-05-30 14:48:21

标签: java mysql jdbc

我正在尝试设置JDBC,但出现此错误。

我尝试在pom.xml中添加依赖项,甚至jar文件也无法正常工作。我尝试了前面问题中提到的方法,但是没有用。

public class FilmLength {

public static void main(String[] args) throws SQLException  {
    Connection dbCon = null;
    PreparedStatement st = null;
    ResultSet rs = null;
    String url = "jdbc:mysql//localhost:3306/sakila";
    String username = "devuser";
    String password = "Demo@123";
    String query = "select * from film ";


    try {
        Class.forName("com.mysql.jdbc.Driver"); 
        dbCon = DriverManager.getConnection(url,username,password);
        st = dbCon.prepareStatement(query);
        rs = st.executeQuery();
        while(rs.next()) {
            String title = rs.getString(1);
            System.out.println(title);
        }
    } catch (Exception e) {

        e.printStackTrace();
    }
    finally {
        dbCon.close();
        st.close();
        rs.close();
    }



}

}

1 个答案:

答案 0 :(得分:1)

代替 String url = "jdbc:mysql//localhost:3306/sakila";

应该是 String url = "jdbc:mysql://localhost:3306/sakila";