首先,如果尝试将基于JVM的IDE Studio与MS SQL Server连接起来,试图让我失望,那么请立即告诉我。我理解Oracle或MySQL会更好。不过,我想尝试一下。如何将Android Studio与SQL Server数据库连接起来
我已经成功地使用Eclipse访问SQL Server数据库,通过向sqljdbc42.jar文件添加外部依赖项并使用Microsoft提供的一些便捷代码(忽略公共静态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')
}
你可以看到,我已经注释掉“编译文件树(......)”这是从我发现其他职位的指令。我还在每条指令的defaultConfig块中添加了“multiDexEnabled true”行。
问题在于sqljdbc42.jar并将其添加为依赖项。包括它,我的项目构建,它清理,它重建,但它不运行或调试。它会产生一个我已经发现的错误。错误上的大多数帖子都是指我正在使用的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
我为奇怪的格式道歉。 谢谢,为您提供帮助。如果您需要更多信息,请发表评论。
多一个.. Eclipse和Android Studio不是编程语言。当您说“使用Eclipse成功访问SQL Server数据库”时,实际上是使用简单的Java代码从Java成功访问了SQL Server –
是的,Eclipse是承载编译Java代码的JVM的IDE。我明白那个。 Android Studio是否也使用JVM编译Java?我不明白如何使用JDBC驱动程序访问SQL Server实例。 – mtprogrammer
实际上,您的PC中没有像您一样使用JVM。 Android使用Dalvik和现在的ART运行时,所以并非Java中的所有功能(JVM)都可以在Android上运行,包括使用JDBC Driver访问SQL Server。但是,当然,创建Android应用程序时需要JVM(JDK),因为它需要编译代码 –