当前位置: 首页 > article >正文

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();
                }
            }
        }


//        输出查询信息到浏览器


    }
}


http://www.kler.cn/news/366608.html

相关文章:

  • LCD手机屏幕高精度贴合
  • 【大数据分析与挖掘模型】matlab实现——非线性回归预测模型
  • Segugio:一款针对恶意软件的进程执行跟踪与安全分析工具
  • Golang | Leetcode Golang题解之第503题下一个更大元素II
  • 腾讯云DBA面试(一面)
  • 力扣143:重排链表
  • SSRF实战:利用ssrf.php对自己内网网站尝试访问
  • 信号(三)【信号保存】
  • nuxt3项目创建
  • 【C++进阶篇】——STL的简介
  • 用友U8接口-库存管理(7)
  • HTML 区块
  • sql-labs靶场第二十关测试报告
  • 【Linux】从open到write:系统文件I/O 的奥秘与实战指南
  • 实测体验Claude 3.5升级版:AI首次实现直接操控电脑!
  • 【Linux】信号量,线程池
  • 智能EDA小白从0开始 —— DAY27 第二届设计自动化产业峰会IDAS 2024:比昂芯科技携最新EDA工具链及应用成果亮相
  • 从文化到实践:DevOps的基本概念与核心实践详解
  • 20241023给荣品RD-RK3588-AHD开发板刷荣品预编译的Android12之后使用TDMonitor V2.0.2测试板载GPS
  • Nginx14-Lua基础
  • 【Leecode】Leecode刷题之路第32天之最长有效括号
  • C++ | STL | 侯捷 | 学习笔记
  • 【Qt】控件——Qt输入类控件、常见的输入类控件、输入类控件的使用、Line Edit、Text Edit、Combo Box、Spin Box
  • VUE前端按钮添加遮罩层
  • HttpRequest模块 --- 存储http请求要素
  • leetCode算法题爬楼梯递归写法