Guest User

Untitled

a guest
Oct 4th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.sql.*;
  8. import java.io.*;
  9.  
  10. public class MyStudentServlet extends HttpServlet {
  11. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  12. static final String DB_URL = "jdbc:mysql://localhost/studentdb";
  13. static final String USER = "root";
  14. static final String PASS = "student";
  15. static Connection conn = null;
  16. static Statement stmt =null;
  17.  
  18. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  19. throws ServletException, IOException {
  20. try {
  21. Class.forName("com.mysql.jdbc.Driver");
  22.  
  23. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  24. }
  25. catch(ClassNotFoundException | SQLException e) { }
  26. response.setContentType("text/html;charset=UTF-8");
  27. PrintWriter out = response.getWriter();
  28.  
  29. out.println("<!DOCTYPE html>");
  30. out.println("<html>");
  31. out.println("<head>");
  32. out.println("Student Details");
  33. out.println("</head>");
  34. out.println("<body>");
  35. out.println("<table border=1 align=center>");
  36. out.println("<tr>");
  37. out.println("<th>NAME</th>");
  38. out.println("<th>DOB</th>");
  39. out.println("<th>REG_NO</th>");
  40. out.println("<th>BRANCH</th>");
  41. out.println("</tr>");
  42.  
  43. try {
  44. stmt = conn.createStatement();
  45. String sql;
  46. sql = "SELECT * from stud";
  47. ResultSet rs = stmt.executeQuery(sql);
  48.  
  49. while(rs.next()) {
  50. String fn = rs.getString("name");
  51. String regno = rs.getString("regno");
  52. String branch = rs.getString("branch");
  53. String dob = rs.getString("dob");
  54.  
  55. out.println("<tr>");
  56. out.println("<td>");
  57. out.println(fn);
  58. out.println("</td>");
  59. out.println("<td>");
  60. out.println(dob);
  61. out.println("</td>");
  62. out.println("<td>");
  63. out.println(regno);
  64. out.println("</td>");
  65. out.println("<td>");
  66. out.println(branch);
  67. out.println("</td>");
  68. out.println("</tr>");
  69. }
  70.  
  71.  
  72. out.println("</table>");
  73. out.println("</body>");
  74. out.println("</html>");
  75. }
  76. catch(Exception e){}
  77.  
  78. }
  79.  
  80. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  81. /**
  82. * Handles the HTTP <code>GET</code> method.
  83. *
  84. * @param request servlet request
  85. * @param response servlet response
  86. * @throws ServletException if a servlet-specific error occurs
  87. * @throws IOException if an I/O error occurs
  88. */
  89. @Override
  90. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  91. throws ServletException, IOException {
  92. processRequest(request, response);
  93. }
  94.  
  95. /**
  96. * Handles the HTTP <code>POST</code> method.
  97. *
  98. * @param request servlet request
  99. * @param response servlet response
  100. * @throws ServletException if a servlet-specific error occurs
  101. * @throws IOException if an I/O error occurs
  102. */
  103. @Override
  104. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  105. throws ServletException, IOException {
  106. processRequest(request, response);
  107. }
  108.  
  109. /**
  110. * Returns a short description of the servlet.
  111. *
  112. * @return a String containing servlet description
  113. */
  114. @Override
  115. public String getServletInfo() {
  116. return "Short description";
  117. }// </editor-fold>
  118.  
  119. }
Add Comment
Please, Sign In to add comment