创建数据库连接失败!
com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 .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 ponent.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at ponent.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at ponent.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at ponent.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 ponent.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";//连接数据库的URLpublic 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 后。在目录下找到SQL Server Configuration Manager这个工具,然后在左边的目录中找到SQL Server 网络配置下的MSSQLSERVER,点击后右侧列出了所有协议,找到TCP/IP,发现是禁用状态,启用后再重启MSSQLSERVER服务即可(此步骤必须!),此项可在该工具下的SQL Server 服务中找到,或在windows服务中找到。