com.microsoft.sqlserver.jdbc.SQLServerException: 到主机的TCP/IP 连接失败。java.net.ConnectException: Connection refused: connect
这种情况一般下,自己写个TEST类测试下配置参数,连接数据库看看,看看是否能连接上去。
1、检查TCP/IP。
2、配置文件的URL,端口是否为1433等。
public class SqlServerDBTest {
private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url = "jdbc:sqlserver://localhost:1433";
private Connection conn = null;
private String username = "sa";
private String password = "sa";
private static ResultSet rs=null;
private static PreparedStatement ps=null;
public SqlServerDBTest(){
try{
Class.forName(driver);
}catch(Exception e){
e.printStackTrace();
}
try{
conn = DriverManager.getConnection(url,username,password);
}catch(Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
return new SqlServerDBTest().conn;
}
public static void main(String[] args){
System.out.println("----start----");
SqlServerDBTest test = new SqlServerDBTest();
Connection conn = test.conn;
String r1 ="";
if(conn != null){
try {
ps =conn.prepareStatement("select 1 from dual");
rs = ps.executeQuery();
if(rs.next()){
r1 = rs.getString(1);
}
rs.close();
ps.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("----11----" + r1);
try {
System.out.println("----end----" + conn.getMetaData().getDatabaseProductVersion());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}