i want to create editable grid in jsp for invoice so that i can insert multiple product at a same time.how to create that in jsap, plz reply
We are providing you the code where we have specified only three fields bookid,author and title in the database. Follow these steps to update these fields:
1) Create book.jsp
<%@page import="java.sql.*"%> <form name=myname method=post action="edit.jsp"> <table border="1"> <tr><td></td> <td><b><u>bookid</u></b></td> <td><b><u>Author</u></b></td> <td><b><u>title</u></b></td> </tr> <%try{ Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "root"); ResultSet rs = null; Statement st=null; st=conn.createStatement(); rs = st.executeQuery("select * from book"); int i=0; while(rs.next()){ %> <tr><td><input type="checkbox" name="check<%=i%>" value=<%= rs.getString("bookid") %>></td> <td><%= rs.getString("bookid") %></td> <td><%= rs.getString("author") %></td> <td><%= rs.getString("title") %></td> </tr><% i++; } }catch(SQLException e){ System.out.println(e.getMessage()); } %> </table> <input type="submit"> </form>
2) Create edit.jsp
<%@page import="java.sql.*"%> <%String id[]= new String[10]; for(int i=0;i<10;i++){ id[i]=request.getParameter("check"+i); out.println(id[i]); } %> <form name=myname method=post action="update.jsp"> <table border="1"> <tr> <td><b><u>bookid</u></b></td> <td><b><u>Author</u></b></td> <td><b><u>title</u></b></td> </tr> <%try{ Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "root"); ResultSet rs = null; Statement st=null; st=conn.createStatement(); for(int a=0;a<10;a++){ rs = st.executeQuery("select * from book where bookid='"+id[a]+"'"); while(rs.next()){ %> <tr> <td><input type="text" name="bid" value="<%= rs.getString("bookid") %>"></td> <td><input type="text" name="auth" value="<%= rs.getString("author") %>"></td> <td><input type="text" name="tit" value="<%= rs.getString("title") %>"></td> </tr><% } } }catch(SQLException e){ System.out.println(e.getMessage()); } %> </table> <input type="submit">
3)Create update.jsp
<%@page import="java.sql.*"%> <% String d[] =request.getParameterValues("bid"); if(d !=null) { for(int i=0;i<d.length;i++) { out.println(d[i]); } } String name[] =request.getParameterValues("auth"); if(name !=null) { for(int i=0;i<name.length;i++) { out.println(name[i]); } } String title[] =request.getParameterValues("tit"); if(title !=null) { for(int i=0;i<title.length;i++) { out.println(title[i]); } } %> <% try{ Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "root"); Statement st=null; st=conn.createStatement(); for(int a=0;a<10;a++){ String book_id=d[a]; String author=name[a]; String tit=title[a]; st.executeUpdate("update book set author='"+author+"', title='"+tit+"' where bookid='"+book_id+"'"); } } catch(Exception e){ e.printStackTrace(); } %>
Ads