Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Can android use mysql?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Can I use mysql for android? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!

Android can use a mysql database, and android can connect to the database in two ways.

The first is to connect to the server, and then read the database by the server to achieve the addition, deletion, modification and query of data, which is also our common way.

The second way is to connect android directly to the database, which consumes a lot of mobile memory and is easy to be decompiled to cause security risks, so it is not recommended in actual projects.

1. Load external jar packages

If you want to use jdbc in the Android project, you need to import the external jar package of jdbc, because there is no jdbc api in the jdk of Java. The jar package I use is a mysql-connector-java-5.1.18-bin.jar package. I use the mysql-connector-java-5.1.18-bin.jar package on the network, and I found it incompatible when I went to use it, so I downloaded a relatively new version, and the jar package can be downloaded from the official website or Baidu. There are a lot of people uploading it before.

two。 How to import jar packages

Method 1:

You can add the following statement directly to the build.gradle file of the project to import

Compile files ('libs/mysql-connector-java-5.1.18-bin.jar')

Method 2: download the jar package and copy it to the libs directory of the project, and then right-click the copied jar package Add as libs

three。 Establish a database connection

Protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_jdbc); new Thread (runnable). Start ();} Handler myHandler=new Handler () {public void handleMessage (Message msg) {/ / TODO Auto-generated method stub super.handleMessage (msg); Bundle data=new Bundle (); data=msg.getData () / System.out.println ("id:" + data.get ("id"). ToString ()); / / output the nth line with the value Log.e named "id" ("TAG", "id:" + data.get ("id"). ToString ()); TextView tv= (TextView) findViewById (R.id.jdbc); / / System.out.println ("content:" + data.get ("content"). ToString ());}} Runnable runnable=new Runnable () {private Connection con = null; @ Override public void run () {/ / TODO Auto-generated method stub try {Class.forName ("com.mysql.jdbc.Driver") / / reference code needs to be modified here. Address is the data IP,Port is the port number, DBName is the data name, UserName is the database login account, and Password is the database login password con = / / DriverManager.getConnection ("jdbc:mysql://192.168.1.202:3306/b2b", "root", ""). DriverManager.getConnection ("jdbc:mysql:// http://192.168.1.100/phpmyadmin/index.php:8086/b2b", UserName,Password);} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (ClassNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace () } try {testConnection (con); / / Test database connection} catch (java.sql.SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} public void testConnection (Connection con1) throws java.sql.SQLException {try {String sql = "select * from ecs_users" / / query all the contents of the table named "oner_alarm" Statement stmt = con1.createStatement (); / / create Statement ResultSet rs = stmt.executeQuery (sql); / / ResultSet similar to Cursor / / ResultSet initially points to the first line Bundle bundle=new Bundle (); while (rs.next ()) {bundle.clear () Bundle.putString ("id", rs.getString ("userid"); / / bundle.putString ("content", rs.getString ("content")); Message msg=new Message (); msg.setData (bundle); myHandler.sendMessage (msg);} rs.close (); stmt.close () } catch (SQLException e) {} finally {if (con1! = null) try {con1.close ();} catch (SQLException e) {}

Note:

After Android4.0, more time-consuming operations are not allowed in the main thread (connecting to the database is a relatively time-consuming operation). You need to open a new thread to deal with this time-consuming operation. When there is no new thread, the program will exit directly, and if you open a new thread to deal with it directly, there will be no problem.

Of course, a network is required to connect to the database, so don't forget to add access to the network:

IV. Bug point

1. The imported jar package must be correct

two。 To connect to the database, you must start a new thread.

3. The IP of the database must be accessible through ping, and the local area network address cannot be accessed by mobile phone.

4. Whether the server where the database is located has opened a firewall to prevent access

Thank you for reading! After reading the above, can you use mysql for android? do you have a general understanding? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report