2016-03-13 23 views
0

我正嘗試從Android Studio連接到Java數據庫。將mysql-connector-java-5.1.38-bin粘貼到lib中 - Android Studio

當我創建一個全新的空項目並運行它時,項目運行良好。只要將mysql-connector-java-5.1.38-bin粘貼到lib目錄(app/lib)中,然後嘗試運行項目,就會出現2個錯誤。

錯誤:com.android.dx.cf.iface.ParseException:糟糕的類文件魔術(cafebabe)或版本(0034.0000)

錯誤:執行失敗的任務 ':應用程序:transformClassesWithDexForDebug'。

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_74\bin\java.exe'' finished with non-zero exit value 1

我也試着將它作爲一個依賴,但我不認爲我需要因爲{包括= [*。JAR],DIR =庫}已經在依賴列表。

感謝您閱讀

回答

0

Does JDBC not work with Android?

JDBC很少使用在Android上,我肯定不會推薦它。恕我直言,JDBC專爲高帶寬,低延遲,高度可靠的網絡連接(例如桌面到數據庫服務器,Web應用服務器到數據庫服務器)而設計。移動設備很少提供這些功能,而且它們中的任何一項都不一致

If so, tell me what alternatives I should look into for remote MySQL database access.

在您的數據庫周圍創建一個Web服務並從Android訪問它。

作爲附帶好處,可以提高安全性(相對於離開你的數據庫打開),可以從客戶端卸載一些業務邏輯,可以更好地支持其他平臺(例如,網站或基於Web的移動架構)等

this answer

Android cannot connect directly to the database server. Therefore we need to create a simple web service that will pass the requests to the database and will return the response.

引用所以我的建議是不要使用JDBC jar文件中的機器人。

查詢this answer關於Execution failed for task :app transformClassesWithDexForDebug的更多細節。