我需要使用jdbc 4.0从java连接到Sql Server 2008。我有一个非常简单的代码:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost;" +
"integratedSecurity=true;";
Connection con = DriverManager.getConnection(connectionUrl);
但是我有这个错误:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. 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.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
...
我遵循这个答案:https://stackoverflow.com/a/12524566/1554397
我在Libraries/Compile中添加了jdbc4.jar
SQL Server Browser windows service正在运行
在"SQL Server Network Configuration"中选择"enabled on TCP/IP properties"。
I设置TCP地址为1433
在Run中,VM Options i put -Djava.library。Path =我到sqljdbc_auth.dll的路径在JDk中拷贝到bin sqljdbc_auth.dll。
我该怎么办?
编辑:当写入cmd telnet localhost 1433时,我得到'Could not open connection to host,on port 1433'
如果使用windows身份验证,您可以这样做:
String url = "jdbc:sqlserver://MYPC\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
,然后将路径添加到sqljdbc_auth.dll中作为VM参数(在构建路径中需要sqljdbc4.jar)。
看看这里的一个简短的分步指南,展示如何从Java连接到SQL Server如果你需要更多的细节。希望能有所帮助!