错误原因如下:
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449) at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160) at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81) at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473) at vo.NewsManager.main(NewsManager.java:17) Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP 连接。”。 at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1033) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:154) at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
在命令行中输入:telnet localhost 1433,连接不上:
检查下配置:服务和连接的外围应用配置器
已经选中了“同时使用TCP/IP和named pipes(B)”
下面再检查SQL Server Configuration Manager
实例名为“SQLEXPRESS”下的TCP/IP已经开启了:
右击“TCP/IP”选择“属性”(或双击“TCP/IP”),选择“IP地址”选项卡,最下面有个“IPALL”:
问题就在这里: 我没有设置TCP端口,改过之后如下图:
再看看“SQL Native Client配置”:
双击“TCP/IP”:
一切正确。现在重启服务:
右击“SQL Server (SQLEXPRESS)”,选择重新启动。
再回到命令行下,键入:telnet localhost 1433
显示黑屏,如上图,说明已经成功了!
最后在Eclipse下用JDBC连接,可以正确连接到SQL Server 2005啦。
转自:http://blog.csdn.net/njnu_mjn/article/details/6668966