yuhsing

Untitled

Dec 12th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import java.sql.*;
  5.  
  6. public class EditStudent extends HttpServlet {
  7.  
  8. public void doGet(HttpServletRequest request,
  9. HttpServletResponse response)
  10. throws ServletException, IOException {
  11. response.setContentType("text/html");
  12. PrintWriter out = response.getWriter();
  13.  
  14. out.println( Global.header("Edit Student Records") );
  15.  
  16. // Read parameters.
  17. String stud_id = request.getParameter("param_id");
  18. if( stud_id != null ){
  19. try
  20. {
  21.  
  22. String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  23. String url = "jdbc:odbc:StudentDB";
  24. String sql ="SELECT * FROM Student " +
  25. "WHERE StudentID = ? ";
  26.  
  27. Class.forName(driver);
  28. Connection con = DriverManager.getConnection(url);
  29. PreparedStatement stm = con.prepareStatement(sql);
  30.  
  31. stm.setString( 1, stud_id );
  32.  
  33. ResultSet rs = stm.executeQuery();
  34.  
  35. int i,x;
  36. String[] data = new String[ Global.sql_data.length ];
  37. if( rs.next() )
  38. for( i = 0; i < Global.sql_data.length; i++ )
  39. data[i] = rs.getString( Global.sql_data[i] );
  40.  
  41.  
  42. out.println("<script type='text/javascript' src='validateForm.js'></script>");
  43. out.println("<form action='EditStudent' method='post' onSubmit='return validateForm();' name='myForm'>");
  44. out.println("<div align=center><table border=0><br/>");
  45.  
  46. for( i = 0; i < Global.sql_data.length-1; i++ ){
  47. out.println("<tr>");
  48. out.println("<td>"+Global.sql_Name[i]+" :</td>");
  49.  
  50. if( i == 3 ){
  51. out.println("<td><p>");
  52. out.println("<label><input type='radio' name='"+Global.sql_data[i]+"' value='M' id='gender_Male' "+( ( data[i].equalsIgnoreCase( "M" ) )?"checked":"" )+" />Male</label>");
  53. out.println("<br/>");
  54. out.println("<label><input type='radio' name='"+Global.sql_data[i]+"' value='F' id='gender_Female' "+( ( data[i].equalsIgnoreCase( "F" ) )?"checked":"" )+"/>Female</label>");
  55. out.println("<br/></p></td>");
  56.  
  57.  
  58. // program
  59. }else if( i == 4 ){
  60. out.println("<td><label>");
  61. out.println("<select name='"+Global.sql_data[i]+"' id='"+Global.sql_data[i]+"'>");
  62. for( x = 0; x < Global.program_data.length; x++ )
  63. out.println("<option value='"+Global.program_data[x]+"' "+( ( data[i].equalsIgnoreCase( Global.program_data[x] ) )?"selected":"" )+">"+Global.program_name[x]+"</option>");
  64. out.println("</select>");
  65. out.println("</label></td>");
  66.  
  67. // year
  68. }else if( i == 5 ){
  69. out.println("<td><label>");
  70. out.println("<select name='"+Global.sql_data[i]+"' id='select2'>");
  71. for( x = 1; x <= 4; x++ )
  72. out.println("<option value='"+x+"' "+( ( Integer.parseInt( data[i] ) == x )?"selected":"" )+" >Year "+x+"</option>");
  73. out.println("</select>");
  74. out.println("</label></td>");
  75.  
  76.  
  77. // hometown
  78. }else if( i == 7 ){
  79. out.println("<td><label>");
  80. out.println("<select name='"+Global.sql_data[i]+"' id='select2'>");
  81. for( x = 0; x < Global.home_data.length; x++ )
  82. out.println("<option value='"+Global.home_data[x]+"' "+( ( data[i].equalsIgnoreCase(Global.home_data[x]) )?"selected":"" )+" >"+Global.home_name[x]+"</option>");
  83. out.println("</select>");
  84. out.println("</label></td>");
  85.  
  86.  
  87. }else{
  88. out.println("<td><input type='text' name='"+Global.sql_data[i]+"' value='"+data[i]+"' "+( ( i == 0 )?"readonly":"" )+" /></td>");
  89. }
  90.  
  91. out.println("</tr>");
  92. }
  93.  
  94. out.println("<tr><td></td><td></td></tr> <tr><td><br/>Decision :</td><td>");
  95.  
  96. out.println("<br/><br/><input type='Submit' value='Update' style='height: 50px; width: 100px'/>");
  97. out.println("<input type='reset' value='Reset' style='height: 50px; width: 100px'/>");
  98. out.println("</td></table></div>");
  99. out.println("</form>");
  100.  
  101. rs.close();
  102. stm.close();
  103. con.close();
  104. }
  105. catch (Exception e)
  106. {
  107. out.println(e.toString());
  108. }
  109.  
  110. }else{
  111. out.println("<h1 align=center>Please ensure you have selected 1 Student from the list.</h1>");
  112. }
  113.  
  114. out.println( Global.footer() );
  115. out.close();
  116. }
  117.  
  118.  
  119.  
  120.  
  121. public void doPost(HttpServletRequest request,
  122. HttpServletResponse response)
  123. throws ServletException, IOException {
  124. response.setContentType("text/html");
  125. PrintWriter out = response.getWriter();
  126.  
  127. out.println( Global.header("Edit Student Records") );
  128.  
  129.  
  130. int i,x,request_year = 0;
  131. String[] request_data = new String[ Global.sql_data.length ];
  132.  
  133. for( i = 0; i < Global.sql_data.length-1; i++ ){
  134. if( i == 5 ) request_year = Integer.parseInt( request.getParameter(Global.sql_data[i]) );
  135. else request_data[i] = request.getParameter(Global.sql_data[i]);
  136. //out.println( "<h3>"+Global.sql_Name[i]+" : "+request_data[i]+"</h3>" );
  137. }
  138.  
  139. java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime() );
  140.  
  141. try
  142. {
  143. String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  144. String url = "jdbc:odbc:StudentDB";
  145. String sql = "UPDATE Student " +
  146. "SET "+Global.sql_data[1]+" = ?, "+Global.sql_data[2]+" = ?, "+Global.sql_data[3]+" = ?, "+Global.sql_data[4]+" = ?, "+Global.sql_data[5]+" = ?, "+Global.sql_data[6]+" = ? , "+Global.sql_data[7]+" = ? , "+Global.sql_data[8]+" = ? " +
  147. "WHERE "+Global.sql_data[0]+" = ? ";
  148.  
  149. Class.forName(driver);
  150. Connection con = DriverManager.getConnection(url);
  151. PreparedStatement stm = con.prepareStatement(sql);
  152.  
  153. stm.setString(1, request_data[1]); // name
  154. stm.setString(2, request_data[2]); // ic
  155. stm.setString(3, request_data[3]); // gender
  156. stm.setString(4, request_data[4]); // prog
  157. stm.setInt(5, request_year ); // year
  158. stm.setString(6, request_data[6]); // phone
  159. stm.setString(7, request_data[7]); // hometown
  160. stm.setDate(8, sqlDate ); // date
  161.  
  162. stm.setString(9, request_data[0]); // id
  163.  
  164. // Execute the UPDATE statement.
  165. // Ignore returned value.
  166. stm.executeUpdate();
  167.  
  168. stm.close();
  169. con.close();
  170.  
  171. out.println("<h1 align='center'>Student updated.</h1>");
  172. }
  173. catch (Exception e)
  174. {
  175. out.println(e.toString());
  176. }
  177.  
  178.  
  179. out.println( Global.footer() );
  180. out.close();
  181. }
  182.  
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment