我的目标是将数据从 excel 文件上传到 postgreSQL,该文件的列数可能是随机的
My goal is to upload data from an excel file that may have random number of columns to postgreSQL
示例:我的 excel 包含 10 列,而我的数据库有 15 列,即使有 5 列为空,它仍会上传。
我使用的代码可以上传 excel 但是当单元格为空时会出错。
Tools/language 我正在使用(我认为它们在这里都不重要):xhtml、Java、Tomcat、PostgreSQl、Primefaces
在此先感谢您的帮助。
这是我使用的代码:
public void upload() throws IOException{
String host="localhost";
String port="5432";
String db_name="postgres";
String username="postgres";
String password="postgres";
int max = 1000;
int min = 0;
String dir="C:\Users\User\Desktop\book1.xlsx";
int batchSize = 20;
Connection connection = null;
try {
long start = System.currentTimeMillis();
FileInputStream inputStream = new FileInputStream(dir);
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = firstSheet.iterator();
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+db_name+"", ""+username+"", ""+password+"");
connection.setAutoCommit(false);
String sql = "INSERT INTO survey.tabledata (id,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,surveyid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
int count = 0;
//rowIterator.next(); // skip the header row
while (rowIterator.hasNext()) {
Row nextRow = rowIterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell nextCell = cellIterator.next();
int columnIndex = nextCell.getColumnIndex();
switch (columnIndex) {
case 0:
int col1 = (int) nextCell.getNumericCellValue();
statement.setInt(1,col1);
break;
case 1:
String col2 = nextCell.getStringCellValue();
statement.setString(2, col2);
break;
case 2:
String col3 = nextCell.getStringCellValue();
statement.setString(3, col3);
break;
case 3:
String col4 = nextCell.getStringCellValue();
statement.setString(4, col4);
break;
case 4:
String col5 = nextCell.getStringCellValue();
statement.setString(5, col5);
break;
case 5:
String col6 = nextCell.getStringCellValue();
statement.setString(6, col6);
break;
case 6:
String col7 = nextCell.getStringCellValue();
statement.setString(7, col7);
break;
case 7:
String col8 = nextCell.getStringCellValue();
statement.setString(8, col8);
break;
case 8:
String col9 = nextCell.getStringCellValue();
statement.setString(9, col9);
break;
case 9:
String col10 = nextCell.getStringCellValue();
statement.setString(10, col10);
break;
case 10:
String col11 = nextCell.getStringCellValue();
statement.setString(11, col11);
break;
case 11:
int col12 = (int) nextCell.getNumericCellValue();
statement.setInt(12,col12);
break;
}
}
statement.addBatch();
if (count % batchSize == 0) {
statement.executeBatch();
}
}
// workbook.close();
// execute the remaining queries
statement.executeBatch();
connection.commit();
connection.close();
long end = System.currentTimeMillis();
System.out.printf("Import done in %d ms\n", (end - start));
} catch (IOException ex1) {
System.out.println("Error reading file");
ex1.printStackTrace();
} catch (SQLException ex2) {
System.out.println("Database error");
ex2.printStackTrace();
}
}
它会给你一个错误,因为你正在转换一个 null 或空值。
即使您的错误消息不在您的问题描述中,我猜您的数据库中有一个 null
异常。
你可以这样解决:
String col = nextCell.getStringCellValue();
if(col == null)
{
col = "";
}
statement.setString(1, col);
示例:我的 excel 包含 10 列,而我的数据库有 15 列,即使有 5 列为空,它仍会上传。
我使用的代码可以上传 excel 但是当单元格为空时会出错。
Tools/language 我正在使用(我认为它们在这里都不重要):xhtml、Java、Tomcat、PostgreSQl、Primefaces
在此先感谢您的帮助。
这是我使用的代码:
public void upload() throws IOException{
String host="localhost";
String port="5432";
String db_name="postgres";
String username="postgres";
String password="postgres";
int max = 1000;
int min = 0;
String dir="C:\Users\User\Desktop\book1.xlsx";
int batchSize = 20;
Connection connection = null;
try {
long start = System.currentTimeMillis();
FileInputStream inputStream = new FileInputStream(dir);
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = firstSheet.iterator();
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+db_name+"", ""+username+"", ""+password+"");
connection.setAutoCommit(false);
String sql = "INSERT INTO survey.tabledata (id,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,surveyid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
int count = 0;
//rowIterator.next(); // skip the header row
while (rowIterator.hasNext()) {
Row nextRow = rowIterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell nextCell = cellIterator.next();
int columnIndex = nextCell.getColumnIndex();
switch (columnIndex) {
case 0:
int col1 = (int) nextCell.getNumericCellValue();
statement.setInt(1,col1);
break;
case 1:
String col2 = nextCell.getStringCellValue();
statement.setString(2, col2);
break;
case 2:
String col3 = nextCell.getStringCellValue();
statement.setString(3, col3);
break;
case 3:
String col4 = nextCell.getStringCellValue();
statement.setString(4, col4);
break;
case 4:
String col5 = nextCell.getStringCellValue();
statement.setString(5, col5);
break;
case 5:
String col6 = nextCell.getStringCellValue();
statement.setString(6, col6);
break;
case 6:
String col7 = nextCell.getStringCellValue();
statement.setString(7, col7);
break;
case 7:
String col8 = nextCell.getStringCellValue();
statement.setString(8, col8);
break;
case 8:
String col9 = nextCell.getStringCellValue();
statement.setString(9, col9);
break;
case 9:
String col10 = nextCell.getStringCellValue();
statement.setString(10, col10);
break;
case 10:
String col11 = nextCell.getStringCellValue();
statement.setString(11, col11);
break;
case 11:
int col12 = (int) nextCell.getNumericCellValue();
statement.setInt(12,col12);
break;
}
}
statement.addBatch();
if (count % batchSize == 0) {
statement.executeBatch();
}
}
// workbook.close();
// execute the remaining queries
statement.executeBatch();
connection.commit();
connection.close();
long end = System.currentTimeMillis();
System.out.printf("Import done in %d ms\n", (end - start));
} catch (IOException ex1) {
System.out.println("Error reading file");
ex1.printStackTrace();
} catch (SQLException ex2) {
System.out.println("Database error");
ex2.printStackTrace();
}
}
它会给你一个错误,因为你正在转换一个 null 或空值。
即使您的错误消息不在您的问题描述中,我猜您的数据库中有一个 null
异常。
你可以这样解决:
String col = nextCell.getStringCellValue();
if(col == null)
{
col = "";
}
statement.setString(1, col);