0

I'm trying to connect to a local sql server using Java and Microsoft SQL Server. I'm using sql server auth.

Here's the code:

import java.sql.*;


public class Main {
    public static void main(String[] args)  {

        String userName ="testlogin2";
        String password ="pass";

        String url ="jdbc:sqlserver://localhost/LabNoteMap;integratedSecurity=true;"; //LabNoteMap beeing target db
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection(url, userName, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("end");
    }
}

and the stacktrace:

"C:\Program Files\Java\jdk-9\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=63510:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Deus\IdeaProjects\testMYSQL\out\production\testMYSQL;C:\Users\Deus\Desktop\jdbc\sqljdbc_6.0\enu\jre8\sqljdbc42.jar Main
end
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/LabNoteMap, port 1433 has failed. Error: "localhost/LabNoteMap. 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:191)
    at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2369)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:551)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1963)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:678)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
    at Main.main(Main.java:13)

Process finished with exit code 0

also some ss:

enter image description here

enter image description here

Also note that in Sql server configuration, the tcp/ip is enabled and the port is set to 1433.

I guess could be a firewall? but not sure how to check for it, or maybe something else?

2
  • check your firewall to see if there is some logging about what application tried to connect to your db Commented Dec 27, 2017 at 14:16
  • Your URL is invalid, please check Building the Connection URL of the SQL Server JDBC driver documentation on the Microsoft site. You've made the mistake of assuming that all JDBC drivers follow the same structure for their URL, they don't. Commented Dec 27, 2017 at 14:45

2 Answers 2

2

Your connection string is wrong. You should change it

String url ="jdbc:sqlserver://localhost/LabNoteMap;integratedSecurity=true;";

to

String url ="jdbc:sqlserver://localhost;databaseName=LabNoteMap;integratedSecurity=true;";

You are not targeting the db which you want to establish connection.

Sign up to request clarification or add additional context in comments.

Comments

0
private static String url="jdbc:sqlserver://localhost:1433;DatabaseName = students";

I'm using msbase.jar and mssqlserver.jar and msutil.jar, maybe you are using the wrong jar or your url is wrong (DatabaseName = ?)

1 Comment

It sounds like you are using an ancient version of the SQL Server driver (the SQL Server 2000 JDBC driver), in recent drivers, you only need mssql-jdbc-6.2.2.jre8.jar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.