<%@ page import="java.io.*" %> <%@ page import="java.util.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.StringTokenizer" %> <%@ page import="java.lang.*" %> <% String sn=request.getParameter("pname1"); int quan=Integer.parseInt(request.getParameter("quan")); float rate=Float.parseFloat(request.getParameter("rate")); String pdate=request.getParameter("pdate"); out.println(sn); out.println(quan); out.println(rate); out.println(pdate); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:diagnostic"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select quan from stockdet where pname='"+sn+"'"); int a=0; float b=0; while(rs.next()) { a=rs.getInt(1); } out.println(a); int b1=a+quan; out.println(b1); st.executeUpdate("update stockdetails set quan='"+b1+"',rate='"+rate+"',pdate='"+pdate+"' where pname='"+sn+"'"); out.println("Executed Succesfully"); //response.sendRedirect("index1.html"); } catch (Exception e) { out.println(e); } %>
Is there Any error...........In first Page I use ajax for displaying the data from database and edit that data...that edit data is used in the above code........in the above code iam getting all the data that present in previous page....while updating it is not updated and also in resultset am getting quantity but it is nt getting..........please solve this....................
The given code displays all the database data into html table.And there is a button at every row to allow the user to edit that particular row. The button will call the function and display the editing row in another page to update that row. The user will then easily update the record.
1)application.jsp:
<%@ page import="java.sql.*" %> <html> <head> <script language="javascript"> function editRecord(id){ var f=document.form; f.method="post"; f.action='edit.jsp?id='+id; f.submit(); } </script> </head> <body> <br><br> <form method="post" name="form"> <table border="1"> <tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr> <% Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "test"; String driver = "com.mysql.jdbc.Driver"; String userName ="root"; String password="root"; int sumcount=0; Statement st; try{ Class.forName(driver).newInstance(); con = DriverManager.getConnection(url+db,userName,password); String query = "select * from employee"; st = con.createStatement(); ResultSet rs = st.executeQuery(query); %> <% while(rs.next()){ %> <tr><td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getString(4)%></td> <td><%=rs.getString(5)%></td> <td><input type="button" name="edit" value="Edit" style="background-color:green;font-weight:bold;color:white;" onclick="editRecord(<%=rs.getString(1)%>);" ></td> </tr> <% } %> <% } catch(Exception e){ e.printStackTrace(); } %> </table> </form> </body> </html>
continue..
2)edit.jsp:
<%@page language="java"%> <%@page import="java.sql.*"%> <form method="post" action="update.jsp"> <table border="1"> <tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr> <% String id=request.getParameter("id"); int no=Integer.parseInt(id); int sumcount=0; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); String query = "select * from employee where id='"+no+"'"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(query); while(rs.next()){ %> <tr> <td><input type="text" name="name" value="<%=rs.getString("name")%>"></td> <td><input type="text" name="address" value="<%=rs.getString("address")%>"></td> <td><input type="text" name="contact" value="<%=rs.getInt("contactNo")%>"></td> <td><input type="text" name="email" value="<%=rs.getString("email")%>"></td> <td><input type="hidden" name="id" value="<%=rs.getString(1)%>"></td> </tr> <tr> <td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td> </tr> <% } } catch(Exception e){} %> </table> </form>
3)update.jsp:
<%@page import="java.sql.*"%> <% String ide=request.getParameter("id"); int num=Integer.parseInt(ide); String name=request.getParameter("name"); String address=request.getParameter("address"); int contact=Integer.parseInt(request.getParameter("contact")); String email=request.getParameter("email"); try{ Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); Statement st=null; st=conn.createStatement(); st.executeUpdate("update employee set name='"+name+"',address='"+address+"',contactNo="+contact+",email='"+email+"' where id='"+num+"'"); response.sendRedirect("/examples/jsp/application.jsp"); } catch(Exception e){ System.out.println(e); } %>
Ads