java - Can't insert to database jsp -
i can't insert or edit records this.
my sqlexception has error "unreachable catch block sqlexception. exception never thrown try statement body"
new java learning process
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string forward = ""; string action = request.getparameter("action"); if (action.equalsignorecase("delete")) { int surveyid = integer.parseint(request.getparameter("survey_id")); firstdao.removefirst(surveyid); forward = list_first; try { request.setattribute("firsts", firstdao.getfirst()); } catch (sqlexception e) { e.printstacktrace(); } } else if (action.equalsignorecase("edit")) { forward = insert_or_edit; int surveyid = integer.parseint(request.getparameter("survey_id")); try { first first = firstdao.getfirstbyid(surveyid); request.setattribute("first", first); } catch (sqlexception e) { e.printstacktrace(); } } else if (action.equalsignorecase("listfirst")) { forward = list_first; try { request.setattribute("firsts", firstdao.getfirst()); } catch (sqlexception e) { e.printstacktrace(); } } else { forward = insert_or_edit; } requestdispatcher view = request.getrequestdispatcher(forward); view.forward(request, response); }
firstcontroller.java
private static final long serialversionuid = 1l; private static string insert_or_edit = "/first.jsp"; private static string list_first = "/listfirst.jsp"; private firstdao firstdao; public firstcontroller() { super(); firstdao = new firstdao(); }
firstdao.java
public void addfirst(first first) { try { string query = "insert survey_data_27 (uname, p1q1, p1q2, p1q3, p1q4) values('"+ first.getuname() +"', '"+ first.getp1q1() +"', '"+ first.getp1q2() +"', '"+ first.getp1q3() +"', '"+ first.getp1q4() +"')"; statement stmt = connection.createstatement(); stmt.executeupdate(query); } catch (sqlexception e) { e.printstacktrace(); } }
since catch sql-exception in dao class not visible in servlet. that's why can not catch there. have removen
try { ... } catch (sqlexception e) { e.printstacktrace(); }
blocks servlet.
also should use prepared statements.
Comments
Post a Comment