What to do if Android cannot connect to mysql
The solution to the failure of Android to connect to mysql: first, customize the listening event in Activty; then create a sub-thread in the custom listening event; finally change the definition of coonection to “DriverManager.getConnection(… )” is enough. Recommendation: “Mysql Video Tutorial” Android Studio connects to MySQL: problem solving: virtual machine cannot connect to local SQL, coon is always Empty Connect to the database in the child thread First, customize the listening event in Activty / /write in Activity private Button mBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R. layout. activity_main); SetListener();//Custom listener event } Create a child thread in the custom listening event, pay attention to the last .start() private void SetListener() { mBtnadmin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Thread(new Runnable() { @Override public void run() { Connection cOnn=null;//Create a connection Statement stmt=null;//Used to execute SQL statements try { //register driver Class.forName(“com.mysql.jdbc.Driver”); // Execution statement of the database String sql=”insert into demo values(6666,6666)”; //Get the connection object. If localhost fails here, change it to 10.0.2.2. This is the address of the computer on the virtual machine. Note that it is a virtual machine, which is used for virtual machine testing cOnn=DriverManager.getConnection(“jdbc:mysql://10.0.2.2:3306/logindata”,”root”,”666666″); stmt=conn.createStatement(); stmt. executeUpdate(sql);…