sqlserver2005获取连接失败:com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败,解决办法:
安装完数据库后设置ICP/IP协议启动,具体如下:
(1)打开SQL Server Configuration Manager
(2)转到SQL Server 2005 Network Configuration->Protocols for MAHAIBO
(3)将TCP/IP设置为Enabled(启用)
(4)双击TCP/IP项,转到IP Addresses页
(5)IP All中设置TCP Port为1433
(6)重新启动服务
即可。
- package org.test.db;
- import java.sql.Connection;
- import java.sql.DriverManager;
- public class Conn {
- private static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=frame";
- private static String username = "sa";
- private static String password = "123456";
- private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- public static Connection getConnection(){
- Connection conn = null;
- try{
- Class.forName(driver);
- conn = DriverManager.getConnection(url,username,password);
- }catch(Exception e){
- e.printStackTrace();
- }
- return conn;
- }
- }