[Solved] Unable to get data from Sql Server 2005 (connection time out exception)


As the jTDS FAQ states, the URL must be in the form

jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]

Infering from your connection:

  • mindmill is the name of your_computer/server where Sql Server 2005 is installed.
  • 1433 is the (default) port to connect with Sql Server 2005.
  • employ is the database name.
  • mahesh is your user and password to connect to server.

From here on, you must set other sql connection parameters. I’ll post you my code:

package edu.jtds.main;

import java.sql.*;

public class SqlServerConnTest {

    Connection conn;

    public void connect() {
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            String dbName = "TestDB";
            String user = "cajeroUpz";
            String password = "cajero";
            //the name of my SQL SERVER 2005 instance
            String SqlServerInstance = "instance=SQL2005";
            String url = "jdbc:jtds:sqlserver://localhost:1433";
            url = url + "https://stackoverflow.com/" + dbName;
            url = url + ";" + SqlServerInstance;
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() {
        return this.conn;
    }

    public static void main(String[] args) {
        SqlServerConnTest oSqlServerConnTest = new SqlServerConnTest();
        oSqlServerConnTest.connect();
        String sql = "SELECT * FROM TEST_TABLE";
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = oSqlServerConnTest.getConnection(); 
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            while(rs.next()) {
                System.out.println(String.format("%d %s", 
                    rs.getInt(1), rs.getString(2)));
            }
            rs.close();
            stat.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The output of my program:

1 hello world
2 goodbye!

The lessons here:

  1. From SQL Server 2005 and on, you must set the name of the instance (like the example above).
  2. In SQL Server, you must check that your TCP/IP protocol is enabled and the communication port is 1433 (this last is setted by default, just check it). You can enable/disable using SQL Server Configuration Management in the SQL Server 2005 Configuration Tools.

Checking TCP/IP protocol for SQL Server 2005

Any other problems, just tell me.

EDIT:

The best case would be if you have already tried this connection with your pc as a Server, I mean your pc must have Sql Server 2005 installed, NetBeans installed and the project already setted up and running (as a proof of concept to connect the database).

Even if you haven’t do the step before, there is a set of questions you should have answered before trying to connect a remote server:

  1. Have you checked communication between your pc and the host? In your case, prompt a Command Line (Start / Run… type ‘cmd’ and Enter) and enter the command “ping mindmill”, check the hostname of your server.
  2. Have you installed Sql Server Management Studio in your pc and connected to your server? does your server allow remote connections? Help 1
  3. Does the user have enough privileges to connect the database? Help 2

Let me know any more issues after you have answered this questions before.

4

solved Unable to get data from Sql Server 2005 (connection time out exception)