title: How to connect Android Studio with SQL Server database
body: <p>First, just tell me right off the bat if my attempts are futile by trying to connect Android Studio, a JVM-based IDE, with MS SQL Server. I do understand Oracle or MySQL would be better. However, I would like to try it.</p>
  
  <p>I have successfully accessed a SQL Server database using Eclipse by adding an external dependency to the sqljdbc42.jar file and with some handy code provided by Microsoft (ignore the public static void main method...This was from Eclipse):</p>
  
  <pre><code>//=====================================================================
  //
  //  File:    connectURL.java      
  //  Summary: This Microsoft JDBC Driver for SQL Server sample application
  //       demonstrates how to connect to a SQL Server database by using
  //       a connection URL. It also demonstrates how to retrieve data 
  //       from a SQL Server database by using an SQL statement.
  //
  //---------------------------------------------------------------------
  //
  //  This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
  //  Copyright (C) Microsoft Corporation.  All rights reserved.
  //
  //  This source code is intended only as a supplement to Microsoft
  //  Development Tools and/or on-line documentation.  See these other
  //  materials for detailed information regarding Microsoft code samples.
  //
  //  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
  //  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  //  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  //  PARTICULAR PURPOSE.
  //
  //===================================================================== 
  
  import java.sql.*;
  
  public class connectURL {
  
  public static void main(String[] args) {
  
      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
          "databaseName=AdventureWorks2014;integratedSecurity=true;";
  
      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;
  
          try {
              // Establish the connection.
              Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                  con = DriverManager.getConnection(connectionUrl);
  
                  // Create and execute an SQL statement that returns some data.
                  String SQL = "SELECT TOP 10 * FROM Person.ContactType";
                  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(1) + " " + rs.getString(2));
                  }
          }
  
      // 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>Essentially, I took the same approach for Android Studio. I used the same JDBC 4.2 driver provided by Microsoft. In the build.gradle file I have tried adjusted the dependencies accordingly to accommodate for the new .jar file. Below is my build.gradle file:</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 23
      buildToolsVersion '23.0.2'
  
      defaultConfig {
          applicationId "androidapp.dillon.betterhalf_v2"
          minSdkVersion 15
          targetSdkVersion 23
          versionCode 1
          versionName "1.0"
  
          multiDexEnabled true
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  
  }
  
  dependencies {
      //compile fileTree(include: ['*.jar'], dir: 'libs')
      testCompile 'junit:junit:4.12'
      compile 'com.android.support:appcompat-v7:23.1.1'
      compile 'com.android.support:design:23.1.1'
      compile 'com.android.support:recyclerview-v7:23.1.1'
      compile files('libs/sqljdbc42.jar')
      }
  </code></pre>
  
  <p>You can see that I have commented out "compile fileTree(...)" This was by instruction from other posts I found. I also added the "multiDexEnabled true" line in my defaultConfig block per instruction.</p>
  
  <p>The issue lies in the sqljdbc42.jar and adding it as a dependency. With it included, my project Builds, it Cleans, and it Rebuilds, but it does NOT Run or Debug. It generates an error(s) that I have found all over. Most posts on the error refer to the version of JDK I am using. I am using jdk1.8.0_71. I did try to target JDK 1.7. The error messages are as follows:</p>
  
  <p>Error 1: </p>
  
  <pre><code>Error:com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
  </code></pre>
  
  <p>Error 2:</p>
  
  <pre><code>Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
  </code></pre>
  
  <blockquote>
    <p>com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_71\bin\java.exe'' finished with non-zero exit value 1</p>
  </blockquote>
  
  <p>I apologize for the weird formatting.
  Thanks, for you help. Please comment if you need any more information.</p>