使用Druid数据库连接池来接数据库表中数据显示到前端页面

8 篇文章 0 订阅
4 篇文章 0 订阅

第一步:

创建一个Druid的配置文件,文件内容如下:

        

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:端口号/数据库名称?useUnicode=true&characterEncoding=utf-8&userSSL=false&serverTimezone=GMT%2B8
username=root
password=密码
initialSize=5
maxActive=10
maxWait=3000

filters=stat
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200

第二步:

创建一个实体类,把配置文件放进来,如下:

package cn.hubu.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtil {
    private static  DataSource ds;
    static {
        try {
            //1.加载配置文件
            Properties pro = new Properties();

            //使用ClassLoader加载配置文件,获取字节输入流
            InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(resourceAsStream);
            //2.初始化连接池对象
            ds = DruidDataSourceFactory.createDataSource(pro);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static  DataSource getDataSource(){
        return ds;
    }

    public static Connection GetConnection() throws SQLException {
        return ds.getConnection();
    }

}

第三步:

        创建一个实体类,把数据库表中的字段放到实体类方法中,如下:

package cn.hubu.domain;

import java.time.LocalDate;

public class Patient {
    private String id;
    private String name;
    private String document_id;
    private String sex;
    private String id_number;
    private String address_id;
    private String report_id;
    private LocalDate first_time;
    private LocalDate last_time;
    private String comment;

    public String getId() {
        return id;
    }
}

生成对应的setter和getter方法和toString方法。

第四步:

        创建一个实体类,使用Spring JDBC里边的一个JdbcTemplate 类,和自己对应的sql语句,如下:  

package cn.hubu.dao;

import cn.hubu.domain.Patient;
import cn.hubu.util.JDBCUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class PatientDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtil.getDataSource());

   public List getPatient(){
        String sql = "select name,document_id,sex,id_number,address_id,report_id,first_time,last_time from t_patient;";
        RowMapper<Patient> rm = new BeanPropertyRowMapper<>(Patient.class);
        List ls = template.query(sql,rm);
        return ls;
    }
}

第五步:

        创建一个Servlet类,实现了第四步中的getPatient() 方法和和使用request.setAttribute的方法实现数据传递,如下:

        

package cn.hubu.web.servlet;

import cn.hubu.dao.PatientDao;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.HttpServlet;

import java.io.IOException;
import java.util.List;

@WebServlet("/add_patient")
public class PatientServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PatientDao pd = new PatientDao();
        List patient = pd.getPatient();
        request.setAttribute("patient",patient);
        request.getRequestDispatcher("/add_patient.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

第六步:

        在webapp文件夹下面创建一个.jsp文件,如下:      

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html >
<head>
    <meta charset="utf-8">
    <!--   移动设备-->
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="table">
            <table>
                <thead>
                <tr>
                    <th scope="col">档案号</th>
                    <th scope="col">姓名</th>
                    <th scope="col">性别</th>
                    <th scope="col">身份证号</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${patient}" var="p">
                    <tr>

                        <td>${p.document_id}</td>
                        <td>${p.name}</td>
                        <td>${p.sex}</td>
                        <td>${p.id_number}</td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

第七步:

        以下是附加的测试内容,如果需要可以使用,否则略过,如下:

        创建一个UserDaoTest测试类,测试数据库连接池是否已经连接到数据库表中的数据如下:

        

package cn.hubu.test;

import cn.hubu.dao.UserDaoTest;
import org.junit.Test;

import java.util.List;

public class UserDaoTest{
    @Test
    public void selectPatient(){
        PatientDao pd = new PatientDao();
        List patient = pd.getPatient();
        System.out.println(patient);

    }
}

 

最后:

        这是以上所有的文件结构图

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值