首先,如果嘗試將基於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),因爲它需要編譯代碼 –