例外は 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;
}
事前に感謝