Java-jdbc

Java

# 存储时间字段

  • 插入字段为时间
  • 插入字段为时间戳
Connection conn = ……        //省略部分代码
String sql = "INSERT INTO users(date, datetime) VALUES (?,?)"; 
 //定义插入数据的SQL语句
PreparedStatement ps = conn.prepareStatement(sql);// 实例化PreparedStatement对象
ps.setDate(1, new java.sql.Date(new Date().getTime()));// 给参数赋值
ps.setTimestamp(2, new java.sql.Timestamp(new Date().getTime()));// 给参数赋值
1
2
3
4
5
6

# 预编译 + 批处理

  // 外层需要close connection
  private void executeBatch(List<Integer> list, Connection connection) {
    String sql = "INSERT INTO user(name)VALUES(?)";
    try (
        PreparedStatement ps = connection.prepareStatement(sql);
    ) {
      for (int i = 0; i < list.size(); i++) {
        ps.setLong(1, i);
        ps.addBatch();

        if (i % 500 == 0) {
          ps.executeBatch();
          ps.clearBatch();
          connection.commit();
        }
      }
      ps.executeBatch();
      connection.commit();
    } catch (Exception e) {
      try {
        connection.rollback();
      } catch (SQLException ex) {
        log.error("数据回滚失败! error", ex);
      }
    }
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

参考:
https://blog.csdn.net/qq_44891295/article/details/103795164 (opens new window)