How do i store getParameterValues(“”) into an Array List using for loop in Servlet?
How do i store getParameterValues(“”) into an Array List using for loop in Servlet?
There is a for loop to display the days on my "calendar" and users are able to select multiple days, as there will be a checkbox beside each day as shown below.
JSP:
<td class="day_cell"> <%=days[i][j]%> <input type="checkbox" value="<%=days[i][j]%>/<%=monthName%>/<%=intYear%>" name="date"> </td>
The following are the codes in my Servlet. I get the dates from multiple check boxes. How can I save the dates into an arraylist?
Servlet:
Calendar cal = new Calendar();
cal.setDate(request.getParameterValues("date"));
String results = request.getParameterValues("date");
for (int i = 0; i < results.length; i++) {
System.out.print(results[i]);
}
try {
DBAO myDatabase = new DBAO();
myDatabase.updateDate(cal, results);
request.getRequestDispatcher("confirm-schedule.jsp").forward(request,response);
}catch(Exception ex) {
}
Database:
public void updateDate(Calendar cal, String results) {
try {
System.out.println("entering");
String insertStatement = "Insert into uskill.timetable (date)";
insertStatement = insertStatement +"values (?)";
PreparedStatement prepStmt = con.prepareStatement(insertStatement);
prepStmt.setString(1, cal.getDate());
System.out.println("Entered");
System.out.println(prepStmt.toString());
int status = prepStmt.executeUpdate();
if(status != 0) {
System.out.println("Recorded Added");
}
}catch(Exception ex) {
throw new Exception("Error:" + ex.getMessage());
}
}
ERROR:
java.lang.NullPointerException
at database.DBAO.updateDate(DBAO.java:119)
at servlet.CalendarServlet.doPost(CalendarServlet.java:91)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:412)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1385)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Yes i do. Each row have the user’s id depending on how many days they each select.
– glhe13
17 hours ago
Is
cal.getDate()
returns string array or comma seperated values ?– Sumesh TG
17 hours ago
cal.getDate()
public String getDate() { return date; }
should i change it to array list instead?– glhe13
16 hours ago
public String getDate() { return date; }
Use batch operation to insert multiple records in to a table.
– Sumesh TG
14 hours ago
1 Answer
1
public void updateDate(Calendar cal, String results) {
try {
System.out.println("entering");
String insertStatement = "Insert into uskill.timetable (date)";
insertStatement = insertStatement +"values (?)";
PreparedStatement prepStmt = con.prepareStatement(insertStatement);
for(String s:cal.getDate()){
prepStmt.setString(1, s);
prepStmt.addBatch();
}
System.out.println("Entered");
System.out.println(prepStmt.toString());
int status = preparedStatement.executeBatch();;
if(status[0] != 0) {
System.out.println("Recorded Added");
}
}catch(Exception ex) {
throw new Exception("Error:" + ex.getMessage());
}
}
Hope this will help you.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do you want insert multiple row in database ?
– Sumesh TG
20 hours ago