jdbc连接数据库失败,com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。
2015-09-09 11:28
911 查看
创建数据库连接失败!
com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.mingrisoft.dao.GetConnection.getCon(GetConnection.java:21)
at com.mingrisoft.dao.UserDao.getUser(UserDao.java:11)
at com.mingrisoft.main.Enter$2.actionPerformed(Enter.java:111)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
源代码是:
安装SQL Server 2005后。在目录下找到SQL Server Configuration Manager这个工具,然后在左边的目录中找到SQL Server 2005网络配置下的MSSQLSERVER,点击后右侧列出了所有协议,找到TCP/IP,发现是禁用状态,启用后再重启MSSQLSERVER服务即可(此步骤必须!),此项可在该工具下的SQL
Server 2005服务中找到,或在windows服务中找到。
com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.mingrisoft.dao.GetConnection.getCon(GetConnection.java:21)
at com.mingrisoft.dao.UserDao.getUser(UserDao.java:11)
at com.mingrisoft.main.Enter$2.actionPerformed(Enter.java:111)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
源代码是:
public class GetConnection { private Connection con; //定义数据库连接类对象 private PreparedStatement pstm; private String user="sa"; //连接数据库用户名 private String password="sa"; //连接数据库密码 private String className="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //数据库驱动 private String url="jdbc:sqlserver://localhost:1433;DatabaseName=db_supermarket"; //连接数据库的URL public GetConnection(){ try{ Class.forName(className); }catch(ClassNotFoundException e){ System.out.println("加载数据库驱动失败!"); e.printStackTrace(); } } /**创建数据库连接*/ public Connection getCon(){ try { con=DriverManager.getConnection(url,user,password); //获取数据库连接 } catch (SQLException e) { System.out.println("创建数据库连接失败!"); con=null; e.printStackTrace(); } return con; //返回数据库连接对象 }解决方法:
安装SQL Server 2005后。在目录下找到SQL Server Configuration Manager这个工具,然后在左边的目录中找到SQL Server 2005网络配置下的MSSQLSERVER,点击后右侧列出了所有协议,找到TCP/IP,发现是禁用状态,启用后再重启MSSQLSERVER服务即可(此步骤必须!),此项可在该工具下的SQL
Server 2005服务中找到,或在windows服务中找到。
相关文章推荐
- TCP连接建立与断开
- 网络钓鱼,你会不会上钩?
- 网络IO模型
- Meta http-equiv属性详解(转)
- VMWare使用host-only网络连接总结
- TCP之深入浅出send和recv
- HTTP Error 12057 - SSL Revocation(解决geen的问题)
- 新树莓派2 无屏幕开机
- POJ 3281 网络流 拆点 Dining
- Linux内核 TCP/IP、Socket参数调优
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- TCP、UDP、IP 协议分析
- TCP协议Demo
- 高性能网络编程7--tcp连接的内存使用
- python 网络爬虫开源框架scrapy
- Cacti - SOCKSTAT - tcpInuse
- 计算机网络学习——04
- Android主线程不能访问网络异常解决办法
- 适合新手的网络编程,用socket传文件(本例传的是jpg图片) 用到sendfile函数
- HTTP/2 与 WEB 性能优化(三)