我正在使用mysql与我的数据库建立连接。代码工作正常,但如果我输入错误的用户名/传递/主机/数据库,我可以得到一个日志消息,该部分是错误的?这是代码:
private void connectDatabase()
{
try {
connection = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/"
+ database + "?autoReconnect=true", username, password);
System.out.println("[MySQL] The connection to MySQL is made!");
} catch (SQLException e) {
System.out.println("[MySQL] The connection to MySQL couldn't be made! reason: "
+ e.getMessage());
}
}
Ex:我输入了错误的密码,我收到了这个错误日志:
[MySQL] The connection to MySQL couldn't be made! reason: Wrong password.
这可能吗?
答案 0 :(得分:0)
对于MySQL,您将获得一个SQLExcpetion,如下所示:
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113) ~[mysql-connector-java-5.1.18.jar:na]
...
SQLException将具有SQLState = 28000和vendorCode = 1045