Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The solution of Android not connecting to mysql

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how Android can't connect to mysql. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

The solution for Android not connecting to mysql: first, customize the listening event in Activivty; then open up child threads in the custom listening event; finally, change the definition of coonection to "DriverManager.getConnection (...)". That's it.

Android Studio connection MySQL: problem solving: virtual machine cannot connect local SQL,coon is always empty

Connect to the database in the child thread

First customize the listening event in Activivty

/ / write private Button mBtn; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); SetListener (); / / Custom listening events} in Activity

Open up a child thread in a custom listening event, and note 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 connection Statement stmt=null / / used to execute the SQL statement try {/ / register the driver Class.forName ("com.mysql.jdbc.Driver"); / / the database execution statement String sql= "insert into demo values (666Magne6666)" / / get the connection object. If localhost is not successful, change it to 10.0.2.2. This is the address of the computer on the virtual machine. Note that it is the virtual machine, and the test conn=DriverManager.getConnection for the virtual machine ("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")

The result of each connection is reported: coon=null. Finally, multi-party debugging found this problem. The ip of the computer corresponding to the virtual machine should be 10.0.2.2, that is, the definition of coonection should be changed to

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

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

Thank you for reading! So much for the solution that Android can not connect to mysql. I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report