有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java重构类似的CSV处理

所以我目前正在做一个项目,包括使用JDBC从几个GZIP压缩的CSV文件更新H2数据库。我注意到,对CSV文件的处理保持不变。只有我填充PreparedStatement对象的部分真正不同。以下是我使用的代码片段:

InputStream in = getStream(world.getUrl(), true, Configuration.GET_PLAYER_COMPRESSED);
Scanner sc = new Scanner(in);
PreparedStatement stmt = con.prepareStatement(SQL_UPDATE_PLAYER);

String[] line;
int i = 0;
while (sc.hasNextLine()) {
  try {
      line = sc.nextLine().split(",");

    { //this code differs from the rest
      stmt.setString(1, world.getIdentifier());
      stmt.setInt(2, Integer.valueOf(line[0]));
      stmt.setString(3, URLDecoder.decode(line[1], "UTF-8"));
      if ("0".equals(line[2])) {
          stmt.setNull(4, Types.INTEGER);
      } else {
          stmt.setInt(4, Integer.valueOf(line[2]));
      }
    }
    stmt.addBatch();
    if (++i >= 1000) {
        stmt.executeBatch(); //execute batch every 1000 entries
        i=0;
    }
  } catch (Exception e) {
      e.printStackTrace();
  }
}
stmt.executeBatch();
stmt.close();
sc.close();

通过如下示例中的回调对象访问此问题是一种好方法吗

  Callback callback  = new Callback() {

  protected int i;
  protected PreparedStatement stmt;

  //called before processing
  public void preProcess() {
    int i = 0;
    //initialisation or whatever
  }

  //called on every line
  public void processLine(String[] line) {
    stmt.setString(1, world.getIdentifier());
    // [...]
    stmt.addBatch()
    if(++i >= 1000) {
      stmt.executeBatch();
      i = 0;
    }
  }

  //called after processing lines
  protected void postProcess() {
    stmt.executeBatch();
  }
}

processCsv(inputStream, callback);

共 (1) 个答案

  1. # 1 楼答案

    我建议使用模板方法。为每种类型的预处理语句创建处理对象的子类

    在基础课上

    protected abstract void processLine(PreparedStatement stmt, String[] line)
        throws SQLException, UnsupportedEncodingException;
    
    public void parse() throws SQLException
    {
     InputStream in = getStream(world.getUrl(), true, Configuration.GET_PLAYER_COMPRESSED);
     Scanner sc = new Scanner(in);
     PreparedStatement stmt = con.prepareStatement(SQL_UPDATE_PLAYER);
    
     String[] line;
     int i = 0;
     while (sc.hasNextLine()) {
       try {
           line = sc.nextLine().split(",");
    
         {
             processLine(stmt, line);
         }
         stmt.addBatch();
         if (++i >= 1000) {
             stmt.executeBatch(); //execute batch every 1000 entries
             i=0;
         }
       } catch (Exception e) {
           e.printStackTrace();
       }
     }
     stmt.executeBatch();
     stmt.close();
     sc.close();
     }
    

    在上面示例中的子类中

    protected void processLine(PreparedStatement stmt, String[] line)
            throws SQLException, UnsupportedEncodingException
    {
        //this code differs from the rest
          stmt.setString(1, world.getIdentifier());
          stmt.setInt(2, Integer.valueOf(line[0]));
          stmt.setString(3, URLDecoder.decode(line[1], "UTF-8"));
          if ("0".equals(line[2])) {
           stmt.setNull(4, Types.INTEGER);
          } else {
           stmt.setInt(4, Integer.valueOf(line[2]));
          }
    }