参数索引超出范围(2>参数个数,为0)

时间:2013-07-29 08:26:45

标签: java jsp jdbc

异常就在prepstmt.setString(2,shot_paths.get(i))附近; 这里shot_paths是一个字符串列表 抛出异常:

java.sql.SQLException:参数索引超出范围(2>参数个数,为0)。

这是我的代码 调试后,我发现了prepstmt.setString(2,shot_paths.get(i))中出现意外的问题。我无法理解这对我来说是个新问题。

package repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/ **     *     * @author Sanjeev     * /      公共课程项目{

private String project_title, level, source_path, binary_path, report_path,       abstract_path,
        completion_date, registration_date, prog_language, description, official_url,      categories;
private int year, no_of_members, no_of_shots, supervisor_id, co_supervisor_id, programme_ID;
private List<String> members = new ArrayList<String>();
private List<String> shot_paths = new ArrayList<String>();
private List<String> all_items = new ArrayList<String>();
//    private Statement stmt = null;
private PreparedStatement prepstmt = null;
private Connection con = null;
Connect connect = new Connect();

public Project() {
    con = connect.getConnection();
}

public int insertToDatabase(List<String> list, String user_type) {
    try {
        //get Details 
        getDetails(list);

        //insert into project
        int newProjectID = getNewId("project");
        String query = "INSERT INTO project (ID, title, source_path, binary_path, level, level_year,"
                + " submission_date, registration_date, programme_ID, categories, language, description, official_url, approved) "
                + " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        prepstmt = con.prepareStatement(query);
        //setting values
        prepstmt.setInt(1, newProjectID);
        prepstmt.setString(2, project_title);
        prepstmt.setString(3, source_path);
        prepstmt.setString(4, binary_path);
        prepstmt.setString(5, level);
        prepstmt.setInt(6, year);
        prepstmt.setString(7, completion_date);
        prepstmt.setString(8, registration_date);
        prepstmt.setInt(9, programme_ID);
        prepstmt.setString(10, categories);
        prepstmt.setString(11, prog_language);
        prepstmt.setString(12, description);
        prepstmt.setString(13, official_url);
        if (user_type.equals("admin")) {
            prepstmt.setString(14, "true");
        } else {
            prepstmt.setString(14, "false");
        }

        //execute update
        prepstmt.executeUpdate();

        //insert report
        int newDocID = getNewId("document");
        query = "INSERT INTO document(ID, path, report_type_ID, project_ID) "
                + " VALUES(?, ?, ?, ?)";
        prepstmt = con.prepareStatement(query);
        //select values
        prepstmt.setInt(1, newDocID);
        prepstmt.setString(2, report_path);
        prepstmt.setInt(3, 2);
        prepstmt.setInt(4, newProjectID);
        //execute Update
        prepstmt.executeUpdate();

        //insert into teacher_in_project
        query = "INSERT INTO teacher_in_project(teacher_ID, project_ID, is_supervisor) "
                + "VALUES(?, ?, ?)";
        prepstmt = con.prepareStatement(query);
        //insert supervisor
        //select values
        prepstmt.setInt(1, supervisor_id);
        prepstmt.setInt(2, newProjectID);
        prepstmt.setInt(3, 1);
        //execute Update
        prepstmt.executeUpdate();

        //insert co-supervisor
        //select values
        prepstmt.setInt(1, co_supervisor_id);
        prepstmt.setInt(2, newProjectID);
        prepstmt.setInt(3, 0);
        //execute Update
        prepstmt.executeUpdate();

        //insert into student_in_group
        query = "INSERT INTO student_in_project(student_ID, project_ID)"
                + " VALUES(?, ?)";
        prepstmt = con.prepareStatement(query);
        for (int i = 0; i < members.size(); i++) {
            //set values
            prepstmt.setString(1, members.get(i));
            prepstmt.setInt(2, newProjectID);
            //execute Update
            prepstmt.executeUpdate();
        }

        //insert into image and image_in_project 
        query = "INSERT INTO image(ID, path, project_ID) VALUES(?, ?, ?)";            
        prepstmt = con.prepareStatement(query);
        for (int i = 0; i < shot_paths.size(); i++) {                
            //set values
            prepstmt.setInt(1, getNewId("image"));
            prepstmt.setString(2, shot_paths.get(i)); //exception point
            prepstmt.setInt(3, newProjectID);
            //execute Update
            prepstmt.executeUpdate();
        }

        return newProjectID;

    } catch (SQLException ex) {
        System.out.print(ex);
        return 0;
    }
}

public int getNewId(String tableName) {
    int newID = 0;
    try {
        String sql = "SELECT MAX(ID) FROM " + tableName + " ";
        prepstmt = con.prepareStatement(sql);
        //set values
  //            prepstmt.setString(programme_ID, sql);
        ResultSet rs = prepstmt.executeQuery(sql);
        rs.next();
        int count = rs.getInt(1);
        if (count > 0) {
            newID = count + 1;
        } else {
            newID = 1;
        }
    } catch (SQLException ex) {
        System.out.println(ex);
    }
    return newID;
}
提前

thanx

1 个答案:

答案 0 :(得分:0)

我看不到你为shot_paths设置值的位置。在尝试访问它之前,请确保它具有值。 此外,准备好的陈述似乎并不认为有任何参数。我会将代码分解成更小的块(这使我更容易看到正在发生的事情)和/或调试它。如果你这样做,我相信你会看到一些有用的东西 最后,我发现有不同的准备语句很有用。以下是描述类似问题的链接:  jdbc error Parameter index out of range (2 > number of parameters, which is 1)