Home > Database > Mysql Tutorial > What to do if Android cannot connect to mysql

What to do if Android cannot connect to mysql

藏色散人
Release: 2020-11-06 10:45:35
Original
2638 people have browsed it

Solution to the problem that Android cannot connect to mysql: First, customize the listening event in Activivty; then open up a child thread in the custom listening event; finally, change the definition of coonection to "DriverManager.getConnection(... )".

What to do if Android cannot connect to mysql

Recommendation: "mysql video tutorial"

Android Studio connects to MySQL: Problem solved: The virtual machine cannot connect to the local SQL, coon is always empty

Connect to the database in a child thread

First customize the listening event in Activivty

//写在Activity中
private Button mBtn;
  @Override
 protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SetListener();//自定义监听事件
        }
Copy after login

Open 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;//创建连接
                        Statement stmt=null;//用以执行SQL语句
                        try{
                        //注册驱动
                            Class.forName("com.mysql.jdbc.Driver");
                        //数据库的执行语句
                            String sql="insert into demo values(6666,6666)";
 //获取connection对象,这里使用localhost如果无法成功,则改成10.0.2.2,这是虚拟机上电脑的地址,注意是虚拟机,用于虚拟机的测试
                            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();
             }
        });
    }
Copy after login

Because I have been using a virtual machine for testing, the definition of coonection has always been

 conn= DriverManager.getConnection("jdbc:mysql://localhost/logindata","root","666666");
Copy after login

Every time the connection result is reported: coon=null, Finally, multiple debugging parties discovered this problem. The IP address of the computer corresponding to the virtual machine should be 10.0.2.2. That is, changing the definition of coonection to

conn= DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/logindata","root","666666");
Copy after login

was successful! I was always worried that it was a code problem, so I didn’t try to 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 follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template