2013-03-15 76 views
0

我试图连接到我的网络上的计算机上的SQL数据库。从我可以告诉TCP/IP设置和防火墙设置都可以连接正确,但我没有与我的连接字符串运气。使用jdbc.sqlserver的TCP/IP连接问题

代码:

static String dbUser = "user"; 
static String dbPass = "pass"; 
static String dbURL = "jdbc:sqlserver://SERVER1/BKUPEXEC;Instance=SQL2005;databaseName=images;"; 
static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 

try { 
     Class.forName(driverName).newInstance(); 
     con = DriverManager.getConnection(dbURL, dbUser, dbPass); 
     System.out.println("Connected with host:port/database."); 
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) { 
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
    } 

错误:

SEVERE: null 
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host TMTSERVER1, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.". 

回答

0

尝试

"jdbc:sqlserver://SERVER1/BKUPEXEC;instanceName=SQL2005;databaseName=images;" 
            ^^^^^^^^^^^^ 
+0

新的错误:com.microsoft.sqlserver.jdbc.SQLServerException:到主机TMTSERVER1/BKUPEXEC的连接,命名实例SQL2005失败。错误:“java.net.UnknownHostException:TMTSERVER1/BKUPEXEC”。 – 2013-03-15 15:53:42

+0

看看[什么原因导致错误 - java.net.UnknownHostException](http://stackoverflow.com/questions/6484275/what-c​​auses-the-error-java-net-unknownhostexception) – jlordo 2013-03-15 15:58:56

0

我最终改变连接字符串使用服务器的IP地址,而不是它的域名。还发现SQL服务器没有使用默认的端口号。

这是新工作的连接字符串:

static String dbURL = "jdbc:sqlserver://192.168.1.240:1053;databaseName=SI_images;";