嘿,我的麻烦遍及许多页面,但没有任何解决方法! 请帮忙!
我经历了很多解决方案,但是没有用!
我尝试遍历其他许多堆栈溢出页面,但它们均无效:这是主要错误:
参数索引超出范围(3>参数数,即2)。
package form;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import database.DB_Connection;
/**
* Servlet implementation class report
*/
@WebServlet("/report")
public class report extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public static String makeDate(String dateString) {
//2019-09-20 10:01:02
String date = dateString;
String finalString;
String[] arrOfStr = date.split("T");
finalString = arrOfStr[0] + " " + arrOfStr[1] + ":00";
System.err.println("I am the final string" + finalString);
return finalString;
}
public static String makeint(String timeString) {
//2019-09-20 10:01:02
String date = timeString;
String finalString;
String[] arrOfStr = date.split(" ");
finalString = arrOfStr[0];
System.err.println("I am the final string" + finalString);
return finalString;
}
public report() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String elements[] = new String[10];
elements[0] = request.getParameter("reportDescription");
elements[1] = request.getParameter("recording");
elements[2] = request.getParameter("Incident");
elements[3] = request.getParameter("date");
elements[4] = request.getParameter("accident");
elements[5] = request.getParameter("address");
elements[6] = request.getParameter("sendEmail");
DB_Connection obj_DB_Connection = new DB_Connection();
Connection connection = obj_DB_Connection.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String query = ("INSERT INTO `Hiho`.`IncidentReports`\n" +
"(\n" +
"`Description`,\n" +
"`RecordingTime`,\n" +
"`IncidentType`,\n" +
"`ReportDate`,\n" +
"`AccidentType`,\n" +
"`Location`,\n" +
"`Email`)\n" +
"VALUES\n" +
"( '?',\n" +
"?,\n" +
"'?', \n" +
"'?',\n" +
"'?',\n" +
"'?',\n" +
"?\n" +
");\n" +
"");
ps = connection.prepareStatement(query);
// for (int i = 0; i < elements[1].length(); i++) {
// if (Character.isDigit(elements[1].charAt(i))) {
// continue;
// }
//
// }
int timeAmount= Integer.parseInt(makeint(elements[1]) );
elements[3] = makeDate(elements[3]);
System.out.println(timeAmount);
System.out.println(elements[3]);
System.out.println(elements);
for (int i = 0; i < elements.length; i++) {
if (elements[i]== null) {
break;
}
else if (i == 1) {
System.out.println("I just printed it up!!!");
ps.setInt(i+1, timeAmount);
}
else if (i==6) {
if (elements[i] == "1") {
System.out.println(elements[i]);
ps.setBoolean(i+1, true);
}
else {
System.out.println(elements[i]);
ps.setBoolean(i+1, false);
}
}
else {
System.out.println("I am here for: " + elements[i] + "and my index is: " );
System.out.println(i);
ps.setString(i+1, elements[i] );
}
}
ps.executeUpdate();
rs = ps.executeQuery();
System.out.println(ps);
System.out.println(rs);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(connection!=null) {
connection.close();
}
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
for (int i = 0; i < elements.length; i++) {
if (elements[i]== null)
break;
System.out.println(elements[i].getClass().getName() + " " + elements[i]);
}
doGet(request, response);
}
}
嘿,我的麻烦遍及许多页面,但没有任何解决方法!请帮忙!
我经历了很多解决方案,但是没有用!
我尝试遍历其他许多堆栈溢出页面,但它们均无效:这是主要错误:
参数索引超出范围(3>参数数,即2)。
答案 0 :(得分:1)
如@andreas所述,更改查询,如下所示:
String query = ("INSERT INTO `Hiho`.`IncidentReports`\n" +
"(\n" +
"`Description`,\n" +
"`RecordingTime`,\n" +
"`IncidentType`,\n" +
"`ReportDate`,\n" +
"`AccidentType`,\n" +
"`Location`,\n" +
"`Email`)\n" +
"VALUES\n" +
"( ?,\n" +
"?,\n" +
"?, \n" +
"?,\n" +
"?,\n" +
"?,\n" +
"?\n" +
");\n" +
"");
此外,也不确定为什么将元素长度定义为10,仅设置7个值,但是遍历所有元素以检查是否为null。为什么不将数组长度定义为7?