更新时间:2023-11-10 GMT+08:00
分享

插入数据

以Java JDBC方式执行SQL语句在集群的dbName.tableName表中插入数据。

String insertTableSql = "insert into " + dbName + "." + tableName + " values(?, ?, ?)";
private static void insert(Connection connection, String sql) throws Exception {
   int INSERT_BATCH_SIZE = 10;
   try(PreparedStatement stmt = connection.prepareStatement(sql)) {
      for (int i =0; i < INSERT_BATCH_SIZE; i++) {
         stmt.setInt(1, i);
         stmt.setInt(2, i * 10);
         stmt.setString(3, String.valueOf(i * 100));
         stmt.addBatch();
      }
      stmt.executeBatch();
   } catch (Exception e) {
      logger.error("Execute sql {} failed.", sql, e);
      throw new Exception(e);
   }
}
分享:

    相关文档

    相关产品