从Android应用程序连接到MariaDB

编程入门 行业动态 更新时间:2024-10-28 02:26:37
本文介绍了从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中安装它/安装在哪里?( downloads.mariadb/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? (downloads.mariadb/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

本文发布于:2023-11-01 17:29:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   应用程序   Android   MariaDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!