当我尝试与我的SQL数据库通信时出现以下错误。此方法从JSP页面获取两个字符串值。代码获取第一个int设置的这个星期日期。 但问题是我正在尝试与数据库通信。它必须是语法错误。我尝试过使用不同的方括号
public String getSpecificSpot(String day, String hour) {
String result = "";
int dayNo= Integer.parseInt(day);
Calendar cal = GregorianCalendar.getInstance();
//System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
// Set the calendar to monday of the current week
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
// Print dates of the current week starting on Monday
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String monday = "";
monday = df.format(cal.getTime());
//System.out.println(monday);
cal.add(Calendar.DATE, dayNo);
String currentDay = df.format(cal.getTime());
String currentDay2= currentDay;
System.out.println("Current DAY: "+currentDay);
boolean found = false;
Connection c = DBHelperClass.getConnection();
String query = "Select * from timetableslot WHERE date BETWEEN ? 00:00:00.00' AND ? 23:59:59.999' and time= ? ";
if (c != null) {
try {
PreparedStatement inserter = c.prepareStatement(query);
inserter.setString(1, currentDay);
inserter.setString(2, currentDay2);
inserter.setString(3, hour);
System.out.println("validating user: " + query);
ResultSet resultSet = inserter.executeQuery();
while (resultSet.next()) {
//result = resultSet.getInt("classID");
result= String.valueOf(resultSet.getInt("slotID"));
System.out.println(result+" result value");
found = true;
}
} catch (SQLException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}
}
//result="ssa";
return result;
}
我得到的错误是:
java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
答案 0 :(得分:0)
您好我在发布问题
之后修复了它public String getSpecificSpot(String day, String hour) {
String result = "";
int dayNo= Integer.parseInt(day);
Calendar cal = GregorianCalendar.getInstance();
//System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
// Set the calendar to monday of the current week
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
// Print dates of the current week starting on Monday
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String monday = "";
monday = df.format(cal.getTime());
//System.out.println(monday);
cal.add(Calendar.DATE, dayNo);
String currentDay = df.format(cal.getTime());
String currentDay2= currentDay;
System.out.println("Current DAY: "+currentDay);
boolean found = false;
Connection c = DBHelperClass.getConnection();
String query = "Select * from timetableslot WHERE date BETWEEN ? AND ? and time= ? ";
if (c != null) {
try {
PreparedStatement inserter = c.prepareStatement(query);
inserter.setString(1, currentDay + " 00:00:00.00");
inserter.setString(2, currentDay2+ " 23:59:59.999");
inserter.setString(3, hour);
System.out.println("validating user: " + query);
ResultSet resultSet = inserter.executeQuery();
while (resultSet.next()) {
//result = resultSet.getInt("classID");
result= String.valueOf(resultSet.getInt("slotID"));
System.out.println(result+" result value");
found = true;
}
} catch (SQLException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}
}
//result="ssa";
return result;
}