SoFunction
Updated on 2025-03-04

Summary of android's method using mysql

Android can use mysql database, and there are two ways to connect to the database.

The first is to connect to the server, and then the server reads the database to implement data addition, deletion, modification and search, which is also a common method for us.

The second way is to directly connect to the database from Android, This method consumes mobile phone memory and is easily decompiled to cause security risks, so it is not recommended to use it in actual projects.

1. Load external jar package

If you want to use jdbc in Android project, you need to import the jdbc external jar package. Because there is no jdbc API in Java jdk, the jar package I use is the mysql-connector-java-5.1. package. There are mysql-connector-java-5.1. package on the Internet. When I use it myself, I found that it is incompatible. So I downloaded a relatively new version of the jar package. You can download it on the official website or Baidu. There are many people uploaded it before.

2. How to import jar packages

Method 1:

You can directly add the following statement to the project file to import it

compile files('libs/mysql-connector-java-5.1.')

Method 2: Download the jar package and copy it to the project's libs directory, and then right-click the jar package to copy Add as libs

3. Establish a database connection

protected void onCreate(Bundle savedInstanceState) {

  (savedInstanceState);

  setContentView(.activity_jdbc);

  new Thread(runnable).start();

}

Handler myHandler=new Handler(){

  public void handleMessage(Message msg) {

    // TODO Auto-generated method stub

    (msg);

    Bundle data=new Bundle();

    data=();

    //("id:"+("id").toString()); //Output the value of the nth row and the column name is "id"
    ("TAG","id:"+("id").toString());

    TextView tv= (TextView) findViewById();

    //("content:"+("content").toString());

  }

};

Runnable runnable=new Runnable() {

  private Connection con = null;

  @Override

  public void run() {

    // TODO Auto-generated method stub

    try {

      ("");

      //The 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 =

          //("jdbc:mysql://192.168.1.202:3306/b2b", "root", "");

      ("jdbc:mysql://http://192.168.1.100/phpmyadmin/:8086/b2b",

          UserName,Password);

    } catch (SQLException e) {

      // TODO Auto-generated catch block

      ();

    } catch (ClassNotFoundException e) {

      // TODO Auto-generated catch block

      ();

    }

    try {

      testConnection(con);  //Test the database connection
    } catch ( e) {

      // TODO Auto-generated catch block

      ();

    }

  }

  public void testConnection(Connection con1) throws  {

    try {

      String sql = "select * from ecs_users";    //Query all contents of the table name "oneer_alarm"
      Statement stmt = ();    //Create Statement
      ResultSet rs = (sql);     //ResultSet is similar to Cursor
      //<code>ResultSet</code> initially points to the first line
      Bundle bundle=new Bundle();

      while (()) {

        ();

        ("id",("userid"));

        //("content",("content"));

        Message msg=new Message();

        (bundle);

        (msg);

      }

      ();

      ();

    } catch (SQLException e) {

    } finally {

      if (con1 != null)

        try {

          ();

        } catch (SQLException e) {}

    }

  }

};

Notice:

After Android 4.0, time-consuming operations in the main thread are not allowed (connecting to the database is a time-consuming operation). You need to open a new thread to handle such time-consuming operations. When there is no new thread, the program will exit directly. If a new thread is opened, it will be fine.

Of course, connecting to the database requires a network, so don't forget to add access to the network:

<uses-permission android:name=””/>

Four. Bug points

1. The imported jar package must be correct

2. Be sure to open a new thread to connect to the database

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

4. Whether the server where the database is located has a firewall, blocking access