源码:
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