4

I am trying to connect to SQL Server from eclipse and i get the following error. I mention that i verified and the SQL Server Browser is running on the host and i have no firewall active.

com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host 
LAURA-PC, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException:
Receive timed out". Verify the server and instance names and check that no firewall
is blocking UDP traffic to port 1434.  For SQL Server 2005 or later, verify that 
the SQL Server Browser Service is running on the host.

This is the code i've written:

   import java.sql.*;


public class ConnectSQLServer {

    public void connect(String url){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Connection connection = DriverManager.getConnection(url);
            System.out.println("Connected");
            Statement statement = connection.createStatement();
            String query = "select * from Vehicle where Mileage < 50000";
            ResultSet rs = statement.executeQuery(query);
            while(rs.next()){
                System.out.println(rs.getString(1));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        ConnectSQLServer connServer = new ConnectSQLServer();
        String url = "jdbc:sqlserver://LAURA-PC\\SQLEXPRESS;databaseName=Register;integratedSecurity=true";
        connServer.connect(url);

    }

}
1
  • I turned off windows firewall and still not working Commented Dec 2, 2013 at 21:42

2 Answers 2

3

First thing before DB programming. Test each "step". Before executing any query or even writing any other code, please check if you can connect to the DB. I assume that you are connecting to the local db. For steps on making your connection URL, see - http://technet.microsoft.com/en-us/library/ms378428.aspx

Try changing your URL to - jdbc:sqlserver://localhost;user=Mine;password=Secret;databaseName=MyDB. I tried this code and it worked.

import java.sql.*;


public class ConnectSQLServer {

    public void connect(String url){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            Connection connection = DriverManager.getConnection(url);
            System.out.println("Connected");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        ConnectSQLServer connServer = new ConnectSQLServer();
        String url = "jdbc:sqlserver://localhost;user=Mine;password=Secret;databaseName=AdventureWorks";
        connServer.connect(url);

    }

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

9 Comments

Note - You have to add the jdbc driver for SQLServer to your build path in eclipse to be able to connect to a db.
I added jdbc driver from the beginning. Running the code you provided, i get this error now: 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."
@laura - In windows services, is your SQL server service running ?
Yes. I've checked and the status is Started.
@laura - I tried to reproduce this error. I stopped SQL server service in my own computer and then ran the code which I suggested. The error I got is - 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.".
|
0

Just ENABLE/START SQL Server Browser and copy sqljdbc_auth.dll file to Windows->System32 by extracting this file from ( Microsoft jdbc driver 9.2 for sql server )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.