Can't update the database after click on the submit button in servlet jsp -
i can't update database after clicking on submit button in servlet jsp.
index.jsp
<%@page import="java.sql.*"%> <%@page import="java.sql.drivermanager"%> <%@page import="java.sql.connection"%> <%@page contenttype="text/html" pageencoding="utf-8"%> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>jsp page</title> </head> <body> <% try{ class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/xigmapro","root",""); statement st=con.createstatement(); resultset rs1=st.executequery("select * user_information"); %> <table border="1"> <tr> <td width="120">user name</td><td width="120">user address</td> <td width="120">user city</td><td width="120">user state</td> <td width="120">user mail</td><td width="120">user phone</td> <td width="120">user post</td><td width="120">user country</td> <td width="120">action</td> </tr> <% while(rs1.next()) { string uid=rs1.getstring("client_id"); out.println(uid); %> <tr><td width="120"><% out.print(rs1.getstring("client_name"));%></td><td width="120"><% out.print(rs1.getstring("client_address")); %></td><td width="120"> <% out.print(rs1.getstring("client_city")); %></td> <td width="120"><% out.print(rs1.getstring("client_state")); %></td><td width="120"><% out.print(rs1.getstring("client_mail")); %></td> <td width="120"><% out.print(rs1.getstring("client_phone")); %></td><td width="120"><% out.print(rs1.getstring("client_post")); %></td><td width="120"><% out.print(rs1.getstring("client_country")); %></td> <td width="190"><form action="newservlet" method="post"> <input type="hidden" name="uid" value="<%=uid %>"/> <input type="submit" name="send" value="update"> </form>||<form action="newservlet" method="post"> <input type="hidden" name="uid" value="<%=uid %>"/> <input type="submit" name="send" value="delete"> </form></td> </tr> <% }%> </table> <% }catch(exception e) { out.println(e); } %> </body> </html>
newsevlet.java
import java.awt.choice; import java.lang.*; import java.io.ioexception; import java.io.printwriter; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.sql.*; import java.sql.drivermanager; import java.sql.connection; import javax.servlet.requestdispatcher; import javax.servlet.http.httpsession; import org.apache.catalina.session; public class newservlet extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); try { } catch (exception e) { out.println(e); } } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); string uid = request.getparameter("uid"); string choice = request.getparameter("send"); try { class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/xigmapro", "root", ""); statement st = con.createstatement(); if (choice.equals("add")) { string uid = request.getparameter("uid"); out.println(uid); statement st1 = con.createstatement(); st1.executeupdate("update user_information set" + "`client_name`='" + request.getparameter("user_name") + "',`client_address`='" + request.getparameter("user_add") + "',`client_city`='" + request.getparameter("user_city") + "'where `client_id`=uid"); //response.sendredirect("index.jsp"); out.println("update user_information set " + "`client_name`='" + request.getparameter("user_name") + "',`client_address`='" + request.getparameter("user_add") + "',`client_city`='" + request.getparameter("user_city") + "' `client_id`='uid'"); } if (choice.equals("delete")) { st.executeupdate("delete user_information `client_id`='" + uid + "'"); out.println("user deleted database successfully"); //requestdispatcher rd=request.getrequestdispatcher("next.jsp"); //rd.include(request, response); } if (choice.equals("update")) { preparedstatement ps = con.preparestatement("select *from xigmapro.user_information client_id = ?"); ps.setstring(1, uid); resultset rs = ps.executequery(); if (rs.next()) { string uid = rs.getstring("client_id"); out.println(uid); out.println("<form action='' method='post'>"); out.println("<input type='hidden' name='uid' value = '" + rs.getstring("client_id") + "' / >"); out.println("<table border='1' width='280px' height='120px'>"); out.println("<tr width='280px'><td width='180'>user name</td><td width='120'>user address</td > <td width = '180'>user city</td > <td width = '180'>user state</td >" + "<td width='120'>user mail</td><td width='180'>user phone</td><td width='120'>user country</td > < / tr >"); out.println("<tr><td>"); out.print("<input type='text' name='user_name' value = '" + rs.getstring("client_name") + "' / >" + " < /td>"); out.print("<td>" + "<input type='text' name='user_add' value = '" + rs.getstring("client_address") + "' / >" + " < /td>"); out.print("<td>" + "<input type='text' name='user_city' value = '" + rs.getstring("client_city") + "' / >" + " < /td>"); out.print("<td>" + "<input type='text' name='user_state' value = '" + rs.getstring("client_state") + "' / >" + " < /td>"); out.print("<td>" + "<input type='text' name='user_mail' value = '" + rs.getstring("client_mail") + "' / >" + " < /td>"); out.print("<td>" + "<input type='text' name='user_phone' value = '" + rs.getstring("client_phone") + "' / >" + " < /td>"); out.print("<td>" + "<input type='text' name='user_contry' value = '" + rs.getstring("client_country") + "' / >" + " < /td>"); out.print("</td></tr>"); out.println("</td></tr></table>"); out.println("<input type='submit' name='send' value='add'>"); out.println("</form>"); } } } catch (exception e) { } } @override public string getservletinfo() { return "short description"; } }
i have created database successfully, connection database successful. when click on "add" button of newsevlet.java
page can't update table. after click on "add" submit button, can see particular user id send hidden field, can't update table. query ok. index.jsp
first page , newservlet.java
second page.
you missed spaces after set
, before where
in line: st1.executeupdate("update user_information...
set" + " `client_name` "'
and strong recommend validate input data , use prepared statement.
Comments
Post a Comment