===6-22补充=
1.AndroidManifest.xml开放网络权限
<uses-permission android:name=\"android.permission.INTERNET\"/>这个解决了我之前出现的:The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2.\”jdbc:mysql://10.66.xxx.xx:3306 ip要写对 通过ipconfig 查询 IPv4 地址 .
3.要在子线程运行
4.jar包需要导入!(libs下 最好版本低点)
========================================
0.仅仅只是记录下。我也是个小白。可能有很多问题也不知道。
1.JDBC语句都得在线程中执行…………前辈们已经为我探好了路……当然线程一定要.start()来启动、
2.JDBC语句需要导入jar包到app/lis下才行……最好用版本号低点……我用的5.1.4
3.后面就是重复语句了。。没啥了
以添加数据为例(我是自己创建了一个thread)
package com.example.myapplication111;
import android.util.Log;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import static android.widget.Toast.LENGTH_SHORT;
public class thread extends Thread {
public void run() {
Connection conn = null;while (!Thread.interrupted()) {try {Thread.sleep(1000*5);} catch (InterruptedException e) {Log.e(\"thread\", \"error1\");}try {Class.forName(\"com.mysql.jdbc.Driver\");conn= DriverManager.getConnection(\"jdbc:mysql://“输入你的ip地址”/“数据库名”?serverTimezone=UTC\",\"root\",\"密码\");String sql = \"INSERT INTO us VALUES (?,?)\";PreparedStatement ps = conn.prepareStatement(sql);// PreparedStatement防止sql注入ps.setString(1,\"9990\");ps.setInt(2,55);int count= ps.executeUpdate();Log.e(\"thread\", String.valueOf(count));} catch (SQLException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}finally {if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}}
}
然后是一件很郁闷的事情……整了好久.
因为我在线程用的是死循环 而us表中的id是主键。。导致第一次导入数据后 过5s后 因为主键唯一不可重复 就报了一堆错……然后疯狂改疯狂报错……但实际上是可以的。
id(pk) sal
11 55
2 111111
22 1111
90 55
9990 55
看看这个可怜的一眼就被忽略过去的 E/thread: 1:
E/thread: 1
W/System.err: Wed Jun 10 07:39:58 GMT 2020 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
W/System.err: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘9990’ for key ‘PRIMARY’
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)
W/System.err: at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2675)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1915)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2136)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2070)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5187)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2055)
at com.example.myapplication111.thread.run(thread.java:36)