简体   繁体   English

如何使用Mysql JDBC驱动程序将Android与MySQL连接

[英]How to connect Android with MySQL using Mysql JDBC driver

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. 我想从MySQL中的表中获取数据并在TextView中显示它,但是当我尝试连接数据库时遇到了一些问题。
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. 我正在使用Eclipse for Android,当我尝试在Java Project中从MySQL获取数据时它可以工作,但是当我使用Android Project时它不起作用。
Does anyone know how can I connect MySQL with Android Project using MySQL JDBC driver? 有谁知道如何使用MySQL JDBC驱动程序将MySQL与Android Project连接?
Or to give me other advice how to connect Android Project with MySQL? 或者给我一些如何将Android项目与MySQL连接的建议?

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 下载驱动程序mysql-connector-java-3.0.17-ga-bin.jar
  • Paste it to the folder libs in your android project. 将其粘贴到android项目中的文件夹libs中。
  • Click ConfigureBuildPath->add jar to include the jar to the project. 单击ConfigureBuildPath-> add jar以将jar包含到项目中。
  • Now you have the driver, but you also need to give permissions in the androidManifest.xml for INTERNET. 现在你有了驱动程序,但你还需要在androidManifest.xml中为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"); } 

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. 建议:如果驱动程序的实例没有给出任何错误但是你得到连接的异常,你应该尝试从清单中删除Target SDK版本,因为某些版本会产生问题。

Android by default does not support MySQL. Android默认不支持MySQL。 It has an in-built database ie SQLite. 它有一个内置的数据库,即SQLite。 If you are trying to access MySQL database remotely, you should expose interface to this database with any standard web service. 如果您尝试远程访问MySQL数据库,则应使用任何标准Web服务向此数据库公开接口。 Eg you could create RESTful Web Service on Server Side which is written using Java/PHP etc. and MySQL Connector. 例如,您可以在服务器端创建使用Java / PHP等和MySQL Connector编写的RESTful Web服务。 (Which you have already done!) And your Android App could talk to this service with the URL generated using this web service. (您已经完成了!)您的Android应用程序可以使用此Web服务生成的URL与此服务进行通信。

Again, this question has been repeated previously, so you can check those solutions. 同样,此问题之前已重复,因此您可以检查这些解决方案。

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

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