Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. JSP
  2.  
  3. <%@page import="java.sql.*"%>
  4. <% Class.forName("com.mysql.jdbc.Driver"); %>
  5. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  10. <title>Selecting Data From A DB</title>
  11. </head>
  12. <body>
  13. <h1>Selecting Data From A DB</h1>
  14.  
  15. <%!
  16. public class Student {
  17. String URL = "jdbc:mysql://localhost:3306/school_db_newnew?useTimezone=true&serverTimezone=GMT";
  18. String USER = "root";
  19. String PASSWORD = "123456";
  20.  
  21. Connection connection = null;
  22. PreparedStatement selectStudents = null;
  23. ResultSet resultSet = null;
  24.  
  25. public Student() {
  26. try {
  27. connection = DriverManager.getConnection(URL, USER, PASSWORD);
  28.  
  29. selectStudents = connection.prepareStatement("SELECT student.name, student.surname FROM student");
  30. } catch (SQLException e){
  31. e.printStackTrace();
  32. }
  33.  
  34. }
  35.  
  36.  
  37. public ResultSet getStudents() {
  38. try {
  39. resultSet = selectStudents.executeQuery();
  40. }catch (SQLException e){
  41. e.printStackTrace();
  42. }
  43. return resultSet;
  44. }
  45. }
  46.  
  47. %>
  48.  
  49. <%
  50. Student student = new Student();
  51. ResultSet students = student.getStudents();
  52. %>
  53. <table border="1">
  54.  
  55. <tbody>
  56. <tr>
  57. <td>Name</td>
  58. <td>Surname</td>
  59. </tr>
  60. <% while (students.next()) {%>
  61. <tr>
  62. <td><%= students.getString("student.name")%></td>
  63. <td><%= students.getString("student.surname")%></td>
  64. </tr>
  65. <%}%>
  66. </tbody>
  67. </table>
  68.  
  69.  
  70.  
  71. </body>
  72. </html>
  73.  
  74. import java.io.IOException;
  75. import java.io.PrintWriter;
  76. import javax.servlet.ServletException;
  77. import javax.servlet.http.HttpServlet;
  78. import javax.servlet.http.HttpServletRequest;
  79. import javax.servlet.http.HttpServletResponse;
  80.  
  81.  
  82. public class MyServlet extends HttpServlet {
  83.  
  84.  
  85. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  86. throws ServletException, IOException {
  87. response.setContentType("text/html;charset=UTF-8");
  88. try (PrintWriter out = response.getWriter()) {
  89. /* TODO output your page here. You may use following sample code. */
  90. out.println("<!DOCTYPE html>");
  91. out.println("<html>");
  92. out.println("<head>");
  93. out.println("<title>Servlet MyServlet</title>");
  94. out.println("</head>");
  95. out.println("<body>");
  96. out.println("<h1>Servlet MyServlet at " + request.getContextPath() + "</h1>");
  97. out.println("</body>");
  98. out.println("</html>");
  99. }
  100. }
  101.  
  102.  
  103. @Override
  104. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  105. throws ServletException, IOException {
  106. processRequest(request, response);
  107. }
  108.  
  109.  
  110. @Override
  111. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  112. throws ServletException, IOException {
  113. processRequest(request, response);
  114. }
  115.  
  116.  
  117. @Override
  118. public String getServletInfo() {
  119. return "Short description";
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement