The jsp page directly reads the mysql database data and displays it on the page

The jsp page directly reads the mysql database data display:

  1. <%@page import="java.sql.ResultSet"%>
  2. <%@page import="com.mysql.jdbc.Statement"%>
  3. <%@page import="java.sql.DriverManager"%>
  4. <%@page import="com.mysql.jdbc.Connection"%>
  5. <%@ page language="java" contentType="text/html; charset=gbk"
  6. pageEncoding="gbk"%>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  11. <title>Insert title here</title>
  12. </head>
  13. <body>
  14. <%
  15. //load the driver
  16. Class.forName("com.mysql.jdbc.Driver");
  17. //establish connection
  18. Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/house", "root",
  19. "");
  20. //Create Statement
  21. Statement stm = (Statement) conn.createStatement();
  22. //execute query
  23. ResultSet rs = stm.executeQuery("select username,pwd from user");
  24. %>
  25. <table border="" width="">
  26. <%
  27. //Loop through the results
  28. while (rs.next()) {
  29. %>
  30. <tr>
  31. <td><%=rs.getString()%></td>
  32. <td><%=rs.getString()%></td>
  33. </tr>
  34. <%
  35. }
  36. %>
  37. </table>
  38. </body>
  39. </html>

Guess you like

Origin blog.csdn.net/zxj19880502/article/details/129179964