2

I am trying to create a connection between some Java and a SQL Server using a JDBC driver. I have created a Connection class which should form the connection to a server which is on my computer with example IP address 'BHX'

Here is the Connection class

public class Connection {

public static void main(String[] args) throws Exception {

    try {
        String databaseDriver = "net.sourceforge.jtds.jdbc.Driver";
        Class.forName(databaseDriver);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {               
        String url = "jdbc:jtds:sqlserver://BHX:1433/Forecast;instance=SQLEXPRESS";
        java.sql.Connection con =  DriverManager.getConnection(url);
        System.out.println("Connection created");
        con.close();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}
}

I'm wondering whether there is anything wrong with the url String, as when I run this code I get the following error:

java.sql.SQLException: Network error IOException: Connection refused: connect
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:417)
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Connection.main(Connection.java:56)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:311)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:261)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:318)
... 5 more

I have seen similar questions (like this here) but its looks as though I have followed all the possible solutions. I have checked that the TCP/IP are enabled in the configuration manager and also that the port number is 1433.

I've tried running this with Firewalls disabled and still get the same error.

telnet BHX 1433 and I am getting the following message '...Could not open connection to the host, on port 1433: Connect failed'

Community
  • 1
  • 1
Nosheen
  • 458
  • 1
  • 5
  • 12
  • Are you sure firewall did not block 1433? Did you checked? – Ami Nov 06 '12 at 08:31
  • Are you able to telnet into that host/port? jTDS does work perfectly with SQL Server so it should be the last one to blame. – bobah Nov 06 '12 at 08:32
  • Is the DB by any chance bound to `localhost` rather than `0.0.0.0` or a class-A/B/C private IP? – Anders R. Bystrup Nov 06 '12 at 08:36
  • I have tried to use the telnet BHX 1433 and I am getting the following message '...Could not open connection to the host, on port 1433: Connect failed' – Nosheen Nov 06 '12 at 12:32
  • I'm not sure if the 1st link Thanga has posted will solve the connection problem with telnet. – Nosheen Nov 06 '12 at 12:34
  • @AndersRostgaardBystrup As I am new to this I'm not sure what you mean by **A/B/C private IP** Can you elaborate please? – Nosheen Nov 06 '12 at 12:37
  • If the `telnet` trick doesn't work, then there is nothing listening on that host/port combination. You can use `netstat -anb` (Windows) to get a view of what processes are listening where. – Anders R. Bystrup Nov 06 '12 at 12:46
  • can u please try with localhost instead of BHX ? @Nosheen – thar45 Nov 06 '12 at 12:46
  • @AndersRostgaardBystrup With the `netstat -anb` command I get the following message `The requested operation requires elevation` Do you think this is to do with the permissions I have to form connections/run commands on this computer? (Sorry if the questions a little out of context) – Nosheen Nov 06 '12 at 13:21
  • @Thanga I tried the `telnet localhost` and am stil getting the same `...could not open connection to the host...Connect failed` message. – Nosheen Nov 06 '12 at 13:22
  • You will need to run the CMD shell as administrator then, if you can't, drop the `b` but then you won't see the process names. – Anders R. Bystrup Nov 06 '12 at 13:22
  • @AndersRostgaardBystrup Yes, it works when dropping the `b` only like youve said the names are not available. I'm not the administrator so will have to contact them. Thanks for showing me how to find what processes are listening. Just to clarify should it be listening to the SQL server on BHX (thats what I am understanding) – Nosheen Nov 06 '12 at 13:30
  • `netstat` shows you on what ports and IP's there is something listening or connected - you should look for `1433` and if it's not there, your SQL Server isn't listening on that port. Then refer to @Thanga's links. – Anders R. Bystrup Nov 06 '12 at 13:56
  • @AndersRostgaardBystrup According to the SQL Server configuration manager the Default Port is 1433. So that should mean my SQL server is istening on that port right? Only BHX cannot connect to that port? I would just like to clarify that what I'm understanding is right. Thanks. – Nosheen Nov 06 '12 at 14:10
  • Your task with `netstat` is to _verify_ if SQLServer is actually listening on 1433, no matter what the defaults are... – Anders R. Bystrup Nov 06 '12 at 14:12
  • So, first check that the SQLServer is listening on 1433, if yes then carry out the steps on Thangas link, if not get the number that the SQLServer is running on from the `netstat` command and add that to the URL `String url = "jdbc:jtds:sqlserver://BHX: _portNumber_ /Forecast;instance=SQLEXPRESS";` (and if need carry out the steps from the link) I'm not to familiar with the use of ports, but think I understand a little better now. – Nosheen Nov 06 '12 at 14:21

1 Answers1

-1

It will work when you add the username and password to your connection string like:

"jdbc:jtds:sqlserver://BHX:1433/Forecast;instance=SQLEXPRESS;user=XXXXX;password=XXXXX"

This should work with SQL Server authentication.

If you only use windows authentication you will also need to provide the domain like:

"jdbc:jtds:sqlserver://BHX:1433/Forecast;instance=SQLEXPRESS;domain=XXXXX;user=XXXXX;password=XXXXX"
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
Red
  • 1