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!