第一步:
准备一个配置文件
druid.properties文件,这个文件的是数据库的信息内容如下:
第二步:
创建一个JDBCutil类,把上面的druid.properties的内容用这个类存储起来
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();
}
}
第三步:
创建一个实体类,用对象的方式,创建对象,生成对应的setter和getter、toString方法。此处省略了setter和getter方法。
package hubu.domain;
import java.time.LocalDate;
public class plament {
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 String first_time;
private String last_time;
private String comment;
@Override
public String toString() {
return "plament{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", document_id='" + document_id + '\'' +
", sex='" + sex + '\'' +
", id_number='" + id_number + '\'' +
", address_id='" + address_id + '\'' +
", report_id='" + report_id + '\'' +
", first_time=" + first_time +
", last_time=" + last_time +
", comment='" + comment + '\'' +
'}';
}
}
第四步:
创建一个实体类,这个个类使用Spring JDBC里边有一个 JdbcTemplate 类和自己要写的sql语句,代码如下:
package hubu.dao;
import hubu.domain.plament;
import 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;
//操作数据库的User类
public class UserDao {
//声明JDBCTemplate对象共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtil.getDataSource());
public List getAccessor(){
String sql = "select * from t_patient";
RowMapper<plament> rm = new BeanPropertyRowMapper<>(plament.class);
List<plament> ls = template.query(sql, rm);
return ls;
}
}
第五步:
创建一个Servlet类,实现了第四步中的getAccessor() 方法和和使用request.setAttribute的方法实现数据传递,如下:
package hubu.web.servlet;
import hubu.dao.UserDao;
import hubu.domain.plament;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException, ServletException {
//1.设置编码
request.setCharacterEncoding("utf-8");
UserDao ad = new UserDao();
List<plament> ls = ad.getAccessor();
request.setAttribute("asd", ls);
request.getRequestDispatcher("/add.jsp").forward(request,response);
}
}
第六步:
在webapp的目录下创建一个.jsp文件,<c:forEach items="${后台的传过来的数据}" var="变量名">用如下:
<%@ 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="${asd}" var="p">
<tr>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.document_id}</td>
<td>${p.sex}</td>
<td>${p.id_number}</td>
<td>${p.address_id}</td>
<td>${p.report_id}</td>
<td>${p.first_time}</td>
<td>${p.last_time}</td>
<td>${p.comment}</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);
}
}
pom.xml里面所需要的jar需要自己下载一下。
最后:
这是以 上所有的文件结构图