連接數據庫時出現如下錯誤提示:
SQLException: Communications link failure
The last packet sent successfully to the serverwas 0 milliseconds ago. The driver has not received any packets from theserver.
附上部分代碼段:
publicstaticConnection getConnection() throws SQLException,
java.lang.ClassNotFoundException
{
//第一步:加載MySQL的JDBC的驅動
Class.forName("com.mysql.jdbc.Driver");
//取得連接的url,能訪問MySQL數據庫的用戶名,密碼;jsj:數據庫名
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=VisitSql";
String username = "sa";
String password = "123";
//第二步:創建與MySQL數據庫的連接類的實例
Connection con = DriverManager.getConnection(url, username,password);
returncon;
}
原因分析:
本機Win7系統安裝的是SQL Server 2008,url設置爲:
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=VisitSql";
而初始是以MySQL的URL方式設置的:
Stringurl = "jdbc:mysql://localhost:3306/VisitSql";
故而會出現上述錯誤。