-4

例外是在 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 */ public class Project {

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;
}

提前谢谢

4

1 回答 1

0

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

于 2013-07-29T15:48:48.880 回答