Java 重构类似的CSV处理

Java 重构类似的CSV处理,java,jdbc,refactoring,Java,Jdbc,Refactoring,因此,我目前正在从事一个项目,其中包括使用JDBC从几个GZIP压缩的CSV文件更新H2数据库。我注意到,CSV文件的处理过程保持不变。只有我填写PreparedStatement对象的部分才真正不同。以下是我使用的代码片段: InputStream in = getStream(world.getUrl(), true, Configuration.GET_PLAYER_COMPRESSED); Scanner sc = new Scanner(in); PreparedStatement s

因此,我目前正在从事一个项目,其中包括使用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);

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

在基类中

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]));
      }
}

外包?我的意思是把多余的代码打包到一个地方。不知道英语中的确切术语。重构。我已相应地重新标记了你的职位。