用JDBC和MySQL解決“通信鏈路故障”我試圖連接到本地MySQL服務(wù)器,但是我一直收到一個(gè)錯(cuò)誤。這是密碼。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) {}
}
}
}}還有錯(cuò)誤: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)我已經(jīng)設(shè)置了類路徑,確保我的.cnf將跳轉(zhuǎn)網(wǎng)絡(luò)選項(xiàng)注釋掉。Java版本為1.2.0_26(64位)MySQL 5.5.14 MySQL連接器5.1.17我確保用戶能夠訪問我的數(shù)據(jù)庫。
3 回答

慕碼人8056858
TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

料青山看我應(yīng)如是
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
bind-address
bind-address = 192.168.123.456
GRANT ALL PRIVILEGES ON dbname.* to username@'%' IDENTIFIED BY 'password';
添加回答
舉報(bào)
0/150
提交
取消