title: Connecting to a remote SQL Server Express 2008 instance on Windows 7 from Java using JDBC (on Mac OS X)
body: <p>I'm running into some issues trying to connect to a remote SQL Server 2008 Express instance from Java.  Here are the specifics:</p>
  
  <ul>
  <li>DB Server: Windows 7 running SQL Server 2008 Express</li>
  <li>Client: Mac OS X (10.6.8) running Java (1.6.0) using the JDBC4 drivers</li>
  </ul>
  
  <p>Here's the code:</p>
  
  <pre><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());
              }
          }
      }
  </code></pre>
  
  <p>When it hits the "DriverManager.getConnection" line, it throws an exception:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>After an extensive Google search, here are all the related issues I've resolved and things I've tried:</p>
  
  <p>SQL Server (on the Windows 7 DB server)</p>
  
  <ul>
  <li>Enabled mixed authentication mode on the SQL instance (http://www.linglom.com/2009/03/28/enable-remote-connection-on-sql-server-2008-express/)</li>
  <li>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)</li>
  <li>Enabled TCP/IP support</li>
  <li>Explicitly set the TCP/IP port to 1433</li>
  <li>Restarted the SQL Server service</li>
  <li>Restarted the machine (just for good measure :) )</li>
  </ul>
  
  <p>Firewall settings (on the Windows 7 DB server)</p>
  
  <ul>
  <li>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)</li>
  <li>Added Inbound and Outbound exceptions for UDP on port 1434</li>
  <li>Added Inbound and Outbound exceptions for sqlservr.exe</li>
  <li>Added Inbound and Outbound exceptions for sqlbrowser.exe</li>
  </ul>
  
  <p>JRE versions (on the OS X Java server)</p>
  
  <ul>
  <li>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)</li>
  </ul>
  
  <p>Connection URL (in the Java code)</p>
  
  <ul>
  <li>Tried jdbc:sqlserver://MYSERVERNAME/SQLEXPRESS;DatabaseName=MyDatabase;user=sa;password=pwd</li>
  <li>Tried jdbc:sqlserver://MYSERVERNAME:1433;DatabaseName=MyDatabase;user=sa;password=pwd</li>
  <li>Tried jdbc:sqlserver://MYSERVERNAME:1434;DatabaseName=MyDatabase;user=sa;password=pwd</li>
  <li>Tried jdbc:sqlserver://MYSERVERNAME;DatabaseName=MyDatabase;user=sa;password=pwd</li>
  </ul>
  
  <p>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!</p>