我正在创建一个程序,我将从excel文件中读取数据并将它们存储在表中。在我的文件中,一些单元格可能是空白的,我会将它们作为空值存储在我的表中。 在我的程序中,我使用LinkedHashMap读取并将每个单元格存储为字符串。 我已经制作了程序并检查了一个小的excel文件并且工作正常。但是当我测试一个更大的文件时,我得到了这个例外:
SQLException: Column count doesn't match value count at row 1
SQLState: 21S01
VendorError: 1136
填写表格的代码如下:
private static void fillTable(Connection con, String fieldname,
List<TableRow> allData) {
for (int row = 0; row < allData.size(); row++) {
LinkedHashMap<String, Integer> rowData = allData.get(row).tableFields;
Iterator iter = rowData.entrySet().iterator();
String str;
String[] tousFields = new String[rowData.size()];
int i = 0;
while (iter.hasNext()) {
Map.Entry pairs = (Map.Entry) iter.next();
Integer fieldType = (Integer) pairs.getValue();
String fieldValue = (String) pairs.getKey();
switch (fieldType) {
case Cell.CELL_TYPE_NUMERIC:
str = fieldValue;
break;
case Cell.CELL_TYPE_STRING:
str = "\'" + fieldValue + "\'";
break;
case Cell.CELL_TYPE_BOOLEAN:
str = fieldValue;
break;
case Cell.CELL_TYPE_BLANK:
str = "null";
break;
default:
str = "";
break;
}
tousFields[i++] = str;
}
try {
Statement stmt = con.createStatement();
String all = org.apache.commons.lang3.StringUtils.join(
tousFields, ",");
String sql = "INSERT INTO " + fieldname + " VALUES (" + all
+ ")";
stmt.executeUpdate(sql);
System.out.println("Fill table...");
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
}
}
创建表的代码是:
private static String getCreateTable(Connection con, String tablename,
LinkedHashMap<String, Integer> tableFields) {
Iterator iter = tableFields.keySet().iterator();
Iterator cells = tableFields.keySet().iterator();
String str = "";
String[] allFields = new String[tableFields.size()];
int i = 0;
while (iter.hasNext()) {
String fieldName = (String) iter.next();
Integer fieldType = (Integer) tableFields.get(fieldName);
switch (fieldType) {
case Cell.CELL_TYPE_NUMERIC:
str = fieldName + " INTEGER";
break;
case Cell.CELL_TYPE_STRING:
str = fieldName + " VARCHAR(255)";
break;
case Cell.CELL_TYPE_BOOLEAN:
str = fieldName + " INTEGER";
break;
case Cell.CELL_TYPE_BLANK:
str = "null";
break;
default:
break;
}
allFields[i++] = str;
}
try {
Class.forName("com.mysql.jdbc.Driver");
Statement stmt = con.createStatement();
try {
String all = org.apache.commons.lang3.StringUtils.join(
allFields, ",");
String createTableStr = "CREATE TABLE " + tablename + " ("
+ all + ")";
System.out.println("Create a new table in the database");
stmt.executeUpdate(createTableStr);
} catch (SQLException e) {
System.out.println("Error: the table already exists. Please try again " );
}
} catch (Exception e) {
System.out.println(((SQLException) e).getSQLState());
System.out.println(e.getMessage());
e.printStackTrace();
}
return str;
}
有人可以帮我解决我的问题吗?
答案 0 :(得分:0)
当insert子句中的字段数与您在括号之间传递的值的数量不匹配时,会发生此错误。只需在执行错误时打印SQL语句,并检查字段数和值的数量。
答案 1 :(得分:0)
在创建表格时,您正在使用:
case Cell.CELL_TYPE_BLANK:
str = "null";
break;
default:
您要么创建名为“null”的表列,要么根本不创建列。我现在不确定,但我认为没有创建专栏。 Hovewer,在你的INSERT语句中,你建议有固定数量的列,并且存在空列。