title: Connect to SQL Server 2012 using jTDs
body: <p>Trying to connect to SQL Server 2012 (Express Edition) using JDBC.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.sql.*;
  
  public class MSSqlTestConnection {
      public static void main(String[] args) {
          try {
              Connection con = DriverManager.getConnection (
                  "jdbc:jtds:sqlserver://localhost;instance=LOCALDB#B431115D;DatabaseName=foo","sa","my_password"
              );
              Statement stmt = con.createStatement();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I have tried every possible combination available. Here is the full stack trace :</p>
  
  <pre><code>java.sql.SQLException: Login failed for user 'sa'.
      at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
      at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
      at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
      at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:632)
      at net.sourceforge.jtds.jdbc.JtdsConnection.&lt;init&gt;(JtdsConnection.java:371)
      at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at MSSqlTestConnection.main(MSSqlTestConnection.java:10)
  </code></pre>
  
  <p>Pipe and TCP/IP is enabled</p>
  
  <p>Mixed Authentication mode is enabled</p>
  
  <p>Works fine with Management Studio when I login using "sa"</p>
  
  <p>Ive pretty much tried everything. I cant seem to find a solution. I tried changing instance=SQLExpress. Removing instance property. Adding username and password property. Adding databasename property. Nothing works. </p>
  
  <p>Any help would be appreciated.</p>
  
  <p><strong>SOLUTION :</strong></p>
  
  <p>I re-installed SQL Server 2012. Finally got to the working connection string. Turns out Microsoft SQL Server 2012 has changed the name of the default instance. It is now MSSQLSERVER instead of SQLExpress. You can choose your own instance i.e default or named while setting up SQL Server 2012.</p>
  
  <pre><code>Connection Connect = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433;instance=MSSQLSERVER;DatabaseName=Database_name",user,password);
  </code></pre>
  
  <p>Also don't forget to use mixed authentication mode while setting up SQL Server.</p>