java 如何使用 Mysql JDBC 驱动程序连接 Android 和 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11105529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:48:58  来源:igfitidea点击:

How to connect Android with MySQL using Mysql JDBC driver

javaandroidmysql

提问by Balsa Bojic

I want to get data from table in MySQL and to show it in TextView, but I have some problems when I try to connect with database.
I'm using Eclipse for Android, and when I try to get data from MySQL in Java Project it works, but when I use Android Project it doesn't work.
Does anyone know how can I connect MySQL with Android Project using MySQL JDBC driver?
Or to give me other advice how to connect Android Project with MySQL?

我想从 MySQL 中的表中获取数据并在 TextView 中显示它,但是当我尝试连接数据库时遇到了一些问题。
我正在使用 Eclipse for Android,当我尝试从 Java Project 中的 MySQL 获取数据时,它可以工作,但是当我使用 Android Project 时,它不起作用。
有谁知道如何使用 MySQL JDBC 驱动程序将 MySQL 与 Android 项目连接起来?
或者给我其他建议如何将 Android 项目与 MySQL 连接?

采纳答案by saurcery

Android by default does not support MySQL. It has an in-built database i.e SQLite. If you are trying to access MySQL database remotely, you should expose interface to this database with any standard web service. E.g you could create RESTful Web Service on Server Side which is written using Java/PHP etc. and MySQL Connector. (Which you have already done!) And your Android App could talk to this service with the URL generated using this web service.

Android 默认不支持 MySQL。它有一个内置数据库,即 SQLite。如果您尝试远程访问 MySQL 数据库,您应该使用任何标准 Web 服务公开该数据库的接口。例如,您可以在使用 Java/PHP 等和 MySQL 连接器编写的服务器端创建 RESTful Web 服务。(您已经完成了!)并且您的 Android 应用程序可以使用使用此 Web 服务生成的 URL 与此服务通信。

Again, this question has been repeated previously, so you can check those solutions.

同样,这个问题之前已经重复过,所以你可以检查这些解决方案。

回答by Jav_Rock

If you want to connect to Mysql database from Android you just need to follow these steps:

如果你想从 Android 连接到 Mysql 数据库,你只需要按照以下步骤操作:

  • Download the driver mysql-connector-java-3.0.17-ga-bin.jar
  • Paste it to the folder libs in your android project.
  • Click ConfigureBuildPath->add jar to include the jar to the project.
  • Now you have the driver, but you also need to give permissions in the androidManifest.xml for INTERNET.
  • Use the next code for connecting:

    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    }catch(Exception e){
        System.err.println("Cannot create connection");
    }
    try{
        connection = DriverManager.getConnection("jdbc:mysql://192.168.xx.xx:3306/dbname","root","password");
        Statement statement = connection.createStatement();
    
        String query = "SELECT column1, column2 FROM table1 WHERE column3 = ";
        query = query +"'" +variable+"'";           
        ResultSet result = statement.executeQuery(query);
    }catch(Exception e){
        System.err.println("Error");
    } 
    
  • 下载驱动mysql-connector-java-3.0.17-ga-bin.jar
  • 将其粘贴到您的 android 项目中的文件夹 libs 中。
  • 单击 ConfigureBuildPath->add jar 将 jar 包含到项目中。
  • 现在您有了驱动程序,但您还需要在 androidManifest.xml 中为 INTERNET 授予权限。
  • 使用下一个代码进行连接:

    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    }catch(Exception e){
        System.err.println("Cannot create connection");
    }
    try{
        connection = DriverManager.getConnection("jdbc:mysql://192.168.xx.xx:3306/dbname","root","password");
        Statement statement = connection.createStatement();
    
        String query = "SELECT column1, column2 FROM table1 WHERE column3 = ";
        query = query +"'" +variable+"'";           
        ResultSet result = statement.executeQuery(query);
    }catch(Exception e){
        System.err.println("Error");
    } 
    

Advice: If the instance of the drivers doesn't give any errors but you get an exception with the connection you should try to remove the Target SDK version from your manifest, as for some versions this gives problems.

建议:如果驱动程序的实例没有给出任何错误,但您的连接出现异常,您应该尝试从清单中删除目标 SDK 版本,因为对于某些版本,这会产生问题。