1024programmer Mysql What to do if Android cannot connect to mysql

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);
                             }
                         } catch (Exception e)
                         {
                             e.printStackTrace();
                         } finally {
                      stmt. close();
                      conn. close();
                            // JDBCUtils.Close(stmt,conn);
                         }
                     }
                 }).start();
              }
         });
     }

Because the virtual machine has been used for testing, the definition of coonection has always been

 cOnn= DriverManager.getConnection("jdbc  :mysql://localhost/logindata","root","666666");

Every time the connection result is reported: coon=null, finally multi-party debugging found this problem, the virtual machine corresponds The ip of your computer should be 10.0.2.2, that is, change the definition of coonection to

cOnn= DriverManager.getConnection("jdbc:mysql://10.0.2.2  :3306/logindata","root","666666");

Success! I was worried about the code problem before, so I didn’t connect to MySQL on the server, now I can try it!

The above is the detailed content of what to do if Android cannot connect to mysql. For more information, please pay attention to other related articles on 1024programmer.com!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/what-to-do-if-android-cannot-connect-to-mysql/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索