java - 用于导入 excel 工作表然后将其存储到 servlet 中的 DB 的省时程序
问题描述
我编写了一个代码来导入 .xls 并将数据存储到数据库中。它适用于少量数据,即数百行 excel。但如果 .xls 有 5000 行,则导入和存储大约需要 1.5 - 2 分钟。这是一段代码:
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();
}
}
我可以采取什么措施来加快这种存储速度。
我可以一次临时存储 .xls 工作表而不是逐行存储(这需要很多时间)。
欢迎任何建议。
解决方案
你正在做一个executeBatch
after each addBatch
。最后执行批处理。最好不要批量处理数千个,所以下面我批量处理 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
}
无关紧要,但您应该使用StringBuilder
io 旧的、较慢的StringBuffer
.
推荐阅读
- python - 如何在抽象语法树中检索文件名和模块名?
- java - 在改变速度时保持 javafx 视频播放器中的音频音高不变
- matomo - Piwik (Matomo) 在单个会话中创建多次访问
- model-view-controller - mvc项目中的三星指纹识别器
- python - 在 php/python 中仅打印表格输出的第一列
- odoo-view - 视图侧 ir.ui.menu.action 的值错误
- visual-studio-code - 如何在不同长度的行之后将多行选择粘贴为对齐的直列?
- javascript - typeof string 怎么不是 instanceof String?
- reactjs - React-Redux 操作被分派了两次
- android - 如何在 React Native 应用程序中从其他本机应用程序内容提供者获取数据