Home>Article>Database> Can android use mysql?

Can android use mysql?

藏色散人
藏色散人 Original
2019-05-07 17:11:18 5564browse

Android can use the mysql database. There are two ways for Android to connect to the database, namely: 1. By connecting to the server, and then the server reads the database to realize the addition, deletion, modification and query of data; 2. Loading the external jar package , directly connect to the database.

Can android use mysql?

android can use the mysql database. There are two ways for android to connect to the database.

The first is to connect to the server and then have the server read the database to add, delete, modify and check data. This is also our common method.

The second method is for Android to directly connect to the database. This method consumes a lot of mobile phone memory and is easily decompiled, causing security risks. Therefore, it is not recommended in actual projects.

1. Load external jar package

If you want to use jdbc in the Android project, you must import the external jar package of jdbc, because there is no jdbc api in Java's jdk. I use The jar package is mysql-connector-java-5.1.18-bin.jar package. There are mysql-connector-java-5.1.18-bin.jar package on the Internet. When I tried to use it myself, I found it was incompatible, so I downloaded it. For newer versions, the jar package can be downloaded from the official website or from Baidu. There are many uploaded by predecessors.

2. How to import the jar package

Method 1:

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

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

method Two: Download the jar package and copy it to the libs directory of the project, then right-click the copied jar package Add as libs

3. 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()); //输出第n行,列名为“id”的值 Log.e("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"); //引用代码此处需要修改,address为数据IP,Port为端口号,DBName为数据名称,UserName为数据库登录账户,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); //测试数据库连接 } 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"; //查询表名为“oner_alarm”的所有内容 Statement stmt = con1.createStatement(); //创建Statement ResultSet rs = stmt.executeQuery(sql); //ResultSet类似Cursor //ResultSet最初指向第一行 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:

In After Android 4.0, time-consuming operations are not allowed in the main thread (connecting to the database is a time-consuming operation). A new thread needs to be opened to handle such time-consuming operations. When there is no new thread, the operation is always Just exit the program directly and open a new thread to handle it directly, and there will be no problem.

Of course, connecting to the database requires a network. Don’t forget to add network access permissions:

Four. Bug points

1. The imported jar package must be Correct

2. A new thread must be opened to connect to the database

3. The IP of the database must be pingable, and the LAN address cannot be accessed by mobile phone

4. Database Is the server where the server is located has a firewall that blocks access?

The above is the detailed content of Can android use mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn