Connecting from Java to SQL server using JDBC (Windows authentication mode)
I need to use JDBC 4.0 to connect from Java to SQL Server 2008.0
Class.forName("com.microsoft.sqlserver.jdbc.sqlServerDriver"); String connectionUrl = "jdbc:sqlserver://localhost;" + "integratedSecurity=true;"; Connection con = DriverManager.getConnection(connectionUrl);
But I made this mistake:
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 JDBC 4.0 to libraries / compile jar
The SQL server browser windows service is running
In the SQL server network configuration, I selected the enabled on TCP / IP attribute
I set the TCP address to 1433
When running, VM option I put - DJava library. Path = my path sqljdbc_ auth. DLL and sqljdbc in Bin_ auth. DLL
What should I do?
Edit: when writing CMD telnet localhost 1433, I get 'unable to open the connection with the host, on port 1433'
Solution
If you use window authentication, you can do the following:
String url = "jdbc:sqlserver://MYPC\\sqlEXPRESS;databaseName=MYDB;integratedSecurity=true"; Class.forName("com.microsoft.sqlserver.jdbc.sqlServerDriver"); Connection conn = DriverManager.getConnection(url);
Then SQL JDBC_ auth. Add the path of DLL as VM parameter (sqljdbc 4.jar is required in the build path)
Check here for a short step-by-step guide on how to connect to SQL server from Java when more details are needed Hope to help you!