有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java没有为参数8指定值

我从文件中读取每行文本,并使用动态查询插入数据库。每个文件都是根据表名命名的,因此我获取了用于构建查询的列名和类型,然后只插入值。 当我不得不插入一个空值时,我遇到了问题,看不出我遗漏了什么? 根据我看到的印刷线条8?标记和8个值,但错误消息显示:没有为参数8指定值

生成的Insert语句:

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) 个答案

  1. # 1 楼答案

    split(',')调用的结果items产生的项少于8项时,就会出现这个问题

    您应该通过将i从0迭代到7(包括7)并检查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
    }