1

Iam trying to access database from system to other system, below is code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class testConnection {

    /**
     * @param args
     * @throws SQLException 
     */
    public static void main(String[] args) throws SQLException {
        // TODO Auto-generated method stub
        String connectionURL = "jdbc:sqlserver://xxx.xxx.xx.xx:1433;DatabaseName=fingerprintdb;user=sa;password=test"; 
        // Change the connection string according to your db, ip, username and password 
          Connection con = null;
        try { 

            // Load the Driver class. 
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
            // If you are using any other database then load the right driver here. 

            //Create the connection using the static getConnection method 
             con = DriverManager.getConnection (connectionURL); 

            //Create a Statement class to execute the SQL statement 
            Statement stmt = con.createStatement(); 

            //Execute the SQL statement and get the results in a Resultset 
            ResultSet rs = stmt.executeQuery("select moviename, releasedate from movies"); 

            // Iterate through the ResultSet, displaying two values 
            // for each row using the getString method 

            while (rs.next()) 
                System.out.println("Name= " + rs.getString("moviename") + " Date= " + rs.getString("releasedate")); 
        } 
        catch (SQLException e) { 
            e.printStackTrace(); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
        finally { 
            // Close the connection 
            con.close(); 
        } 

    }

}

Below is exception iam getting.

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host xxx.xxx.xx.xx, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at testConnection.main(testConnection.java:26)
Exception in thread "main" java.lang.NullPointerException
    at testConnection.main(testConnection.java:48)

Iam able to connect to the database from the same syste. but now iam trying to connect remote way.

I got the above ip address from typing ipconfig in cmd prompt. Am I doing correctly, if not please correct me.

Below is log of pings to host.

Pinging xxx.xxx.xx.xx with 32 bytes of data:

Reply from xxx.xxx.xx.xx: TTL expired in transit.
Reply from xxx.xxx.xx.xx: TTL expired in transit.
Reply from xxx.xxx.xx.xx: TTL expired in transit.
Reply from xxx.xxx.xx.xx: TTL expired in transit.

Ping statistics for xxx.xxx.xx.xx:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
1
  • thanks for updating. Have you tried to open telnet connection on that port(1433)? just to check specific port is open or not? Commented Nov 8, 2012 at 7:02

2 Answers 2

1

Yes, Sometimes it Happens.. (Do the following Process where your SQLServer is installed)

You need to Go to

Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager

When it opens Go to

SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for SQLExpress 

Where you'll find the Protocol TCP/IP, if disabled then Enable it Click on TCP/IP, You'll find its properties.

In this properties Remove All the TCP Dynamic Ports and Add value of 1433 to all TCP Port and restart your

SQL Server Services > SQL Server

And Its Done...

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

3 Comments

i will try your suggestions and will let you know the status
I have done exactly what you suggested, but still iam getting same exception
have you changed all of your TCP Dynamic Ports to 1433?
0

you need to configure the remote SQL server to accept TCP/IP connection and you may require to allow remote connection in case if it not enabled.

Comments

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.