I have a problem in editing the database through form because of multivalued attribute it is creating the error and also how to write code for displaying the data when there is multivalued attribute? edit.html
edit.jsp
<%
String accno=request.getParameter("acc");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection
con=java.sql.DriverManager.getConnection("jdbc:odbc:dms","smartlib","smartlib");
java.sql.Statement stmt1=con.createStatement();
java.sql.ResultSet rs1=stmt1.executeQuery("SELECT
a.title,e.author(e.author1,e.author2,e.author3),a.noofcopies,a.category,b.edition,c.pubyear,d.price
FROM bookdet a, edition b, publication c, price d,author_det e where
(a.accno='"+accno+"') and (a.accno=b.accno and a.accno=c.accno and
a.accno=d.accno and a.accno=e.accno)");
%>
<% while(rs1.next()) { %>
<form name="ed" action="http://localhost:8080/examples/jsp/update.jsp"
method="post">
<%
while(rs1.next())
{ %>
<table>
<tr> <td> title </td> <td><input type="text" name="title"value="
<%=rs1.getString(1)%>" /> </td> </tr>
<tr> <td> author1 </td> <td><input type="text" name="author1"value="
<%=rs1.getString(2)%>" /> </td> </tr>
<tr> <td> author2 </td> <td><input type="text" name="author2"value="
<%=rs1.getString(3)%>" /> </td> </tr>
<tr> <td> author3 </td> <td><input type="text" name="author3"value="
<%=rs1.getString(4)%>" /> </td> </tr>
<tr> <td> no<em>of</em>copies</td> <td> <input type="text" name="copies"
value="<%= rs1.getString(5)%>" /> </td> </tr>
<tr> <td> category </td> <td> <input type="text"
name="cat"value="<%=rs1.getString(6)%>" /> </td> </tr>
<tr> <td> edition</td> <td><input type="text" name="edition"
value="<%=rs1.getString(7)%>" /> </td> </tr>
<tr> <td> pub<em>year</td> <td><input type="text"
name="pub</em>year"value="<%=rs1.getString(8)%>" /> </td> </tr>
<tr> <td> price</td> <td><input type="text" price="price"
value="<%=rs1.getString(9)%>" /></td> </tr>
<%} %></p>
<tr><td><input type="submit" value="submit" /> </td> <td> <input type="reset"
value="reset" /></td></tr>
</table>
<input type=hidden name="acc" value="<%=accno%>">
</form>
<%
con.close();
} catch(ClassNotFoundException cnfe){
out.println(cnfe.getMessage());
} catch(java.sql.SQLException sqle){
out.println(sqle.getMessage());
}
%>
update.jsp
<% String accno=request.getParameter("acc");
String title=request.getParameter("title");
String copies=request.getParameter("copies");
String cat=request.getParameter("cat");
String edition=request.getParameter("edition");
String pubyear=request.getParameter("pubyear");
String price=request.getParameter("price");
String author1=request.getParameter("author1");
String author2=request.getParameter("author2");
String author3=request.getParameter("author3");
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection
con=java.sql.DriverManager.getConnection("jdbc:odbc:dms","smartlib","smartlib");
java.sql.Statement stmt1=con.createStatement();
java.sql.Statement stmt2=con.createStatement();
java.sql.Statement stmt3=con.createStatement();
java.sql.Statement stmt4=con.createStatement();
java.sql.Statement stmt5=con.createStatement();
java.sql.Statement stmt6=con.createStatement();
java.sql.Statement stmt7=con.createStatement();
java.sql.Statement stmt8=con.createStatement();
java.sql.Statement stmt9=con.createStatement();
stmt1.executeUpdate("UPDATE book_det Set title='"+title+"' where
accno='"+accno+"'");
stmt2.executeUpdate("UPDATE bookdet Set noof_copies="+copies+" where
accno='"+accno+"'");
stmt3.executeUpdate("UPDATE book_det Set category='"+cat+"' where
accno='"+accno+"'");
stmt4.executeUpdate("UPDATE edition Set edition='"+edition+"' where
accno='"+accno+"'");
stmt5.executeUpdate("UPDATE publication Set pubyear='"+pubyear+"' where
accno='"+accno+"'");
stmt6.executeUpdate("UPDATE price Set price="+price+" where accno='"+accno+"'");
stmt7.executeUpdate("UPDATE author_det Set author1="+author1+" where
accno='"+accno+"'");
stmt8.executeUpdate("UPDATE author_det Set author2="+author2+" where
accno='"+accno+"'");
stmt9.executeUpdate("UPDATE author_det Set author3="+author3+" where
accno='"+accno+"'");
con.close(); } catch(ClassNotFoundException cnfe){cnfe.printStackTrace();
} catch(java.sql.SQLException sqle){
sqle.printStackTrace();
}
%>
JSP edit multiple values
1)editapplication.jsp:
<%@ page import="java.sql.*" %> <html> <form name="form" method="post" action="retcheck.jsp"> <table border="1"> <tr><th></th><th>Name</th><th>Address</th></tr> <% int i=0; 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 data"; st = con.createStatement(); ResultSet rs = st.executeQuery(query); %> <% while(rs.next()){ %> <tr><td><input type="checkbox" value="<%= rs.getString("id")%>" name="check"> <td><%=rs.getString("name")%></td> <td><%=rs.getString("address")%></td> </tr> <% } %> <% } catch(Exception e){ e.printStackTrace(); } %> </table> <input type="submit" value="Edit"> </form> </html>
2)retcheck.jsp:
<%@ page import="java.sql.*" %> <% String id[]=request.getParameterValues("check"); %> <form name="form" method=post action="up.jsp"> <table border=1> <tr><th></th><th>Name</th><th>Address</th></tr> <% Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); ResultSet rs = null; Statement st=null; st=conn.createStatement(); for(int a=0;a<id.length;a++){ rs = st.executeQuery("select * from data where id='"+id[a]+"'"); while(rs.next()){ %> <tr> <td><input type="hidden" name="id" value="<%=rs.getString("id")%>"></td> <td><input type="text" name="name" value="<%=rs.getString("name")%>"></td> <td><input type="text" name="address" value="<%=rs.getString("address")%>"></td></tr> <% } } %> </table> <input type="submit" value="update"> </form>
3)up.jsp:
<%@ page import="java.sql.*" %> <% String id[]=request.getParameterValues("id"); String name[]=request.getParameterValues("name"); String add[]=request.getParameterValues("address"); 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(); for(int i=0;i<id.length;i++){ st.executeUpdate("update data set name='"+name[i]+"',address='"+add[i]+"' where id='"+id[i]+"'"); out.println("Data is updated successfully"); } response.sendRedirect("editapplication.jsp"); %>
Ads