Although I think you should update your application design, more specifically the way you are getting the data from servlet.
There should be a bean with name e.g. MyBean to hold the data for each entity you will store into database not having separated arrays of attributes:
class MyBean {
String remark;
String quantity;
String startTime; //You said it is a String but maybe should be a timestamp
String endTime;
//Getters & Setters
}
So back to your code now, and you should note that this is a wild guess since I can really figure out how you want the data to go under your tables:
* Do you want to store an array of remarks in a single column?
* Or those remarks stand each for a separate record?
Supposing that you are going for the second approach, your data access layer service can be as below:
public void insertMyEntity(String remark,
String quantity,
String startTime,
String endTime) throws SQLException {
Connection con = ConnectionManager.getConnection();
PreparedStatement insertStatement = null;
String insertString =
"insert into YOUR_TABLE(remarks,quantity,startTime,endTime) values (?,?,?,?)";
try {
insertStatement = con.prepareStatement(insertString);
insertStatement.setString(1, remark);
insertStatement.setString(1, quantity);
insertStatement.setString(1, startTime);// use setTimestamp() if it is a timestamp
insertStatement.setString(1, endTime);
insertStatement.executeUpdate();
} catch (SQLException e ) {
System.out.println(e.getMessage());
} finally {
if (insertStatement != null) {
insertStatement.close();
}
if (con != null) {
con.close();
}
}
}
Then when you have retrieved your data from client request in your controller (Servlet), you can use a loop to iterate over them and insert to database:
public class MySservlet extends HttpServlet
{
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
...
String[] remarks = request.getParameterValues("txtRemarks");
String[] quantity = request.getParameterValues("txtQty");
String[] startHrs = request.getParameterValues("txtStartTime");
String[] endHrs = request.getParameterValues("txtEndtime");
for (int i = 0; i < remarks.length; i++)
{
insertMyEntity(remarks[i], quantity[i], startHrs[i], endHrs[i]);
}
response.sendRedirect("resourceuri");//replace the resource with servlet, jsp or html URL
}
}