java - update text field using servlet -
i'm newbie in java , i'm trying update textfield using servlet.insert , delete done update not work.i'm working html , servlet. here servlet
computercontroller.java
protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); computerdao comdao = new computerdao(); //get service string service = request.getparameter("service"); if (service == null || service == "") { service = "listallcomputer"; } if (service.equals("addcomputer")) { //get parameter string name = request.getparameter("cname"); string quan = request.getparameter("quantity"); string price = request.getparameter("price"); string func = request.getparameter("functions"); //check invalid here computer com = new computer(0, name, integer.parseint(quan), double.parsedouble(price), func); //add db int n = comdao.addcomputer(com); if (n > 0) { out.println("<h1> inserted</h1>"); } } if (service.equalsignorecase("listallcomputer")) { arraylist<computer> arr = comdao.getallcomputer("select * computer"); if (arr.size() == 0) { out.println("<h1> no record found </h1>"); } else { out.println(" <table width='100%' border='1'>"); out.println("<caption>"); out.println(" <h2>computer list</h2>"); out.println(" </caption>"); out.println("<tr>"); out.println(" <th scope='col' width='5%'>id</th>"); out.println(" <th scope='col'width='30%'>computer name</th>"); out.println(" <th scope='col'width='10%'>quantity</th>"); out.println(" <th scope='col'width='15%'>price</th>"); out.println(" <th scope='col'width='30%'>functions</th>"); out.println(" <th scope='col' colspan=2 width='10%'>action</th>"); out.println(" </tr>"); (int = 0; < arr.size(); i++) { computer com = arr.get(i); out.println("<tr>"); out.println(" <td>" + com.getcid() + "</td>"); out.println(" <td>" + com.getcname() + "</td>"); out.println(" <td>" + com.getquantity() + "</td>"); out.println(" <td>" + com.getprice() + "</td>"); out.println(" <td>" + com.getfunc() + "</td>"); out.println(" <td><a href = computercontroller?service=update&id=" + com.getcid() + ">update</a></td>"); out.println(" <td><a onclick= \"return confirm('are sure want delete item?');\" href=computercontroller?service=delete&id=" + com.getcid() + ">delete</a></td>"); out.println(" </tr>"); } out.println("</table>"); out.println("<a href= index.jsp>back main page</a>"); } } if (service.equalsignorecase("delete")) { string deleteid = request.getparameter("id"); try { if (deleteid != null) { int deleteidint = integer.parseint(deleteid); int isok = comdao.removecomputer(deleteidint); if (isok != 0) { out.print("deleted computer id = " + deleteidint); } else { out.print("error"); } } } catch (exception ex) { out.print(ex.getmessage()); } } if (service.equalsignorecase("update")) { out.println(" <html>"); out.println("<body>"); out.println("<form id='form1' name='form1' method='post' action='computercontroller' style='width: 500px; margin: 20px auto 0 auto;'>"); out.println("<h2>update computer</h2>"); out.println(" <table width='100%' border='0'>"); out.println("<tr>"); out.println(" <th width='28%' scope='row'>computer name</th>"); out.println(" <td width='72%'><input type='text' name='cnameupdate' id='cnameupdate' /></td>"); out.println(" </tr>"); out.println(" <tr>"); out.println(" <th scope='row'>quantity</th>"); out.println(" <td><input type='text' name='quanupdate' id='quanupdate' /></td>"); out.println("</tr>"); out.println("<tr>"); out.println(" <th scope='row'>price</th>"); out.println(" <td><input type='text' name='priceupdate' id='priceupdate' /></td>"); out.println("</tr>"); out.println("<tr>"); out.println(" <th scope='row'>functions</th>"); out.println(" <td><textarea rows='4' name='funcupdate' id='funcupdate'></textarea></td>"); out.println(" </tr>"); out.println("<tr>"); out.println(" <th scope='row'> </th>"); out.println(" <td><input type='submit' name='button' id='button' value='update' />"); out.println(" <input name='updatecomputer' type='hidden' id='updatecomputer' value='updatecomputer' /></td>"); out.println(" </tr>"); out.println(" </table>"); out.println(" </form>"); out.println(" </body>"); out.println("</html>"); } if (service.equalsignorecase("updatecomputer")) { string uid = request.getparameter("cid"); string uname = request.getparameter("cname"); string uquan = request.getparameter("quantity"); string uprice = request.getparameter("price"); string ufunc = request.getparameter("functions"); computer com = new computer(integer.parseint(uid), uname, integer.parseint(uquan), double.parsedouble(uprice), ufunc); int n = comdao.updatecomputer(com); if (n > 0) { out.print("<h2>updated</h2>"); } } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); }
here dao.i think no problem in file
computerdao.java
public int addcomputer(computer com) { int n = 0; try { statement add = conn.createstatement(); string sql = "insert computer(cname,quantity,price,functions)" + "values(?,?,?,?)"; preparedstatement pre = conn.preparestatement(sql); pre.setstring(1, com.getcname()); pre.setint(2, com.getquantity()); pre.setdouble(3, com.getprice()); pre.setstring(4, com.getfunc()); n = pre.executeupdate(); } catch (sqlexception ex) { logger.getlogger(computerdao.class.getname()).log(level.severe, null, ex); } return n; } public resultset getdata(string sql) { try { state = conn.createstatement(resultset.type_scroll_insensitive, resultset.concur_read_only); rs = state.executequery(sql); } catch (sqlexception ex) { logger.getlogger(computerdao.class.getname()).log(level.severe, null, ex); } return rs; } public arraylist<computer> getallcomputer(string sql) { arraylist<computer> arr = new arraylist<computer>(); try { state = conn.createstatement(resultset.type_scroll_insensitive, resultset.concur_read_only); rs = state.executequery(sql); while (rs.next()) { int id = rs.getint("cid"); string cname = rs.getstring(2); int quan = rs.getint(3); double price = rs.getdouble(4); string func = rs.getstring(5); computer com = new computer(id, cname, quan, price, func); arr.add(com); } } catch (sqlexception ex) { logger.getlogger(computerdao.class.getname()).log(level.severe, null, ex); } return arr; } public int removecomputer(int id) { int n = 0; try { //code here string query = "delete computer cid = '" + id + "'"; statement st = conn.createstatement(); int rs = st.executeupdate(query); return rs; } catch (sqlexception ex) { logger.getlogger(computerdao.class.getname()).log(level.severe, null, ex); } return n; } public int updatecomputer(computer com) { int n = 0; try { //code here string query = "update computer set cname ='" + com.getcname() + "', quantity = " + com.getquantity() + ", price = '" + com.getprice() + ", functions = '" + com.getfunc() + "' cid = " + com.getcid() + ""; statement st = conn.createstatement(); int rs = st.executeupdate(query); return rs; } catch (sqlexception ex) { system.out.println(ex.getmessage()); logger.getlogger(computerdao.class.getname()).log(level.severe, null, ex); } return n; }
you suppose set values controllers when write update page. this:
out.println("<input name='updatecomputer' type='hidden' id='updatecomputer' value='"+compid+"'/></td>");
take note @ value='"+compid+"'"
in above code. don't see anywhere in code setting values on form. unless if missing something.
Comments
Post a Comment