0

I'm running into some issues trying to connect to a remote SQL Server 2008 Express instance from Java. Here are the specifics:

  • DB Server: Windows 7 running SQL Server 2008 Express
  • Client: Mac OS X (10.6.8) running Java (1.6.0) using the JDBC4 drivers

Here's the code:

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

    public abstract class SQLDatabase {

        protected Connection c;

        protected void establishConnection() {
            try {
                String url = "jdbc:sqlserver://MYSERVERNAME\\SQLEXPRESS;DatabaseName=MyDatabase;user=sa;password=pwd";
                c = DriverManager.getConnection(url);
            } catch (SQLException e) {
                System.err.println("SQLException: " + e.getMessage());
            }
        }
    }

When it hits the "DriverManager.getConnection" line, it throws an exception:

The connection to the host MYSERVERNAME, named instance sqlexpress has failed. Error: "java.net.UnknownHostException: MYSERVERNAME". Verify the server and instance names, check that no firewall is blocking UDP traffic to port 1434, and for SQL Server 2005 or later verify that the SQL Server Browser Service is running on the host.

After an extensive Google search, here are all the related issues I've resolved and things I've tried:

SQL Server (on the Windows 7 DB server)

  • Enabled mixed authentication mode on the SQL instance (http://www.linglom.com/2009/03/28/enable-remote-connection-on-sql-server-2008-express/)
  • Ensured that the SQL browser service was enabled (http://blogs.msdn.com/b/bethmassi/archive/2008/09/17/enabling-remote-sql-express-2008-network-connections-on-vista.aspx)
  • Enabled TCP/IP support
  • Explicitly set the TCP/IP port to 1433
  • Restarted the SQL Server service
  • Restarted the machine (just for good measure :) )

Firewall settings (on the Windows 7 DB server)

  • Added Inbound and Outbound exceptions for TCP on port 1433 (http://www.sevenforums.com/system-security/58817-remote-access-sql-server-express-2008-windows-7-a.html)
  • Added Inbound and Outbound exceptions for UDP on port 1434
  • Added Inbound and Outbound exceptions for sqlservr.exe
  • Added Inbound and Outbound exceptions for sqlbrowser.exe

JRE versions (on the OS X Java server)

  • Tried 1.4, 1.5, and 1.6.0, all with the same result (http://stackoverflow.com/questions/7841411/driver-getconnection-hangs-using-sqlserver-driver-and-java-1-6-0-29)

Connection URL (in the Java code)

  • Tried jdbc:sqlserver://MYSERVERNAME/SQLEXPRESS;DatabaseName=MyDatabase;user=sa;password=pwd
  • Tried jdbc:sqlserver://MYSERVERNAME:1433;DatabaseName=MyDatabase;user=sa;password=pwd
  • Tried jdbc:sqlserver://MYSERVERNAME:1434;DatabaseName=MyDatabase;user=sa;password=pwd
  • Tried jdbc:sqlserver://MYSERVERNAME;DatabaseName=MyDatabase;user=sa;password=pwd

At this point, I am simply stumped. Is there something I'm missing, perhaps related to running the JVM on a Mac? I'd be eternally grateful for any insights!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mike Monteiro
  • 1,427
  • 1
  • 14
  • 21
  • Did you verify SQL Browser service running or not? Try ping on the port from other machine and see. – kosa Jan 16 '12 at 05:10
  • Hmm, good idea. When I run "telnet 192.168.1.114 1433" from the command line, I connect with no problem. But when I run "telnet 192.168.1.114 1434" it can't connect. Does this mean 1434 isn't open somehow? – Mike Monteiro Jan 16 '12 at 05:45
  • Yes, I guess you are correct. It is not connected on 1434 and port 1434 is not open. – kosa Jan 16 '12 at 05:47
  • So I looked into this, and it looks like telnet only works with TCP protocol. By definition, telnet would never "connect" with a UDP port because UDP is connectionless. Instead, I used netcat to test whether port 1434 is open on the remote machine, and it appears to be working ("nc -vvzu 192.168.1.114 1434" returns "1434 port [udp/ms-sql-m] succeeded!"). Additionally, I temporarily disabled the entire firewall on the DB server just as a check, and the Java code continued to error out. So that would appear to rule out the firewall / port 1434 being the problem. – Mike Monteiro Jan 19 '12 at 00:17
  • I don't want to hijack, but my situation is so similar that I'll jump in. I've spent more than a week trying to fix it. Just about to look into file / directory sharing. My app works on Vista where I first built it. I've systematically checked through the Windows 7 installation to see that I did everything as before. SQL Server Management Studio can hook up to SQL Server and I but I can only connect SQLMS from another machine on the LAN if I use the local ip address in the login. I'm still trying to find good error messages to help. I just know my app doesn't have a connection. – Roger F. Gay Feb 07 '13 at 09:40

0 Answers0