没有为参数8指定值

时间:2018-01-06 23:31:50

标签: java sql postgresql

我正在从文件中读取每行文本并使用动态查询插入数据库。每个文件都按照表名命名,因此我获取列名称和类型来构建查询,然后只插入值。 当我不得不插入一个NULL值我已经遇到问题,不能看到我失踪了什么? 根据印刷线我看到8?标记和8个值,但错误消息显示:没有为参数8指定值。

生成的插入声明:

INSERT INTO dbo.request_service_log (request_service_log_id, request_service_id, severity_cde, log_cde, log_txt, source_id, message_id, application_id) values(?, ?, ?, ?, ?, ?, ?, ?)

参数值:

23584022,222635,C,10002002,Start Monitor Failed,0,25431082,NULL

代码:

public void insertData(String tableName, List<String> rows) {
    List<String> columnNames = null;
    try{
        //get the column names and types for the specified table
        columnNames = getColumns(tableName, "dbo");
    }
    catch(SQLException se){
        System.out.println(se.getMessage());
    }

    String insertColumns = "";
    String insertValues = "";

    for(int i =0; i < columnNames.size(); i++){
        if(i == 0){
            insertColumns += columnNames.get(i);
        }
        else {
            insertColumns += ", " + columnNames.get(i);
        }
    }

    for(int i = 0; i < columnNames.size(); i++){
        if((i + 1) < columnNames.size()){
            insertValues += "?, ";
        }
        else {
            insertValues += "?";
        }

    }

    String insertSql = "INSERT INTO dbo." + tableName + " (" + insertColumns + ") values(" + insertValues + ")";
    System.out.println(insertSql);
    PreparedStatement ps = null;
    try{
        ps = conn.prepareStatement(insertSql);
        try{
            int index = 0;
            for(String row : rows){
                index++;
                System.out.println(index + ": " + row);
                String[] items = row.split(",");
                for(int i = 0; i < items.length; i++) {
                    if(StringUtils.isInteger(columnTypes.get(i))){
                        if("NULL".equalsIgnoreCase(items[i])){
                            ps.setNull((1 +1), Types.INTEGER);
                        }
                        else {
                            ps.setInt((i + 1), new Integer(items[i]).intValue());
                        }
                    }
                    else if(StringUtils.isVarchar(columnTypes.get(i))) {
                        if("NULL".equalsIgnoreCase(items[i])){
                            ps.setNull((i + 1), Types.VARCHAR);
                        }
                        else {
                            ps.setString((i + 1), items[i]);
                        }
                    }
                    else if(StringUtils.isDatetime((columnTypes.get(i)))){
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
                        java.util.Date date = null;
                        try {
                            date = formatter.parse(items[i]);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        if(date == null){
                            ps.setNull((i +1), Types.TIMESTAMP);
                        }
                        else {
                            ps.setTimestamp((i + 1), new java.sql.Timestamp(date.getTime()));
                        }
                    }
                }
                ps.executeUpdate(); //insert our data
            }

        }
        catch(SQLException se){
            System.out.println(se.getMessage());
        }
    }
    catch(SQLException sqle){
        //do something with it
        System.out.println(sqle.getMessage());
    }
    finally{
        try{
            if(ps != null){
                ps.close();
            }
        }
        catch(SQLException se){
            se.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

itemssplit(',')调用的结果,产生的项目少于8件,就会出现此问题。

您应该通过从0到7(包括0和7)重复i并检查items是否有足够的元素来涵盖i来解决此问题:

for (int i = 0 ; i != 8 ; i++) {
    String item = (items.length() < i) ? items[i] : "NULL";
    ... // Now use item instead of items[i] below
}
相关问题