6

I need to connect to Sql Server 2008 from java using jdbc 4.0. I have a very simple code:

 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
 String connectionUrl = "jdbc:sqlserver://localhost;" +
    "integratedSecurity=true;";
 Connection con = DriverManager.getConnection(connectionUrl);

But i have this error:

    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)
...

I follow this answer:https://stackoverflow.com/a/12524566/1554397

I added jdbc4.jar in Libraries/Compile

SQL Server Browser windows service is running.

In SQL Server Network Configuration i selected Enebled on TCP/IP properties.

I Set TCP Address to 1433.

On Run,VM Options i put -Djava.library.path=my path to sqljdbc_auth.dll And copy in JDk ,in bin sqljdbc_auth.dll.

What should I do?

EDIT: When write in cmd telnet localhost 1433 i get 'Could not open connection to the host,on port 1433'

Community
  • 1
  • 1
Blocked
  • 340
  • 2
  • 5
  • 20
  • You don't need `forName()` with jdbc 4. – Sotirios Delimanolis May 21 '13 at 18:00
  • Ok,so?I get the same error... – Blocked May 21 '13 at 18:07
  • No.It must go with the first string."Connect to the default database on the local computer by using integrated authentication: jdbc:sqlserver://localhost;integratedSecurity=true;" from http://msdn.microsoft.com/en-us/library/ms378428.aspx – Blocked May 21 '13 at 18:14
  • The only thing I can suggest is restarting the SQL server and trying again. The error clearly states that no server is listening on the port you put. – Sotirios Delimanolis May 21 '13 at 18:18
  • How do i restart sql server?I close Sql Server Management and open again?I did that already. – Blocked May 21 '13 at 18:26
  • If that is just a UI management tool, no that won't be enough. There should be a command to restart it. Or check in your running processes, kill it, and restart it with its executable. – Sotirios Delimanolis May 21 '13 at 18:28
  • Meanwhile I restarted the computer and now work.I need to select your answer and if you want to write, in your answer, information about 'restart SQL Server'.This is answer http://social.msdn.microsoft.com/Forums/en-US/sqlgetstarted/thread/78298175-1ae5-4f9a-b61d-b23d585365e3 – Blocked May 21 '13 at 18:40
  • :) I'm glad you got it to work. Honestly, you're better off putting up your own correct answer of how you got it to work since you have more details than I do. You can also accept your own answer after 2 days. – Sotirios Delimanolis May 21 '13 at 18:52

1 Answers1

4

If using Window authentication you can do something like:

String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);

and then add the path to sqljdbc_auth.dll as a VM argument (you need sqljdbc4.jar in the build path).

Take a look here for a short step-by-step guide showing how to connect to SQL Server from Java should you need more details. Hope it helps!

Thusi
  • 929
  • 7
  • 5