连接数据库时出现如下错误提示:
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";
故而会出现上述错误。