2016-01-22 341 views
0

首先,如果嘗試將基於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

我爲奇怪的格式道歉。 謝謝,爲您提供幫助。如果您需要更多信息,請發表評論。

回答

0

您是否想創建一個訪問SQL Server的Android應用程序?如果是這樣,恐怕你不能。 Android無法直接訪問SQL Server,Android應用只是需要中間件或服務來訪問SQL Server的客戶端應用程序。您需要首先使用服務器端語言ex創建中間件/服務。 Java,PHP,.Net,Phyton等,然後您的Android應用程序使用Http連接調用它

+0

多一個.. Eclipse和Android Studio不是編程語言。當您說「使用Eclipse成功訪問SQL Server數據庫」時,實際上是使用簡單的Java代碼從Java成功訪問了SQL Server –

+0

是的,Eclipse是承載編譯Java代碼的JVM的IDE。我明白那個。 Android Studio是否也使用JVM編譯Java?我不明白如何使用JDBC驅動程序訪問SQL Server實例。 – mtprogrammer

+0

實際上,您的PC中沒有像您一樣使用JVM。 Android使用Dalvik和現在的ART運行時,所以並非Java中的所有功能(JVM)都可以在Android上運行,包括使用JDBC Driver訪問SQL Server。但是,當然,創建Android應用程序時需要JVM(JDK),因爲它需要編譯代碼 –

2

找到了解決方法。沒有太多答案。使用SourceForge提供的不同的JDBC驅動程序,而不是Microsoft提供的驅動程序。檢查出來here

+0

感謝您的更新。只是好奇:你問題中的示例代碼顯然試圖使用'integratedSecurity = true'。您的jTDS解決方案是否可以使用該類型的身份驗證,還是必須提供用戶ID /密碼憑據? –

+0

我必須在連接URL中提供user = yourusername和password = yourpassword屬性作爲附加屬性。通常,SQL Server的默認用戶名是sa。 – mtprogrammer

相關問題