import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Test02 { public static void main(String[] args) { Statement stmt = null; Connection conn = null; try { //1. DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //2. String url ="jdbc:mysql://127.0.0.1:3306/linf"; String user = "root"; String password = "89399129"; conn = DriverManager.getConnection(url, user, password); System.out.println("数据库连接对象" + conn); //3. stmt = conn.createStatement(); //4. String sql = "insert from t_student where sex = 3"; int count = stmt.executeUpdate(sql); System.out.println(count == 1 ? "保存成功" : "保存失败"); //5. } catch (SQLException e) { e.printStackTrace(); } finally { //6. try { if (stmt != null) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
结果:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Test02.main(Test02.java:17)
Process finished with exit code 0
错误信息告诉你了呀,com.mysql.jdbc.Driver驱动程序已经不建议用了,账户或密码错误
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
以后看到这个,就只有一种错误,数据库连接密码不对,不用想其它的。
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632