鱼雨羽 2019-08-13 16:08 采纳率: 50%
浏览 629
已采纳

androidstudio为啥连不上mysql呀,老报如下错

源码:

     bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("进入点击事件!!!!!!!!!");
                new Thread(new Runnable(){
                    public void run(){
                        try {
                            System.out.println("进入线程!!!!!!!!");
                            System.out.println("开始加载驱动");
                            Class.forName("com.mysql.jdbc.Driver");
                            System.out.println("驱动加载完成");
                            java.sql.Connection cn= DriverManager.getConnection(
                                    "jdbc:mysql://192.168.0.106:3306/test","root",
                                    "root");
                            System.out.println("链接数据库");
                            String sql="select name from androidtest";

                            Statement st=(Statement)cn.createStatement();
                            ResultSet rs=st.executeQuery(sql);
                            while(rs.next()){//读表mytable中的每一列
                                String mybook=rs.getString("name ");//读取的是B_Name这一列,传给mybook
                                Log.i("MainActivity",mybook);
                            }
                            cn.close();//一定要关闭
                            st.close();
                            rs.close();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }


                    }
                }).start();

            }

报错信息:

2019-08-13 16:04:24.442 29017-29017/com.example.androidmysqltext001 I/System.out: 进入点击事件!!!!!!!!!
2019-08-13 16:04:24.447 29017-29418/com.example.androidmysqltext001 I/System.out: 进入线程!!!!!!!!
2019-08-13 16:04:24.447 29017-29418/com.example.androidmysqltext001 I/System.out: 开始加载驱动
2019-08-13 16:04:24.542 29017-29036/com.example.androidmysqltext001 I/zygote64: Do partial code cache collection, code=30KB, data=21KB
2019-08-13 16:04:24.542 29017-29036/com.example.androidmysqltext001 I/zygote64: After code cache collection, code=26KB, data=20KB
2019-08-13 16:04:24.542 29017-29036/com.example.androidmysqltext001 I/zygote64: Increasing code cache capacity to 128KB
2019-08-13 16:04:24.608 29017-29418/com.example.androidmysqltext001 I/System.out: 驱动加载完成
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err: The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.lang.reflect.Constructor.newInstance0(Native Method)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.lang.reflect.Constructor.newInstance0(Native Method)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:569)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:219)
2019-08-13 16:06:31.922 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.example.androidmysqltext001.MainActivity$1$1.run(MainActivity.java:33)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.lang.Thread.run(Thread.java:784)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err: Caused by: java.net.ConnectException: failed to connect to /192.168.0.106 (port 3306) from /:: (port 51602): connect failed: ETIMEDOUT (Connection timed out)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:138)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:357)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:201)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:183)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.Socket.connect(Socket.java:616)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.Socket.connect(Socket.java:565)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.Socket.<init>(Socket.java:445)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at java.net.Socket.<init>(Socket.java:248)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:   ... 14 more
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at libcore.io.Linux.connect(Native Method)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:126)
2019-08-13 16:06:31.923 29017-29418/com.example.androidmysqltext001 W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:152)
2019-08-13 16:06:31.924 29017-29418/com.example.androidmysqltext001 W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:130)
2019-08-13 16:06:31.924 29017-29418/com.example.androidmysqltext001 W/System.err:   ... 25 more
  • 写回答

1条回答 默认 最新

  • 关注

    要么是空闲连接被回收了,要么就是你连接的目标主机有问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀