项目目录结构
utils包下的工具类
jdbcUtils
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JdbcUtils {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/test02?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false";
private static final String USER = "用户名";
private static final String PASSWORD = "密码";
/**
* 两种方法导入数据库的驱动包(JDBC)
*/
//静态代码块:随着类的加载只执行一次
static {
//使用第三方jar报jdbc操作数据库
//方法一:通过new对象导入驱动
// try {
// new com.mysql.jdbc.Driver();
// } catch (SQLException e) {
// e.printStackTrace();
// }
//方法二:通过反射导入驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 创建与数据库的连接
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 查询完毕,要对以下三个对象进行关闭
* @param rs 结果集
* @param pstm 查询窗口
* @param conn 数据库的连接
*/
public static void close(ResultSet rs, PreparedStatement pstm, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 增删改操作的实现
* @param sql
* @param objects
*/
public static void executeUpdate(String sql, Object... objects) {
Connection conn = null;
PreparedStatement pstm = null;
conn = getConnection();
try {
//预处理,把sql语句中
pstm = conn.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
pstm.setObject(i + 1, objects[i]);
}
int result = pstm.executeUpdate();
System.out.println(result);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(null, pstm, conn);
}
}
/**
* 查询操作的实现
*
* @param sql
* @param rowMap 获取查询的数据,并且添加到list集合中
* @param objects
* @param <T>
* @return
*/
public static <T> List<T> executeQuery(String sql, RowMap<T> rowMap, Object... objects) {
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
List<T> lists = new ArrayList<>();
try {
conn = getConnection();
//预处理,把sql语句中
pstm = conn.prepareStatement(sql);
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
pstm.setObject(i + 1, objects);
}
}
rs = pstm.executeQuery();
while (rs.next()) {
T t = rowMap.rowMapping(rs);
lists.add(t);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rs, pstm, conn);
}
return lists;
}
}
RowMap接口
import java.sql.ResultSet;
import java.sql.SQLException;
/**
泛型接口
*/
public interface RowMap<T> {
public T rowMapping(ResultSet rs) throws SQLException;
}
dao包
import com.neuedu.poio.Cat;
import java.util.List;
public interface IcatDao {
//先写接口再写实现类,方便后期扩展
public List<Cat> getAll();
}
import com.neuedu.poio.Cat;
import com.neuedu.utils.JdbcUtils;
import com.neuedu.utils.RowMap;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* 对MySQL数据库进行操作
*/
public class CatDaoImpl implements IcatDao{
@Override
public List<Cat> getAll() {
//对MySQL数据库中Cat表的查询
List<Cat> list= JdbcUtils.executeQuery("select *from cat", new RowMap<Cat>() {
@Override
public Cat rowMapping(ResultSet rs) throws SQLException {
Cat cat = new Cat();
cat.setId(rs.getInt("id"));
cat.setName(rs.getString("name"));
cat.setAge(rs.getInt("age"));
return cat;
}
});
return list;
}
}
poio(实体类),根据数据库中的字段设计实体类,自动生成get(),set(),toString()三个方法
service包(服务层)
import com.neuedu.poio.Cat;
import java.util.List;
public interface IcatService {
public List<Cat> getAll();
}
import com.neuedu.dao.CatDaoImpl;
import com.neuedu.dao.IcatDao;
import com.neuedu.poio.Cat;
import java.util.List;
/**
* 对Dao查到的数据,返回给web
*/
public class CatServiceImpl implements IcatService {
private IcatDao dao = new CatDaoImpl();
@Override
public List<Cat> getAll() {
return dao.getAll();
}
}
web页面,在我的项目中没有创建web包,
这些都属于web层的类
import com.neuedu.poio.Cat;
import com.neuedu.service.CatServiceImpl;
import com.neuedu.service.IcatService;
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.util.List;
/**
* web页面
*/
public class CatListWeb extends HttpServlet {
private IcatService service = new CatServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Cat> cats = service.getAll();
//绑定数据
req.setAttribute("cats",cats);
//绑定要显示数据的页面
req.getRequestDispatcher("cats.jsp").forward(req,resp);
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class PutDataWeb extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("方法执行了.....");
String caseNumber = req.getParameter("caseNumber");
Integer age = Integer.parseInt(req.getParameter("age"));
System.out.println(caseNumber);
System.out.println(age);
resp.sendRedirect("register");
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RegisterWeb extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("register.jsp").forward(req,resp);
}
}
import com.neuedu.poio.Stu;
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.util.ArrayList;
import java.util.List;
/**
* 重新service
*/
public class web extends HttpServlet {
/**
*
* @param req 请求
* @param resp 回复
* @throws ServletException
* @throws IOException
*/
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// resp.setContentType("text/html;charset=utf-8");
// System.out.println("请求代码执行啦...");
// PrintWriter printWriter = resp.getWriter();
// printWriter.print("<div style='background:blue;width:300px;height:100px'>中文相应</div>");
//生成的测试数据
// List<String> strings = new ArrayList<>();
// for (int i = 0;i < 100; i++) {
// strings.add(i + "hello world");
// }
List<Stu> lists= new ArrayList<>();
for (int i = 0; i < 20; i++) {
Stu stu = new Stu();
stu.setSno(i);
stu.setName(i+"abc");
stu.setHeight(i+"00");
lists.add(stu);
}
//设置数据
req.setAttribute("data", lists);
req.getRequestDispatcher("test.jsp").forward(req,resp);
}
}
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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>webs</servlet-name>
<servlet-class>com.neuedu.web</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webs</servlet-name>
<url-pattern>/tests</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>data</servlet-name>
<servlet-class>com.neuedu.PutDataWeb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>data</servlet-name>
<url-pattern>/putData</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>com.neuedu.RegisterWeb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
jsp的页面
<%--
Created by IntelliJ IDEA.
User: admin-wpn
Date: 2020/5/14
Time: 17:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#header{
/*根据我们的窗口自动调整,用百分比*/
width: 100%;
height: 40px;
background-color: aqua;
color: white;
font-size: 25px;
font-family: "微软雅黑";
/*字体加粗*/
font-weight: 800;
}
#content{
width: 1280px;
height: 486px;
}
#left{
float: left;
}
dl{
width: 150px;
}
dt,dd{
height: 80px;
/*border: solid 1px black;*/
padding-left: 15px;
/*内容垂直居中*/
line-height: 80px;
/*内容颜色*/
color: black;
/*背景色*/
background-color: bisque;
border-bottom: solid 1px black;
}
dd{
background-color: #dddddd;
height: 30px;
line-height: 30px;
padding-left: 40px;
display: none;
}
#right{
width:1000px;
height: 486px;
float: left;
/*background-color: #71ff83;*/
}
*{
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<!--添加浮动的效果,浮动会脱离文档流-->
<div id="header">东软云医院HIS系统</div>
<div id="content">
<div id="left">
<dl>
<dt>挂号收费</dt>
<dd><a href="/register"></a>现场挂号</dd>
<dd><a href="/tuihao.jsp"></a>退号</dd>
<dd>收费</dd>
<dd>退费</dd>
<dd>发票补打</dd>
</dl>
<dl>
<dt>门诊医生</dt>
<dd>门诊1</dd>
<dd>门诊2</dd>
<dd>门诊3</dd>
<dd>门诊4</dd>
<dd>门诊5</dd>
</dl>
<dl>
<dt>医技处置</dt>
<dd>医技处置1</dd>
<dd>医技处置2</dd>
<dd>医技处置3</dd>
<dd>医技处置4</dd>
<dd>医技处置5</dd>
</dl>
<dl>
<dt>药房管理</dt>
<dd>药房管理1</dd>
<dd>药房管理2</dd>
<dd>药房管理3</dd>
<dd>药房管理4</dd>
<dd>药房管理5</dd>
</dl>
</div>
<div id="right">
<iframe src="" width="1000" height="486" style="border: none;"></iframe>
</div>
</div>
<!--js实现功能显示效果,直接用jquery-->
<script src="static/js/jquery-3.4.1.min.js"></script>
<script>
// <!--必须要有入口-->
$(function () {
//查看jquery是否引入成功
// alert("fffff")
//获取元素
//js事件,点击
$("dt").click(function () {
//jquery 获取同级元素
$(this).siblings().toggle().parent().siblings().children("dd").hide()
})
$("dd").click(function () {
$("iframe").removeAttr("src")
var addr=$(this).children("a").attr("href")
$("iframe").attr("src",addr)
})
})
</script>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: admin-wpn
Date: 2020/5/14
Time: 17:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="static/css/iconfont.css" rel="stylesheet" type="text/css">
<style>
input{
border-radius: 5px 5px;
height: 24px;
padding-left: 10px;
}
.btn{
background-color: #1babff;
width: 80px;
height: 30px;
color: white;
border: 0px;
}
span{
color: red;
}
.item{
width: 180px;
height: 60px;
float: left;
/*border: solid 1px #000;*/
margin-right: 20px;
/*margin-top: 20px;*/
}
.item1{
width: 300px;
height: 60px;
float: left;
/*border: solid 1px #000;*/
margin-right: 20px;
/*margin-top: 20px;*/
}
select{
width: 80px;
height: 30px;
border-radius: 5px 5px;
border: solid 1px #dddddd;
}
</style>
</head>
<body>
<div>现场挂号页面</div>
<%--数据提交--%>
<form action="putData">
发票号:<input value="123126" name="fapiao" type="text"> <input type="submit" value="挂号" class="btn iconfont"> <input type="reset" value="清空" class="btn iconfont">
<div style="font-size: 20px;margin-top: 20px">挂号信息</div>
<div class="item">
<span>*</span>病历号:<br>
<input value="600626" name="caseNumber" type="text">
</div>
<div class="item">
<span>*</span>姓名:<br>
<input placeholder="请输入姓名" name="realName" type="text">
</div>
<div class="item">
<span>*</span>性别:<br>
<select name="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
</div>
<div class="item1">
<span>*</span>年龄:<br>
<input placeholder="请输入年龄" name="age" type="text">
<select>
<option value="">岁</option>
<option value="">月</option>
<option value="">天</option>
</select>
</div>
<div class="item">
<span>*</span>出生日期:<br>
<input placeholder="请输入出生日期" type="text">
</div>
<div class="item1">
<span>*</span>身份证号:<br>
<input placeholder="请输入身份证号" type="text" style="width: 300px">
</div>
<div class="item1" style="margin-left: 80px">
<span>*</span>家庭住址:<br>
<input placeholder="请输入家庭住址" type="text" style="width: 300px">
</div>
<div class="item">
<span>*</span>结算类别:<br>
<select>
<option value="">自费</option>
<option value="">社保</option>
</select>
</div>
<div class="item">
<span>*</span>看诊日期:<br>
<input placeholder="请输入看诊日期" type="text">
</div>
<div class="item">
<span>*</span>午别:<br>
<select>
<option value="">上午</option>
<option value="">下午</option>
</select>
</div>
<div class="item">
<span>*</span>挂号科室:<br>
<select>
<option value="">骨科</option>
<option value="">外科</option>
</select>
</div>
<div class="item">
<span>*</span>号别:<br>
<select>
<option value="">普通号</option>
<option value="">专家号</option>
</select>
</div>
<div class="item">
<span>*</span>看诊医生:<br>
<select>
<option value="">医生A</option>
<option value="">医生B</option>
</select>
</div>
<div class="item">
<span>*</span>初试号额:<br>
<input placeholder="初始号额" type="text">
</div>
<div class="item">
<span>*</span>已用号额:<br>
<input placeholder="已用号额" type="text">
</div>
<div class="item">
<span>*</span>病历本:<br>
<input type="checkbox" name="isBook">
</div>
<div class="item">
<span>*</span>应收金额:<br>
<input placeholder="应收金额" type="text">
</div>
<div class="item">
<span>*</span>收费方式:<br>
<select>
<option value="">现金</option>
<option value="">微信</option>
<option value="">支付宝</option>
</select>
</div>
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: admin-wpn
Date: 2020/5/14
Time: 17:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="/static/css/iconfont.css" rel="stylesheet" type="text/css">
<style>
.item{
width: 180px;
height: 60px;
float: left;
/*border: solid 1px #000;*/
margin-right: 30px;
margin-top: 20px;
}
span{
color: red;
}
.btn{
background-color: #1babff;
width: 80px;
height: 30px;
color: white;
border: 0px;
margin-top: 40px;
border-radius: 5px;
}
th{
width: 120px;
font-size: 15px;
/*padding-left: 60px;*/
/*border-bottom: solid #33b0ee;*/
/*background-color: #71ff83;*/
color: #cccccc;
}
</style>
</head>
<body>
<div>患者信息查询</div>
<form action="">
<div class="item">
<span>*</span>病历号:<br>
<input value="600626" name="caseNumber" type="text">
</div>
<input type="submit" value="搜索" class="btn iconfont">
<div style="width: 100px;font-family: 微软雅黑;margin-top: 20px">患者信息确认</div>
<div class="item">
<span>*</span>姓名:<br>
<input placeholder="请输入姓名" name="realName" type="text">
</div>
<div class="item">
<span>*</span>身份证号:<br>
<input placeholder="请输入身份证号" type="text" style="width: 300px">
</div>
<div class="item" style="margin-left: 190px">
<span>*</span>家庭住址:<br>
<input placeholder="请输入家庭住址" type="text" style="width: 300px">
</div>
<div style="width: 100px;font-family: 黑体;margin-top: 20px">患者挂号信息</div>
<table>
<thead>
<tr style=" box-shadow:0 1px 0 0 #cccccc;">
<th>病历号</th>
<th>姓名</th>
<th>身份证号</th>
<th>挂号日期</th>
<th>挂号午别</th>
<th>看诊科室</th>
<th>看诊状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
<div style="text-align: center;width: 900px; color: #cccccc;margin-top: 20px">暂无数据</div>
</form>
</body>
</html>
<%--这是导入的java的包--%>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: admin-wpn
Date: 2020/5/14
Time: 11:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入jstl标签库--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>WebTitle</title>
<style>
table {
border: solid 1px black;
/*去掉表格之间的距离*/
border-collapse: collapse;
}
th, td {
border: solid 1px black;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>绑定数据</th>
</tr>
</thead>
<%--前后端分离,在前端中不要出现java代码
使用el表达式+jstl标签库(需要导入jar包),替换html中的java代码,简单易用
--%>
<tbody>
<%--
jsp中用<%%>符号内写java代码
<%
for (String str : (List<String>) request.getAttribute("data")) {%>
<tr>
<td><%=str%></td>
</tr>
<% }%>
--%>
<c:forEach items=" ${data}" var="da">
<tr>
<td>${da.sno}</td>
<td>${da.name}</td>
<td>${da.height}</td>
</tr>
</c:forEach>
</tbody>
</table>
<%--获取data数据--%>
<%--
<div style="height: 300px;width: 100px;background-color: aquamarine">
写java代码
<%=
request.getAttribute("data")
%>
</div>
--%>
</body>
</html>
实现的效果