android simulator ADT links to local database Mysql

android simulator ADT links to local database Mysql

1. Create a new link database thread and insert data into the database to view the link effect
Note that if you only test the link ip between the simulator and the local database MySQl, you can directly fill in 10.0.2.2
ip can be directly filled in 10.0.2.2
ip can be directly filled in 10.0.2.2
I don't need to check the ip address of the computer. I need to say the important things three times

import android.util.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RegisterThread extends Thread{
    private Locationuserinfo user;
    public static String TGA = "MyActivity";
    public RegisterThread(Locationuserinfo user) {
        this.user = user;
    }
    public void run() {

        Log.e(TGA,"Start linking");
        Connection cn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            cn = DriverManager.getConnection("jdbc:mysql://10.0.2.2: 3306 / run ", / / IP + port + database name ip 10.0.2.2 port 3306 database run
                    "root", "123456");             //User name, password user name root password 123456
            String sql = "INSERT INTO users(id,pw) VALUES (?,?)";       //sql inserts data into the database
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setString(1,"12345678850");
            ps.setString(2,"123321");
            ps.executeUpdate();

            Log.e(TGA,"Link success");
            if (ps!=null) {
                ps.close();
            }
            if (cn!=null) {
                cn.close();
            }
        }catch (ClassNotFoundException e) {
            Log.e(TGA,"link failure");
            e.printStackTrace();
        }catch (SQLException e) {
            Log.e(TGA,"link failure");
            e.printStackTrace();
        }
    }
}

2. call in MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RegisterThread registerThread = new RegisterThread(new Locationuserinfo("",""));
        registerThread.start();
    }
};

3. Import jdbc package
Switch to the project directory and paste the jar package directly into libs
Then add a dependency
Right click the jar package and click ok
4. Add network permission

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mysql">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  //network right
    <uses-permission android:name="android.permission.INTERNET" />        

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Login">

        </activity>
    </application>

</manifest>

5. Operation effect


6. Project code
Links: https://pan.baidu.com/s/1p0FjsSxvWIACdm9hLDUzRQ
Extraction code: 1iba

Summary: in this paper, the simulator links Mysql, and the real machine cannot achieve the effect
IP: 10.0.2.2 when key link database

Tags: Mobile Android Database SQL MySQL

Posted on Mon, 18 Nov 2019 12:45:48 -0500 by davitz38