hi friends !!
i have a values from jsp page .and i want to store that values into DB at the same time i want to show immediatlly on same jsp page with tabular format.
hi friend,
Try the following code, may this will be helpful for you
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Student Record</title> </head> <body> <h2>Student Record</h2> <form action="#" method="post"> <div> <div align="left"> <table> <tr> <td> Select Course <select name="course"> <option value="select">select</option> <option value="MCA">MCA</option> <option value="BCA">BCA</option> </select> </td> <td></td> <td align="right">Enter Roll No</td> <td><input type="text" name="roll"/></td> </tr> </table> </div> <div> <table> <tr> <th>Subject</th> <th>Obtained Marks</th> <th>Full Marks</th> </tr> <tr> <td align="center">C</td> <td align="center"><input type="text" size="5" name="c"/></td> <td align="center">100</td> </tr> <tr> <td align="center">Java</td> <td align="center"><input type="text" size="5" name="java"/></td> <td align="center">100</td> </tr> <tr> <td align="center">.Net</td> <td align="center"><input type="text" size="5" name="net"/></td> <td align="center">100</td> </tr> <tr> <td align="center">VB</td> <td align="center"><input type="text" size="5" name="vb"/></td> <td align="center">100</td> </tr> <tr> <td align="center">DBMS</td> <td align="center"><input type="text" size="5" name="dbms"/></td> <td align="center">100</td> </tr> <tr> <td></td> </tr> <tr> <td></td> </tr> <tr><td></td><td align="center"><input type="submit" value="submit"/></td></tr> </table> </div> </div> </form>
Continue....
<%! Connection con = null; PreparedStatement ps = null; ResultSet rs = null; String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/record"; String user = "root"; String psw = "root"; %> <% String c = request.getParameter("c"); String j = request.getParameter("java"); String n = request.getParameter("net"); String v = request.getParameter("vb"); String d = request.getParameter("dbms"); String course = request.getParameter("course"); String roll = request.getParameter("roll"); if(!(course == null || course.isEmpty())) { if(!(roll == null || roll.isEmpty())) { if(!(c == null || c.isEmpty())) { if(!(j == null || j.isEmpty())) if(!(n == null || n.isEmpty())) if(!(v == null || v.isEmpty())) if(!(d == null || d.isEmpty())) { int cmarks = Integer.parseInt(c); int jmarks = Integer.parseInt(j); int nmarks = Integer.parseInt(n); int vmarks = Integer.parseInt(v); int dmarks = Integer.parseInt(d); int rollNo = Integer.parseInt(roll); int total = cmarks+jmarks+nmarks+vmarks+dmarks; int avg = (total)/5; int percent = avg; String grade =""; if(percent < 40){ grade = "E"; } else if(percent >= 40 && percent <=44){ grade = "D"; } else if(percent >=45 && percent <=49){ grade = "D+"; } else if(percent >=50 && percent <=54){ grade = "C-"; } else if(percent >=55 && percent<=59){ grade = "C"; } else if(percent >=60 && percent <=64){ grade = "C+"; } else if(percent >=65 && percent<=69){ grade = "B-"; } else if(percent >=70 && percent <=74){ grade = "B"; } else if(percent >=75 && percent <=79){ grade = "B+"; } else if(percent >=80 && percent <=84){ grade = "A"; } else if (percent >=85 && percent <=100){ grade = "A+"; } %>
Continue...
<div> <div> <table> <tr> <td align="left"><b>Course</b></td><td></td> <td align="center"><%=request.getParameter("course") %></td> <td align="right"><b>Roll No : </b></td> <td align="center"><%=request.getParameter("roll") %></td> </tr> </table> </div> <div> <table border="1"> <tr> <th>Subject</th> <th>Obtained Marks</th> <th>Full Marks</th> </tr> <tr> <td align="center">C</td> <td align="center"><%=cmarks %></td> <td align="center">100</td> </tr> <tr> <td align="center">Java</td> <td align="center"><%=jmarks %></td> <td align="center">100</td> </tr> <tr> <td align="center">.Net</td> <td align="center"><%=nmarks %></td> <td align="center">100</td> </tr> <tr> <td align="center">VB</td> <td align="center"><%=vmarks %></td> <td align="center">100</td> </tr> <tr> <td align="center">DBMS</td> <td align="center"><%=dmarks %></td> <td align="center">100</td> </tr> <tr> <td align="center"><b>Aggregate Marks</b></td> <td align="center"><%=total %></td> </tr> <tr> <td align="center"><b>Grade</b></td> <td align="center"><%=grade %></td> </tr> </table> </div> </div> Continue...
<% try{ Class.forName(driverName); con = DriverManager.getConnection(url,user,psw); String sql = "insert into sturecord values(?,?,?,?,?,?,?,?)"; ps = con.prepareStatement(sql); ps.setInt(1, rollNo); ps.setString(2, course); ps.setInt(3, cmarks); ps.setInt(4, jmarks); ps.setInt(5, nmarks); ps.setInt(6, vmarks); ps.setInt(7, dmarks); ps.setString(8, grade); ps.executeUpdate(); out.println("Added Successfully"); } catch(Exception e) { out.println(e); } finally { ps.close(); con.close(); } } } } } %> </body> </html>
Ads