我需要使用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://dev59.com/lGcs5IYBdhLWcg3w3HkG#12524566 我在Libraries/Compile中添加了jdbc4.jar SQL Server Browser窗口服务正在运行。
在SQL Server Network Configuration中,我选择了TCP/IP属性上的Enebled。
我将TCP地址设置为1433。
在Run,VM Options中,我输入了-Djava.library.path=my path to sqljdbc_auth.dll,并在JDK中复制了bin sqljdbc_auth.dll。
我应该怎么做?
编辑: 当在cmd中写入telnet localhost 1433时,我收到“无法连接到主机,在端口1433上”。
forName()
。 - Sotirios Delimanolis