记录每次遇到的bug与异常
?
jdbc的下载地址与DBMS的配制方法见下文http://www.360doc.com/content/14/0806/02/15319145_399731507.shtml
?
我在测试链接的时候遇到了如下异常:
数据库驱动加载成功...
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败。 ClientConnectionId:ea4142b1-ae5c-4618-b6a5-c827b5205235
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test.TestSQLConnect.main(TestSQLConnect.java:16)
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败。 ClientConnectionId:ea4142b1-ae5c-4618-b6a5-c827b5205235
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test.TestSQLConnect.main(TestSQLConnect.java:16)
?
连接代码如下
public class TestSQLConnect { public static void main(String[] args){ String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:sqlserver://localhost:1433;databaseName=TestData"; String userName = "sa"; String userPwd = ""; try { Class.forName(driverName); System.out.println("数据库驱动加载成功..."); Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("连接数据库成功"); }catch (Exception e) { e.printStackTrace(); System.out.print("连接失败"); } }}
如果用户名和密码没错的情况下,造成该异常的原因主要是TCP/IP端口的问题:
?打开SQL?SERVER 配置工具 中的SQL SERVER配置管理器。左边窗口扩展SQL?SERVER 网络配置,?选择SQLEXPRESS?的协议(你的名字不一定是SQLEXPRESS),?右边窗口中双击TCP/IP,?弹出窗口中顶部TAB点"IP?地址",?看IPALL, 里面的TCP端口号才是你的URL端口号,可能有的朋友1433默认端口号被占用了,因此URL端口号被篡改了
?
?上图是我今天装的SQL SERVER2012,由于1433端口号被MSSQLSERVER协议给占用了,因此我的SQLEXPRESS中TCP端口号是其他端口,因此导致连接错误。
?
只需要将EXPRESS中TCP/IP端口号修改一下,并重启数据库服务,并在程序URL中填上相应端口号即可。
?
希望以后有人遇到类似的问题能够看到