web查询数据库
修改欢迎页面
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>query.html</welcome-file>
<!-- 将 query.html 作为 第一个 欢迎页面-->
</welcome-file-list>
</web-app>
前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
font-size: 48px;
color: red;
}
a{
text-decoration: none;
}
</style>
</head>
<body>
<!-- 显示页面 -->
<h1>信息系统</h1>
<a href="/web3/query">查询来自数据库的成员</a>
</body>
</html>
数据库属性
建库建表
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>query.html</welcome-file>
<!-- 将 query.html 作为 第一个 欢迎页面-->
</welcome-file-list>
<servlet>
<servlet-name>aa</servlet-name>
<servlet-class>com.yanyu.Query</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>aa</servlet-name>
<url-pattern>/query</url-pattern>
</servlet-mapping>
</web-app>
后端
package com.yanyu;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ResourceBundle;
public class Query extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置 请求和相应的 编码格式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 连接数据库
ResourceBundle bundle = ResourceBundle.getBundle("com\\yanyu\\db");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String pass = bundle.getString("pass");
// 放大作用于
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
con.setAutoCommit(false);
st = con.createStatement();
String sql = "select * from user";
rs = st.executeQuery(sql);
// 遍历结果集
out.print("<!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.print(" <title>Document</title>");
out.print("</head>");
out.print("<body>");
out.print(" ");
out.print(" <table border='2px' width='400px'>");
out.print(" <tr>");
out.print(" <td>姓名</td>");
out.print(" <td>密码</td>");
out.print(" </tr>");
while (rs.next()) {
String name = rs.getString("name");
String pass1 = rs.getString("pass");
out.print(" <tr>");
out.print(" <td>"+name+"</td>");
out.print(" <td>"+pass1+"</td>");
out.print(" </tr>");
}
out.print("");
out.print(" </table>");
out.print("</body>");
out.print("</html>");
con.commit();
} catch (ClassNotFoundException | SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
// 输出查询信息到浏览器
}
}