用JDBC和MySQL解决“通信链路故障”我试图连接到本地MySQL服务器,但是我一直收到一个错误。这是密码。public class Connect {
public static void main(String[] args) {
Connection conn = null;
try {
String userName = "myUsername";
String password = "myPassword";
String url = "jdbc:mysql://localhost:3306/myDatabaseName";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Database connection established");
} catch (Exception e) {
System.err.println("Cannot connect to database server");
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
System.out.println("Database Connection Terminated");
} catch (Exception e) {}
}
}
}}还有错误:Cannot connect to database serverCommunications link failureThe last packet sent successfully to the server was 0
milliseconds ago.
The driver has not received any packets from the server.com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications
link
failureThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets
from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)我已经设置了类路径,确保我的.cnf将跳转网络选项注释掉。Java版本为1.2.0_26(64位)MySQL 5.5.14 MySQL连接器5.1.17我确保用户能够访问我的数据库。
3 回答
慕码人8056858
TA贡献1803条经验 获得超6个赞
料青山看我应如是
TA贡献1772条经验 获得超8个赞
bind-address
bind-address = 192.168.123.456
GRANT ALL PRIVILEGES ON dbname.* to username@'%' IDENTIFIED BY 'password';
添加回答
举报
0/150
提交
取消