0
votes

I'm having problems connecting to a SQL Server database I created in my pc for a small app. The server's called 'INSPIRONN5110-D'. I use Windows Authentication to login to the server. Already downloaded the driver from microsoft and copied its content to the program files folder and added the path (C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar) to the path and classpath variable (not sure which one to use) and still get this error.

Creating Connection.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host INSPIRONN5110-D, port 1433 has failed. Error: "connect timed out. 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.".
    .....

Here's the method where I try to connect to the data base.

private Connection connectToDB() {

        String connectionUrl = "jdbc:sqlserver://INSPIRONN5110-D; databaseName=MemoryHelp;";
        Connection con = null;
        try{
//          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("Creating Connection.");
            con = DriverManager.getConnection(connectionUrl);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
//      } catch (ClassNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
        } finally{

            System.out.println("Connection created.");

        }


        return con;
    }

Tried changing the connection url to "jdbc:sqlserver://localhost; databaseName=MemoryHelp;"

Getting this error now:

Creating Connection.
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.".
    ....

Finally solved it. Had to go to SQL Server Configuration Manager and enable tcp/ip connections to the server, then copy the sqljdbc_auth.dll file from the driver's folder to the system32 folder and using the following connection URL could get it to work:

String connectionUrl = "jdbc:sqlserver://localhost;databaseName=MemoryHelp;integratedSecurity=true";
1
To figure out if this is a JDBC problem or a general network problem, try connecting to the server via SQL Server Management Studio, using the exact same hostname, port, username, and password. If that fails, then your problem has nothing to do with JDBC. - Brandon
As mentioned above I connect to the server via SQL Server Management Studio using my windows account(I'm using windows 8.1, if that helps). I would like to to the same here. I have no problems in Management Studio. - DidierFuentes

1 Answers

0
votes

you didnt provide the ip or hostname of the server , also the port. You need to provide those in order to connect to the server .

In your case if the server is on the same computer as the app .. then it should be : jdbc:sqlserver://localhost:3306

port 3306 is just the default port i assumed you are using , that depends on w.e port you are hosting server on .