从Android应用程序连接到MariaDB [英] Connecting to MariaDB from an Android app

查看:136
本文介绍了从Android应用程序连接到MariaDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我知道人们通常不这样做,出于成本原因,我正在使用SQL数据库,而且我对编程还比较陌生.

Disclaimer: I know people don't typically do this, I am using an SQL database for cost reasons, also I am relatively new to programming.

我正在尝试从Android Studio应用程序向我的MariaDB数据库发送SQL脚本.我目前正在努力为MariaDB实现Java数据库连接(JDBC)驱动程序,但是我不确定需要包含什么.

I am trying to send SQL scripts to my MariaDB database from my Android Studio application. I am currently working to implement a Java Database Connectivity (JDBC) driver for MariaDB however I'm not sure what I need to include.

我访问了 JDBC MariaDB 驱动程序的下载网站,但是我可以选择下载许多 jar 文件.您如何知道您需要哪一个,以及如何在Android中安装它/安装在哪里?( https://downloads.mariadb.org/connector-java/2.3.0/)

I went to the download website for the JDBC MariaDB driver however there are many jar files which I have the option of downloading. How do you know which one you need and how do you know how to install it/where for Android? (https://downloads.mariadb.org/connector-java/2.3.0/)

请注意,我的Java代码如下,因此收到错误消息找不到适合于...的驱动程序":

As a note, my Java code is as follows, for which I get the error message "No suitable driver found for .... ":

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null){
            if(result.getContents() == null) {
                Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
            }
            else {
                // Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
                String host = "jdbc:mariadb://HOST:PORT/DBNAME";
                String username = "UNAME";
                String password ="PWD";
                try {
                    Connection con = DriverManager.getConnection( host, username, password );
                    Toast.makeText(this, result.getContents() + " - Success!", Toast.LENGTH_LONG).show();
                } catch (Exception err) {
                    Toast.makeText(this, err.getMessage(), Toast.LENGTH_LONG).show();

                }

            }
        }
        else {
            super.onActivityResult(requestCode, resultCode, data);
        }

    }

推荐答案

您需要确保使用正确的驱动程序(连接器).下载它,然后将其导入Android Studio(libs文件夹),然后将以下内容添加到您的应用程序构建包中:

You need to make sure to use the correct driver (connector). Download it and then you import it into Android Studio (libs folder) and then add the following to your app build gradle:

implementation files('libs/mariadb-java-client-1.8.0.jar')

这是我使用的Connector/J(V1.8.0)...较新的似乎不兼容.示例代码:

This is the Connector/J that I use (V1.8.0) ... the newer ones don't seem to be compatible. Sample code:

// Create a connection 
val myConn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/DB?user=username&password=myPassword")
Class.forName("org.mariadb.jdbc.Driver")  // Initialize it

// Now you create SQL statements
val sql01 : PreparedStatement? = (myConn!!.prepareStatement(getString(R.string.sql01)))

// Submit the query
reset = sql01.executeQuery()

这篇关于从Android应用程序连接到MariaDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆