0

我正在制作一个程序来从上述excel文件中读取数据并将它们存储在表格中。我设法从excel文件中读取所有数据作为字符串并将它们存储在表中。

ID  Name         Salary   

50  christine   2349000

43  paulina     1245874

54  laura       4587894

23  efi         3456457 

43  jim         4512878

但是在我的项目中,我还有其他几个文件与上面的示例具有相同的空白单元格

ID  Name            Salary     

50  christine       2349000

43  paulina         4587894

23  laura           3456457

43  jim             4512878    

当我运行相同的程序时,我得到了这个异常:

SQLException: Column count doesn't match value count at row 1
SQLState: 21S01
VendorError: 1136

创建表的代码如上:

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;
        default:
            str = "";
            break;
        }
        allFields[i++] = str;
    }
    try {
        Statement stmt = con.createStatement();

        try {
            String all = org.apache.commons.lang3.StringUtils.join(
                    allFields, ",");
            String createTableStr = "CREATE TABLE IF NOT EXISTS "
                    + tablename + " (" + all + ")";

            System.out.println("Create a new table in the database");
            stmt.executeUpdate(createTableStr);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState:     " + e.getSQLState());
            System.out.println("VendorError:  " + e.getErrorCode());
        }
    } catch (Exception e) 
    {
        System.out.println( ((SQLException) e).getSQLState() );
        System.out.println( e.getMessage() );
        e.printStackTrace();
    }
    return str;
}

private static void fillTable(Connection con, String fieldname,
        LinkedHashMap[] tableData) {
    for (int row = 0; row < tableData.length; row++) {
        LinkedHashMap<String, Integer> rowData = tableData[row];
        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;
            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);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        }

    }

    }

我正在使用 HashMap 从 excel 文件中读取所有数据作为字符串并将它们存储在表中。但是由于我每次都不知道列的确切数量,我该如何解决呢?我已经阅读了其他具有特定列数的帖子,它们使用值('',?,?)`等。我将如何在我的程序中做到这一点?我得到的输出如下: 在此处输入图像描述

4

1 回答 1

0

很可能在fieldname变量中,您没有与变量中相同数量的值,all所以基本上这意味着指定的列数与值的数量不相等

此外,所有字符串都必须转义并用单引号括起来。

编辑:从 efoikonom 收到一些附加信息后:

如果您有“空白”值,则需要指定默认值,例如:

 INSERT INTO my_table (
   ID,
   Name,
   Salary
 ) VALUES (
    43,
    'paulina',
    0
 ),(
    0,
    'laura',
    4587894.0
 )

更新:试试这个:

switch (fieldType) {
    case Cell.CELL_TYPE_NUMERIC:
        str = fieldValue;
        if(str.length() == 0){
          str = "0";
        }
        break;
    case Cell.CELL_TYPE_STRING:
        str = "\'" + fieldValue + "\'";
        if(str.length() == 0){
          str = "'none'";
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        str = fieldValue;
        if(str.length() == 0){
          str = "0";
        }
        break;
    default:
        str = "";
        break;
}
于 2013-05-09T07:34:13.000 回答