jdbc连接数据库失败,com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。
生活随笔
收集整理的這篇文章主要介紹了
jdbc连接数据库失败,com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
創(chuàng)建數(shù)據(jù)庫連接失敗!
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)
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; //定義數(shù)據(jù)庫連接類對象 private PreparedStatement pstm; private String user="sa"; //連接數(shù)據(jù)庫用戶名 private String password="sa"; //連接數(shù)據(jù)庫密碼 private String className="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //數(shù)據(jù)庫驅(qū)動 private String url="jdbc:sqlserver://localhost:1433;DatabaseName=db_supermarket"; //連接數(shù)據(jù)庫的URL public GetConnection(){try{Class.forName(className);}catch(ClassNotFoundException e){System.out.println("加載數(shù)據(jù)庫驅(qū)動失敗!");e.printStackTrace();} } /**創(chuàng)建數(shù)據(jù)庫連接*/ public Connection getCon(){try {con=DriverManager.getConnection(url,user,password); //獲取數(shù)據(jù)庫連接} catch (SQLException e) {System.out.println("創(chuàng)建數(shù)據(jù)庫連接失敗!");con=null;e.printStackTrace();}return con; //返回數(shù)據(jù)庫連接對象 }解決方法:
安裝SQL Server 2005后。在目錄下找到SQL Server Configuration Manager這個工具,然后在左邊的目錄中找到SQL Server 2005網(wǎng)絡(luò)配置下的MSSQLSERVER,點擊后右側(cè)列出了所有協(xié)議,找到TCP/IP,發(fā)現(xiàn)是禁用狀態(tài),啟用后再重啟MSSQLSERVER服務(wù)即可(此步驟必須!),此項可在該工具下的SQL Server 2005服務(wù)中找到,或在windows服務(wù)中找到。
總結(jié)
以上是生活随笔為你收集整理的jdbc连接数据库失败,com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts2.x和Struts1.x的
- 下一篇: 图片按钮ImageButton