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

查看:54
本文介绍了从 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 应用程序将 SQL 脚本发送到我的 MariaDB 数据库.我目前正在为 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 文件夹),然后将以下内容添加到您的应用构建 gradle:

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')

这是我使用的连接器/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天全站免登陆