微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jdbc到MYSQL错误:找不到适用于jdbc:mysql:// localhost:3306 / test的合适驱动程序?user='root'&password =''

如何解决jdbc到MYSQL错误:找不到适用于jdbc:mysql:// localhost:3306 / test的合适驱动程序?user='root'&password =''

如果您在建立第一个连接之前没有加载驱动程序,则可能发生这种情况。

Class.forName("com.MysqL.jdbc.Driver");

可以肯定的是,驾驶员必须进入/WEB-INF/lib,而不是进入/WEB- INF。顺便说一下,那里有一些sql注入漏洞。看看PreparedStatement。该finally还可以提高,因为你现在拥有它时,con当永远不会被关闭rs.close()抛出异常。

解决方法

我有以下代码连接到mysql数据库:

public static void insertIntoDatabase(String code,String name,String temp,String hum,String del) {
    Connection con = null;
    ResultSet rs = null;

    String url = "jdbc:mysql://localhost:3306/test";
    String user = "root";
    String password = "";

    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url,user,password);
        rs = con.prepareStatement("CREATE TABLE IF NOT EXISTS AiportDetails(code VARCHAR(50) PRIMARY KEY," +
                "name VARCHAR(50),temp VARCHAR(50),hum VARCHAR(50),del VARCHAR(50)) ENGINE=InnoDB;").executeQuery();
        rs = con.prepareStatement("INSERT INTO AirportDetails(code,name,temp,hum,del) VALUES("+code+","+
                name+","+temp+","+hum+","+del+");").executeQuery();
    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

我收到以下错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

注意 我在网上发现了一些常见的更正:

1. The driver is not in the /WEB-INF/lib folder.
2. The url is wrong.

我不认为我的代码就是这种情况。
谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。