Guest User

Untitled

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