Issue
I am trying to write a servlet that takes in values from the form and stores it in a table called material. The page is as follows : This is my page.The number of entries can be anything between 1 to 6.
For my test run, I only had a single entry and on clicking submit I get the error "index 6 out of bounds for length 6". Note: A single entry means I fill details of only one material out of 6.
This is how my servlet code looks like. I have used request.getParameterValues to input the form data in arrays.
Class.forName(driver);
Connection connection = DriverManager.getConnection(connectionUrl + database, userid, password);
connection.setAutoCommit(false);
String insertTableSQL = "INSERT INTO material" + " (PassNumber, InitiatingOfficer, staff_id, Materials, Quantity, Unit, Date_of_return) VALUES " + " (?, ?, ?, ?, ?, ?, ?);";
PreparedStatement st = connection.prepareStatement(insertTableSQL);
for(int i=0; i<Materials.length; i++){
st.setString(1, PassNumber);
st.setString(2, InitiatingOfficer);
st.setInt(3, staff_id);
st.setString(4, Materials[i]);
st.setString(5, Quantity[i]);
st.setString(6, Unit[i]);
st.setString(7, Date[i]);
st.addBatch();
}
st.executeBatch();
This is how I have initialised the arrays:
public class raisegatepass extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LoginBean loginBean = new LoginBean();
String PassNumber = generatePIN(); //Pass Number
String InitiatingOfficer = loginBean.getName(); // Name
int staff_id = loginBean.getstaffid(); // Staff ID
String[] Materials = request.getParameterValues("materialInfo"); // Array containing Material Name list
String[] Unit = request.getParameterValues("materialUnit"); // Array containing Unit
String[] Quantity = request.getParameterValues("materialQuantity"); // Array containing Quantity in string
String[] Date = request.getParameterValues("materialDate"); // date of return
Also, is this the right way to enter data into my table whenever I do not know in advance how many rows the user will fill?
Edit: Here is my complete code for the servlet
public class raisegatepass extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LoginBean loginBean = new LoginBean();
String PassNumber = generatePIN(); //Pass Number
String InitiatingOfficer = loginBean.getName(); // Name
int staff_id = loginBean.getstaffid(); // Staff ID
String[] Materials = request.getParameterValues("materialInfo"); // Array containing Material Name list
String[] Unit = request.getParameterValues("materialUnit"); // Array containing Unit
String[] Quantity = request.getParameterValues("materialQuantity"); // Array containing Quantity in string
String[] Date = request.getParameterValues("materialDate"); // date of return
String driver = "com.mysql.jdbc.Driver";
String connectionUrl = //Url
String database = //Database
String userid = //userid
String password = //password
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(connectionUrl + database, userid, password);
connection.setAutoCommit(false);
String insertTableSQL = "INSERT INTO material" + " (PassNumber, InitiatingOfficer, staff_id, Materials, Quantity, Unit, Date_of_return) VALUES " + " (?, ?, ?, ?, ?, ?, ?);";
PreparedStatement st = connection.prepareStatement(insertTableSQL);
for(int i=0; i<Materials.length; i++){
st.setString(1, PassNumber);
st.setString(2, InitiatingOfficer);
st.setInt(3, staff_id);
st.setString(4, Materials[i]);
st.setString(5, Quantity[i]);
st.setString(6, Unit[i]);
st.setString(7, Date[i]);
st.addBatch();
}
st.executeBatch();
} catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
RequestDispatcher view = request.getRequestDispatcher("gatepass_raise.jsp");
view.forward(request, response);
}
public String generatePIN()
{
int x = (int)(Math.random() * 6);
x = x + 1;
Random r = new Random();
char c = (char)(r.nextInt(26) + 'A');
String randomPIN = c + (x + "") + ( ((int)(Math.random()*100)) + "" );
return randomPIN;
}
}
Solution
If the length of all arrays is the same, this code should be fine, but my guess is it´s not the same (only guessing, since no exception stack provided). What do you have in the view (jsp)? Materials possibly having more elements than Unit, Quantity, Date?
To check the length, you can add some debug outputs in the servlet, like that:
String[] Date = request.getParameterValues("materialDate"); // date of return
...
System.out.println("Materials length: " + Materials.length));
System.out.println("Unit length: " + Unit.length));
System.out.println("Quantity length: " + Quantity.length));
System.out.println("Date length: " + Date.length));
Answered By - Serge
Answer Checked By - David Marino (JavaFixing Volunteer)