如何将Android Studio与SQL Server数据库连接

首先,如果尝试连接Android Studio(一个基于JVM的IDE)和MS SQL Server,我的尝试是徒劳的,请立即告诉我。 我确实理解Oracle或MySQL会更好。 但是,我想尝试一下。

我已经使用Eclipse成功访问了一个SQL Server数据库,为sqljdbc42.jar文件添加了一个外部依赖项,并提供了一些由Microsoft提供的方便代码(忽略public static void main方法……这来自Eclipse):

//===================================================================== // // 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) {} } } } 

从本质上讲,我对Android Studio采用了相同的方法。 我使用了Microsoft提供的相同JDBC 4.2驱动程序。 在build.gradle文件中,我尝试相应地调整依赖项以适应新的.jar文件。 下面是我的build.gradle文件:

 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') } 

你可以看到我已经注释掉“compile fileTree(…)”这是我发现的其他post的指示。 我还在每条指令的defaultConfig块中添加了“multiDexEnabled true”行。

问题在于sqljdbc42.jar并将其添加为依赖项。 包含它,我的项目Builds,它Cleans,它Rebuilds,但它不运行或调试。 它会生成我已经发现的错误。 错误上的大多数post都是指我正在使用的JDK版本。 我使用的是jdk1.8.0_71。 我确实试图定位JDK 1.7。 错误消息如下:

错误1:

 Error:com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000) 

错误2:

 Error:Execution failed for task ':app:transformClassesWithDexForDebug'. 

com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:进程’命令’C:\ Program Files \ Java \ jdk1.8.0_71 \ bin \ java.exe”以非零退出值1结束

我为奇怪的格式道歉。 谢谢你的帮助。 如果您需要更多信息,请发表评论。

找到了解决方法。 答案不是很多。 使用SourceForge提供的其他JDBC驱动程序而不是Microsoft提供的驱动程序。 看看这里 。

您是否要创建访问SQL Server的Android应用程序? 如果是这样,我恐怕你不能。 Android无法直接访问SQL Server,Android应用程序只是需要中间件或服务才能访问SQL Server的客户端应用程序。 您需要首先使用服务器端语言ex创建中间件/服务。 Java,PHP,.Net,Phyton等等你的Android应用程序用Http Connection调用它