title: Java / SQL Server 2008 R2 Express connection issues
body: <p>I'm trying to connect to a SQL Server db on my local machine using the following code:</p>
  
  <pre><code>import java.sql.*;
  import com.microsoft.sqlserver.jdbc.*;
  import java.sql.*;
  
  
  public class JDBConn {
  
      //public class connectURL {
  
          public static void main(String[] args) {
              // Declare the JDBC objects.
              Connection con = null;
              Statement stmt = null;
              ResultSet rs = null;
  
              // Create a variable for the connection string.
  
              //String connectionUrl = "jdbc:sqlserver://localhost;database=optRes1;integratedSecurity=true;";
              //con = DriverManager.getConnection(connectionUrl);
  
  
  
                  try {
                      // Establish the connection.
                      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                      String connectionUrl = "jdbc:sqlserver://localhost";
                      con = DriverManager.getConnection(connectionUrl);
                      Statement s = con.createStatement();
                      ResultSet r = s.executeQuery("SELECT * FROM some_table_name");
                      while (r.next()) {
                          System.out.println(r.getString(1));
                      }
  
  
                          // Create and execute an SQL statement that returns some data.
                          String SQL = "SELECT * from [optRes1].[dbo].[unEmpDat]";
                          stmt = con.createStatement();
                          rs = stmt.executeQuery(SQL);
  
                          // Iterate through the data in the result set and display it.
                          while (rs.next()) {
                              System.out.println(rs.getString(4) + " " + rs.getString(6));
                          }
                  }
  
              // Handle any errors that may have occurred.
              catch (Exception e) {
                  e.printStackTrace();
              }
  
              finally {
                  if (rs != null) try { rs.close(); } catch(Exception e) {}
                      if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                      if (con != null) try { con.close(); } catch(Exception e) {}
              }
          }
  }
  </code></pre>
  
  <p>I'm getting the following message when I try to connect (I'm using Eclipse as an IDE):</p>
  
  <pre><code>com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
  </code></pre>
  
  <p>So far, I've gone into the Configuration Mgr and activated and enabled TCP/IP.</p>
  
  <p>I've also gone in and set each IP address to on and active under the Properties > IP Addresses tab.  </p>
  
  <p>A couple of potential issues.  </p>
  
  <p>When I look at the status of the SQL Server Browser it is 'Stopped'.  I'm not sure if this is an issue and if so how to fix it. </p>
  
  <p>I'm using the <code>sqljdbc4.jar</code> file and have it in the Referenced Libraries section of the project.</p>
  
  <p>Let me know if you have any suggestions please.</p>
  
  <p>EDIT:</p>
  
  <p>I'm using a Windows 7 box.</p>
  
  <p>UPDATE:</p>
  
  <pre><code>&gt;java -version
  java version "1.6.0_14"
  Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
  Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
  </code></pre>