简体   繁体   English

用于导入 Excel 表然后将其存储到 servlet 中的 DB 的高效程序

[英]Time efficient program for importing an excel sheet and then storing it into DB in servlet

I have written a code to import .xls and store the data into db.我编写了一个代码来导入 .xls 并将数据存储到 db 中。 It works fine for small amount of data ie some hundreds of rows of excel.它适用于少量数据,即数百行 excel。 But if the .xls has say 5000 rows, the importing and storing takes about 1.5 - 2 minutes.但是如果 .xls 有 5000 行,导入和存储大约需要 1.5 - 2 分钟。 Here is the piece of code:这是一段代码:

sql = new StringBuffer("insert into bookdetails(bookno");
for (int i = 0; i < header.length; i++) sql.append("," + header[i]);

sql.append(") values(?");
for (int i = 0; i < header.length; i++) sql.append(",?");

sql.append(")");
System.out.println(sql);
psmt = con.prepareStatement(sql.toString());

columnCount = s.getColumns();

// Reading Individual Row Content
for (int i = 1; i < rowCount; i++) {
  // Get Individual Row

  auto =
      getAutoIncrement(
          con,
          "bookdetails"); // getAutoIncrement() just increments the value(as the name suggests).
  psmt.setString(1, auto);

  // s is sheet
  if (s.getCell(0, 0).getContents().length() != 0) {
    for (int j = 0; j < columnCount; j++) {
      rowData = s.getCell(j, i).getContents();

      System.out.println(
          "Header====" + header[j] + "==rowData[j]==" + rowData + "==i==" + i + "==j==" + j);
      // let's say our excel has 4 columns[sno, hello, asd, column]
      if (header[j].equalsIgnoreCase("sno")) {
        psmt.setString(2, rowData);
      } else if (header[j].equalsIgnoreCase("hello")) {
        psmt.setString(3, rowData);
      } else if (header[j].equalsIgnoreCase("asd")) {
        psmt.setString(4, rowData);
      } else if (header[j].equalsIgnoreCase("column")) {
        psmt.setString(5, rowData);
      }
    }
    psmt.addBatch();
    psmt.executeBatch();
  }
}

what measures can I take to make this storing faster.我可以采取什么措施来加快这种存储速度。
Can I store the .xls sheet all at once temporarily instead of row-by-row(which takes a lot of time).我可以一次性存储 .xls 工作表,而不是逐行存储(这需要很多时间)。 Any suggestions are welcome.欢迎任何建议。

You are doing an executeBatch after each addBatch .您在每个addBatch之后执行executeBatch Execute the batch at the end.最后执行批处理。 It might be better to do not batches of thousands, so below I do batches of 100 (less memory usage and less heavy data transport by the driver).最好不要批量处理数千个,所以在下面我做 100 个批次(更少的内存使用和更少的驱动程序传输的繁重数据)。

int batchSize = 0;

for (int i = 1; i < rowCount; i++) {
  if (batchSize >= 100) {
      batchSize = 0;
      psmt.executeBatch();
      psmt.clearBatch();
  }

  // Get Individual Row

  auto =
      getAutoIncrement(
          con,
          "bookdetails"); // getAutoIncrement() just increments the value(as the name suggests).
  psmt.setString(1, auto);

  // s is sheet
  if (s.getCell(0, 0).getContents().length() != 0) {
    for (int j = 0; j < columnCount; j++) {
      rowData = s.getCell(j, i).getContents();

      System.out.println(
          "Header====" + header[j] + "==rowData[j]==" + rowData + "==i==" + i + "==j==" + j);
      // let's say our excel has 4 columns[sno, hello, asd, column]
      if (header[j].equalsIgnoreCase("sno")) {
        psmt.setString(2, rowData);
      } else if (header[j].equalsIgnoreCase("hello")) {
        psmt.setString(3, rowData);
      } else if (header[j].equalsIgnoreCase("asd")) {
        psmt.setString(4, rowData);
      } else if (header[j].equalsIgnoreCase("column")) {
        psmt.setString(5, rowData);
      }
    }
    psmt.addBatch();
  }
  psmt.executeBatch(); // At the end.
  psmt.close(); // Close
}

Irrelevant, but you should use a StringBuilder io the old, slower StringBuffer .不相关,但您应该使用StringBuilder io 旧的、较慢的StringBuffer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM