简体   繁体   English

通过JDBC将Android Studio与(本地)mySql Server连接

[英]Connecting Android Studio with (local) mySql Server via JDBC

I am trying to connect my Android App with a my local MySQL Database and then to get some content of the Database with JDBC. 我试图将我的Android应用程序与本地MySQL数据库连接,然后使用JDBC获得数据库的某些内容。 So I tried this code to connect to the Database: 因此,我尝试使用以下代码连接到数据库:

try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/rpg","root", "whatever");
            Statement statement = connection.createStatement();
            final ResultSet resultSet = statement.executeQuery("select * from player");

            Log.i("Content",resultSet.getString(0));

        }catch (Exception e) {
            e.printStackTrace();
        }

I gave internet permission in manifest ( <uses-permission android:name="android.permission.INTERNET"/> ) 我在清单中授予了Internet许可( <uses-permission android:name="android.permission.INTERNET"/>

and implemented the mySQL connector as library(mysql-connector-java-8.0.12.jar). 并将mySQL连接器实现为库(mysql-connector-java-8.0.12.jar)。

When I want to launch this I get this error: 当我要启动此程序时,出现以下错误:

Default interface methods are only supported starting with Android N (--min-api 24): void com.mysql.cj.protocol.AuthenticationPlugin.destroy()  
Default interface methods are only supported starting with Android N (--min-api 24): void com.mysql.cj.protocol.MessageSender.send(com.mysql.cj.protocol.Message)   
Default interface methods are only supported starting with Android N (--min-api 24): byte com.mysql.cj.protocol.MessageReader.getMessageSequence()  
Default interface methods are only supported starting with Android N (--min-api 24): com.mysql.cj.result.Row com.mysql.cj.result.RowList.get(int)   
Default interface methods are only supported starting with Android N (--min-api 24): com.mysql.cj.protocol.Message com.mysql.cj.interceptors.QueryInterceptor.postProcess(com.mysql.cj.protocol.Message, com.mysql.cj.protocol.Message) 
null    
com.android.builder.dexing.DexArchiveBuilderException: Failed to process C:\Users\Alexander\Desktop\Programmierung\Testing2\app\libs\mysql-connector-java-8.0.12.jar    
com.android.builder.dexing.DexArchiveBuilderException: Error while dexing.  
com.android.tools.r8.CompilationFailedException: Compilation failed to complete 
com.android.tools.r8.utils.AbortException

This sounds like I have to increase the minSdkVersion to 24 right? 听起来我必须将minSdkVersion增加到24,对吗? So I did this, and then this error appears: 所以我做到了,然后出现此错误:

Invoke-customs are only supported starting with Android O (--min-api 26)    
null    
com.android.builder.dexing.DexArchiveBuilderException: Failed to process C:\Users\Alexander\Desktop\Programmierung\Testing2\app\libs\mysql-connector-java-8.0.12.jar
com.android.builder.dexing.DexArchiveBuilderException: Error while dexing.  
com.android.tools.r8.CompilationFailedException: Compilation failed to complete 
com.android.tools.r8.utils.AbortException   

So I also increased it to 26, then getting this error: 所以我也将其增加到26,然后出现此错误:

Installation failed with message INSTALL_FAILED_OLDER_SDK: Failed parse during installPackageLI: /data/app/vmdl127069021.tmp/base.apk (at Binary XML file line #7): Requires newer sdk version #26 (current version is #25).
It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.

WARNING: Uninstalling will remove the application data!

Do you want to uninstall the existing application?

But it feels like I am in the completely wrong way anyways. 但是,无论如何,我感觉完全是错误的。 Somebody has an idea how I can do this. 有人知道我该怎么做。

  • open build.gradle (Module: Application) in Project Window 在项目窗口中打开build.gradle (模块:应用程序)
  • change minSdkVersion to 24 or higher minSdkVersion更改为24或更高

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM