04_web基础(八)之车票实现增删改查初级版本
43.web页面显示车票列表简略完成
代码:
控制层代码
package com.day03.station.controller; import com.day03.station.model.Ticket;
import com.day03.station.service.impl.TicketService; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
@WebServlet("/list")
public class TicketListServlet extends HttpServlet {
//业务对象
private TicketService ticketService=new TicketService();
/**
* 查询车票列表
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.接收参数
//2.调用业务方法
List<Ticket> list = ticketService.queryAll();
req.setAttribute("list",list);
//3.控制跳转
req.getRequestDispatcher("/WEB-INF/views/ticketList.jsp").forward(req,resp); }
}
TicketListServlet
jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>车票列表</title>
</head>
<body>
我是车票列表页面
${list}
<%--
表格标签
行 <tr>
列 <td>
--%>
<table align="center" border="1">
<tr>
<td>编号</td>
<td>start_station</td>
<td>stop_station</td>
<td>start_time</td>
<td>ticket_price</td>
</tr>
<tr>
<td>${list[0].id}</td>
<td>${list[0].startStation}</td>
<td>${list[0].stopStation}</td>
<td>${list[0].startTime}</td>
<td>${list[0].ticketPrice}</td>
</tr>
<tr>
<td>${list[1].id}</td>
<td>${list[1].startStation}</td>
<td>${list[1].stopStation}</td>
<td>${list[1].startTime}</td>
<td>${list[1].ticketPrice}</td>
</tr>
<tr>
<td>${list[2].id}</td>
<td>${list[2].startStation}</td>
<td>${list[2].stopStation}</td>
<td>${list[2].startTime}</td>
<td>${list[2].ticketPrice}</td>
</tr> </table>
</body>
</html>
ticketList.jsp
44.web页面显示车票列表实现
1.使用c标签前的准备
为了完全在JSP中消除Java代码,学习JAVA的标签库:
Java标签库:
标准标签库(JSTL).(SUN预先提供好的,我们使用的.)
以后学习MVC框架(Struts2/SpringMVC)的时候,都会学习一套标签.
=====================================================================
准备:
1:准备JSTL相关的jar(Tomcat根/webapps/examples/WEB-INF/lib).
jstl.jar standard.jar
el-api.jar jsp-api.jar
2:使用JSTL的JSP页面中,引人标签库:
比如引人核心JSTL:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
3:注意:有的idea没有添加c标签库,所以必须手动添加
2.ide配置
jsp代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>车票列表</title>
</head>
<body>
我是车票列表页面 <%--
表格标签
行 <tr>
列 <td>
--%>
<table align="center" border="1">
<tr>
<td>编号</td>
<td>start_station</td>
<td>stop_station</td>
<td>start_time</td>
<td>ticket_price</td>
</tr>
<c:forEach items="${list}" var="ticket">
<tr>
<td>${ticket.id}</td>
<td>${ticket.startStation}</td>
<td>${ticket.stopStation}</td>
<td>${ticket.startTime}</td>
<td>${ticket.ticketPrice}</td>
</tr>
</c:forEach> </table>
</body>
</html>
列表页面显示效果:
45.web页面实现车票增加
实现步骤如下:
1.在列表页面显示新增按钮,列表jsp页面代码如下:
列表界面效果如图:
2.获取新增页面的servlet代码如下:
package com.day03.station.controller; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
@WebServlet("/addPage")
public class AddTicketPageServlet extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收参数
//调用业务方法
//控制跳转
req.getRequestDispatcher("/WEB-INF/views/addTicket.jsp").forward(req,resp);
}
}
AddTicketPageServlet
3.新增页面jsp代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>新增页面</title>
</head>
<body> 我是增加页面
<form action="/addData" method="post">
开始车站:<input id="startStation" type="text" name="startStation" value=""/>
<br/>
<br/>
到达车站:<input id="stopStation" type="text" name="stopStation" value=""/>
<br/>
<br/>
<input type="submit" value="保存"/> </form>
</body>
</html>
页面效果图:
4.点击时后端servlet代码如下:
package com.day03.station.controller; import com.day03.station.model.Ticket;
import com.day03.station.service.impl.TicketService; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
@WebServlet("/addData")
public class AddDataServlet extends HttpServlet {
private TicketService ticketService=new TicketService();
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收参数
String startStation = req.getParameter("startStation");
String stopStation = req.getParameter("stopStation");
//调用业务方法
Ticket ticket = new Ticket();
ticket.setStartStation(startStation);
ticket.setStopStation(stopStation);
ticketService.save(ticket);
//控制跳转
resp.sendRedirect("/list");
}
}
AddDataServlet
新增完成!
46.web页面实现删除
实现步骤如下:
1.添加操作列效果如下:
代码如下:
2.servlet实现
package com.day03.station.controller; import com.day03.station.model.Ticket;
import com.day03.station.service.impl.TicketService; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
@WebServlet("/delete")
public class DeleteTicketServlet extends HttpServlet {
private TicketService ticketService=new TicketService();
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收参数
String idStr = req.getParameter("id");
//如何将字符串类型转变为整数类型
Integer id = Integer.valueOf(idStr);
//调用业务方法
ticketService.delete(id);
//控制跳转
resp.sendRedirect("/list");
}
}
DeleteTicketServlet
删除完成!
47.web页面实现修改
实现步骤如下:
1.在列表上显示修改按钮,代码如下
页面效果如下:
2.点击修改时获取修改页面的servlet代码如下:
package com.day03.station.controller; import com.day03.station.model.Ticket;
import com.day03.station.service.impl.TicketService; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
@WebServlet("/updatePage")
public class UpdateTicketPageServlet extends HttpServlet {
private TicketService ticketService=new TicketService();
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收参数
String idStr = req.getParameter("id");
//调用业务方法 通过id查找到具体的数据
Integer id = Integer.valueOf(idStr); Ticket ticket = ticketService.queryById(id);
//然后再显示到页面去 放入请求
req.setAttribute("ticket",ticket); //控制跳转
req.getRequestDispatcher("/WEB-INF/views/updateTicket.jsp").forward(req,resp);
}
}
UpdateTicketPageServlet
3.修改界面代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<title>修改页面</title>
</head>
<body> 我是修改页面
<form action="/updateData" method="post">
id:<input id="id" type="text" name="id" value="${ticket.id}"/>
<br/>
<br/>
开始车站:<input id="startStation" type="text" name="startStation" value="${ticket.startStation}"/>
<br/>
<br/>
到达车站:<input id="stopStation" type="text" name="stopStation" value="${ticket.stopStation}"/>
<br/>
<br/>
<input type="submit" value="修改"/> </form>
</body>
</html>
修改界面效果如下:
4.点击修改按钮时,后端执行的servlet代码如下:
package com.day03.station.controller; import com.day03.station.model.Ticket;
import com.day03.station.service.impl.TicketService; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
@WebServlet("/updateData")
public class UpdateDataServlet extends HttpServlet {
private TicketService ticketService=new TicketService();
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收参数
String idStr = req.getParameter("id");
Integer id = Integer.valueOf(idStr); String startStation = req.getParameter("startStation");
String stopStation = req.getParameter("stopStation");
//调用业务方法
Ticket ticket = new Ticket();
ticket.setId(id);
ticket.setStartStation(startStation);
ticket.setStopStation(stopStation);
ticketService.update(ticket);
//控制跳转
resp.sendRedirect("/list");
}
}
UpdateDataServlet
48.web页面实现高级查询
实现步骤如下:
1.在列表界面中添加高级查询输入框,jsp代码如下:
界面效果如下:
2.控制层代码,
package com.day03.station.controller; import com.day03.station.model.Ticket;
import com.day03.station.query.TicketQueryObj;
import com.day03.station.service.impl.TicketService; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
@WebServlet("/queryList")
public class QueryListServlet extends HttpServlet {
private TicketService ticketService=new TicketService();
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.接收参数
String startStation = req.getParameter("startStation");
String stopStation = req.getParameter("stopStation");
String minTicketPrice = req.getParameter("minTicketPrice");
String maxTicketPrice = req.getParameter("maxTicketPrice");
//把条件封装成查询对象
TicketQueryObj ticketQueryObj = new TicketQueryObj();
//使用if处理没有传的参数
if (startStation!=null && !startStation.equals("")){
ticketQueryObj.setStartStation(startStation);
}
if (stopStation!=null && !stopStation.equals("")){
ticketQueryObj.setStopStation(stopStation);
}
if (minTicketPrice!=null && !minTicketPrice.equals("")){
ticketQueryObj.setMinTicketPrice(minTicketPrice);
}
if (maxTicketPrice!=null && !maxTicketPrice.equals("")){
ticketQueryObj.setMaxTicketPrice(maxTicketPrice);
} //2.调用业务方法
List<Ticket> query = ticketService.query(ticketQueryObj); //3.控制跳转
req.setAttribute("list",query);
req.setAttribute("ticketQueryObj",ticketQueryObj);
//3.控制跳转
req.getRequestDispatcher("/WEB-INF/views/ticketList.jsp").forward(req,resp);
}
}
QueryListServlet
3.业务层代码
4.dao方法实现
@Override
public List<Ticket> query(TicketQueryObj ticketQueryObj) {
List<Ticket> list=new ArrayList<>();
try { //1.加载
Class.forName("com.mysql.jdbc.Driver");
//2.链接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_data", "root", "admin");
//3.创建编译语句
String sql="SELECT id,start_station,stop_station,start_time,ticket_price FROM ticket WHERE 1=1";
//拿一个袋子装 依次装参数
List<Object> param=new ArrayList<>(); //如果有开始车站条件,则添加 该查询条件
if (ticketQueryObj.getStartStation()!=null){
sql=sql+" AND start_station=?";
param.add(ticketQueryObj.getStartStation());
}
if (ticketQueryObj.getStopStation()!=null){
sql=sql+" AND stop_station=?";
param.add(ticketQueryObj.getStopStation());
}
if (ticketQueryObj.getMinTicketPrice()!=null){
sql=sql+" AND ticket_price>?";
param.add(ticketQueryObj.getMinTicketPrice());
}
if (ticketQueryObj.getMaxTicketPrice()!=null){
sql=sql+" AND ticket_price<?";
param.add(ticketQueryObj.getMaxTicketPrice());
} System.out.println(" sql="+sql); PreparedStatement preparedStatement = connection.prepareStatement(sql);
// for循环设置参数
for (int i=0;i<param.size();i++){
preparedStatement.setObject(i+1,param.get(i));
System.out.println(" 参数 ="+param.get(i));
} //4.执行
ResultSet resultSet = preparedStatement.executeQuery();
//解析结果集
while (resultSet.next()){
//取id
int id1 = resultSet.getInt("id");
//取开始车站
String startStation = resultSet.getString("start_station");
//取到达车站
String stopStation = resultSet.getString("stop_station");
//取发车时间
String startTime = resultSet.getString("start_time");
//取车票价格
int ticketPrice = resultSet.getInt("ticket_price");
//封装到对象里面去
Ticket ticket = new Ticket();
ticket.setId(id1);
ticket.setStartStation(startStation);
ticket.setStopStation(stopStation);
ticket.setStartTime(startTime);
ticket.setTicketPrice(ticketPrice); //将车票对象装入集合对象里面
list.add(ticket); } //5.释放资源
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
5.查询封装对象代码
package com.day03.station.query; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
public class TicketQueryObj {
/* String startStation = req.getParameter("startStation");
String stopStation = req.getParameter("stopStation");
String minTicketPrice = req.getParameter("minTicketPrice");
String maxTicketPrice = req.getParameter("maxTicketPrice");*/
private String startStation;
private String stopStation;
private String minTicketPrice;
private String maxTicketPrice; public String getStartStation() {
return startStation;
} public void setStartStation(String startStation) {
this.startStation = startStation;
} public String getStopStation() {
return stopStation;
} public void setStopStation(String stopStation) {
this.stopStation = stopStation;
} public String getMinTicketPrice() {
return minTicketPrice;
} public void setMinTicketPrice(String minTicketPrice) {
this.minTicketPrice = minTicketPrice;
} public String getMaxTicketPrice() {
return maxTicketPrice;
} public void setMaxTicketPrice(String maxTicketPrice) {
this.maxTicketPrice = maxTicketPrice;
}
}
TicketQueryObj
6.效果演示如下:
49.50.web页面实现分页
实现后的效果:
实现步骤:
1.页面jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>车票列表</title>
</head>
<body> <%--
表格标签
行 <tr>
列 <td>
--%> <br/>
<div align="center">
<form action="/queryList" method="post">
当前页:<input id="currentPage" name="currentPage" value="1">
每页显示条数:<input id="pageSize" name="pageSize" value="3">
<br/>
开始车站:<input id="startStation" type="text" name="startStation" value="${ticketQueryObj.startStation}"/>
到达车站:<input id="stopStation" type="text" name="stopStation" value="${ticketQueryObj.stopStation}"/>
<br/>
最低价格:<input id="minTicketPrice" type="text" name="minTicketPrice" value="${ticketQueryObj.minTicketPrice}"/>
最高价格:<input id="maxTicketPrice" type="text" name="maxTicketPrice" value="${ticketQueryObj.maxTicketPrice}"/>
<input type="submit" value="查询"/>
</form>
</div>
<div align="center">
<a href="/addPage" >增加</a>
</div>
<br/>
<table align="center" border="1">
<tr>
<td>编号</td>
<td>开始车站</td>
<td>到达车站</td>
<td>发车时间</td>
<td>票价</td>
<td>操作</td>
</tr>
<c:forEach items="${pageResult.data}" var="ticket">
<tr>
<td>${ticket.id}</td>
<td>${ticket.startStation}</td>
<td>${ticket.stopStation}</td>
<td>${ticket.startTime}</td>
<td>${ticket.ticketPrice}</td>
<td>
<a href="/delete?id=${ticket.id}" >删除</a>|
<a href="/updatePage?id=${ticket.id}" >修改</a>
</td>
</tr> </c:forEach> <tr>
<td colspan="7">
<a href="#" onclick="goPage(1);" >首页</a>
<a href="#" onclick="goPage(${pageResult.currentPage}-1);">上页</a>
<a href="#" onclick="goPage(${pageResult.currentPage}+1);">下页</a>
<a href="#" onclick="goPage(${pageResult.totalPage});">末页</a>
每页显示${pageResult.pageSize}条,当前${pageResult.currentPage}/${pageResult.totalPage}页,共${pageResult.totalNum}条数据
</td>
</tr> </table>
</body>
<%--js函数--%>
<script type="text/javascript">
/*定义函数
*
* 作用:用来提交表单
* */
function goPage(currentPage){
alert("-----我是第一个函数-------");//向页面弹框
if (currentPage<1){
currentPage=1;
}
//给当前页在输入框中赋值
document.getElementById("currentPage").value=currentPage;
//提交表单
document.forms[0].submit();
} </script>
</html>
2.控制层代码
package com.day03.station.controller; import com.day03.station.common.PageResult;
import com.day03.station.query.TicketQueryObj;
import com.day03.station.service.impl.TicketService; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
@WebServlet("/queryList")
public class QueryListServlet extends HttpServlet {
private TicketService ticketService = new TicketService(); @Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.接收参数
String startStation = req.getParameter("startStation");
String stopStation = req.getParameter("stopStation");
String minTicketPrice = req.getParameter("minTicketPrice");
String maxTicketPrice = req.getParameter("maxTicketPrice");
//取出分页用的数据
String currentPage = req.getParameter("currentPage");
String pageSize = req.getParameter("pageSize"); //把条件封装成查询对象
TicketQueryObj ticketQueryObj = new TicketQueryObj();
//使用if处理没有传的参数
if (startStation != null && !startStation.equals("")) {
ticketQueryObj.setStartStation(startStation);
}
if (stopStation != null && !stopStation.equals("")) {
ticketQueryObj.setStopStation(stopStation);
}
if (minTicketPrice != null && !minTicketPrice.equals("")) {
ticketQueryObj.setMinTicketPrice(minTicketPrice);
}
if (maxTicketPrice != null && !maxTicketPrice.equals("")) {
ticketQueryObj.setMaxTicketPrice(maxTicketPrice);
}
if (currentPage != null && !currentPage.equals("")) {
ticketQueryObj.setCurrentPage(Integer.valueOf(currentPage));
}
if (pageSize != null && !pageSize.equals("")) {
ticketQueryObj.setPageSize(Integer.valueOf(pageSize));
} //2.调用业务方法
// List<Ticket> query = ticketService.query(ticketQueryObj); PageResult pageResult = ticketService.queryPageResult(ticketQueryObj); //3.控制跳转
req.setAttribute("pageResult", pageResult);
req.setAttribute("ticketQueryObj", ticketQueryObj);
//3.控制跳转
req.getRequestDispatcher("/WEB-INF/views/ticketList.jsp").forward(req, resp);
}
}
QueryListServlet
3.业务层代码
@Override
public PageResult queryPageResult(TicketQueryObj ticketQueryObj) {
//封装分页对象
PageResult pageResult = new PageResult();
// private List<Ticket> data;
List<Ticket> query = ticketDao.query(ticketQueryObj);
pageResult.setData(query);
//当前页
// private Integer currentPage=1;
pageResult.setCurrentPage(ticketQueryObj.getCurrentPage());
//每页显示条数
// private Integer pageSize=3;
Integer pageSize = ticketQueryObj.getPageSize(); pageResult.setPageSize(pageSize);
//共多少条 必须查询数据库
// private Integer totalNum=10;
Integer count = ticketDao.count(ticketQueryObj);
pageResult.setTotalNum(count);
//共多少页
// private Integer totalPage=3;
Integer totalPage=(count+pageSize-1)/pageSize;
pageResult.setTotalPage(totalPage); return pageResult;
}
4.dao的两条查询语句的实现
/**
* 查询分页列表
* @param ticketQueryObj
* @return
*/
@Override
public List<Ticket> query(TicketQueryObj ticketQueryObj) {
List<Ticket> list=new ArrayList<>();
try { //1.加载
Class.forName("com.mysql.jdbc.Driver");
//2.链接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_data", "root", "admin");
//3.创建编译语句
String sql="SELECT id,start_station,stop_station,start_time,ticket_price FROM ticket WHERE 1=1";
//拿一个袋子装 依次装参数
List<Object> param=new ArrayList<>(); //如果有开始车站条件,则添加 该查询条件
if (ticketQueryObj.getStartStation()!=null){
sql=sql+" AND start_station=?";
param.add(ticketQueryObj.getStartStation());
}
if (ticketQueryObj.getStopStation()!=null){
sql=sql+" AND stop_station=?";
param.add(ticketQueryObj.getStopStation());
}
if (ticketQueryObj.getMinTicketPrice()!=null){
sql=sql+" AND ticket_price>?";
param.add(ticketQueryObj.getMinTicketPrice());
}
if (ticketQueryObj.getMaxTicketPrice()!=null){
sql=sql+" AND ticket_price<?";
param.add(ticketQueryObj.getMaxTicketPrice());
}
//拼接分页 LIMIT ?,?
sql=sql+" LIMIT ?,? ";
param.add(ticketQueryObj.getStartIndex());
param.add(ticketQueryObj.getPageSize()); System.out.println(" sql="+sql); PreparedStatement preparedStatement = connection.prepareStatement(sql);
// for循环设置参数
for (int i=0;i<param.size();i++){
preparedStatement.setObject(i+1,param.get(i));
System.out.println(" 参数 ="+param.get(i));
} //4.执行
ResultSet resultSet = preparedStatement.executeQuery();
//解析结果集
while (resultSet.next()){
//取id
int id1 = resultSet.getInt("id");
//取开始车站
String startStation = resultSet.getString("start_station");
//取到达车站
String stopStation = resultSet.getString("stop_station");
//取发车时间
String startTime = resultSet.getString("start_time");
//取车票价格
int ticketPrice = resultSet.getInt("ticket_price");
//封装到对象里面去
Ticket ticket = new Ticket();
ticket.setId(id1);
ticket.setStartStation(startStation);
ticket.setStopStation(stopStation);
ticket.setStartTime(startTime);
ticket.setTicketPrice(ticketPrice); //将车票对象装入集合对象里面
list.add(ticket); } //5.释放资源
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
} /**
* 查询总记录条数
* @param ticketQueryObj
* @return
*/
@Override
public Integer count(TicketQueryObj ticketQueryObj) { Integer totalNum=0;
try { //1.加载
Class.forName("com.mysql.jdbc.Driver");
//2.链接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_data", "root", "admin");
//3.创建编译语句
String sql="SELECT COUNT(1) totalNum FROM ticket WHERE 1=1 ";
//拿一个袋子装 依次装参数
List<Object> param=new ArrayList<>(); //如果有开始车站条件,则添加 该查询条件
if (ticketQueryObj.getStartStation()!=null){
sql=sql+" AND start_station=?";
param.add(ticketQueryObj.getStartStation());
}
if (ticketQueryObj.getStopStation()!=null){
sql=sql+" AND stop_station=?";
param.add(ticketQueryObj.getStopStation());
}
if (ticketQueryObj.getMinTicketPrice()!=null){
sql=sql+" AND ticket_price>?";
param.add(ticketQueryObj.getMinTicketPrice());
}
if (ticketQueryObj.getMaxTicketPrice()!=null){
sql=sql+" AND ticket_price<?";
param.add(ticketQueryObj.getMaxTicketPrice());
} System.out.println(" sql="+sql); PreparedStatement preparedStatement = connection.prepareStatement(sql);
// for循环设置参数
for (int i=0;i<param.size();i++){
preparedStatement.setObject(i+1,param.get(i));
System.out.println(" 参数 ="+param.get(i));
} //4.执行
ResultSet resultSet = preparedStatement.executeQuery();
//解析结果集
while (resultSet.next()){
//取id
totalNum = resultSet.getInt("totalNum"); } //5.释放资源
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return totalNum;
}
5.封装的查询对象
package com.day03.station.query; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
public class TicketQueryObj {
/* String startStation = req.getParameter("startStation");
String stopStation = req.getParameter("stopStation");
String minTicketPrice = req.getParameter("minTicketPrice");
String maxTicketPrice = req.getParameter("maxTicketPrice");*/
private String startStation;
private String stopStation;
private String minTicketPrice;
private String maxTicketPrice;
//String currentPage = req.getParameter("currentPage");
//String pageSize = req.getParameter("pageSize");
private Integer currentPage=1;
private Integer pageSize=3;
//开始下标
private Integer startIndex=0; /**
* 开始下标计算
* @return
*/
public Integer getStartIndex() {
if (currentPage!=0 && pageSize!=0){
startIndex=(currentPage-1)*pageSize;
}
return startIndex;
} public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
} public String getStartStation() {
return startStation;
} public void setStartStation(String startStation) {
this.startStation = startStation;
} public String getStopStation() {
return stopStation;
} public void setStopStation(String stopStation) {
this.stopStation = stopStation;
} public String getMinTicketPrice() {
return minTicketPrice;
} public void setMinTicketPrice(String minTicketPrice) {
this.minTicketPrice = minTicketPrice;
} public String getMaxTicketPrice() {
return maxTicketPrice;
} public void setMaxTicketPrice(String maxTicketPrice) {
this.maxTicketPrice = maxTicketPrice;
} public Integer getCurrentPage() {
return currentPage;
} public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
} public Integer getPageSize() {
return pageSize;
} public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
6.封装的分页对象
package com.day03.station.common; import com.day03.station.model.Ticket; import java.util.List; /**
* 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html
* 疑问咨询wx:851298348
*/
public class PageResult {
private List<Ticket> data;
//当前页
private Integer currentPage=1;
//每页显示条数
private Integer pageSize=3;
//共多少条
private Integer totalNum=10;
//共多少页
private Integer totalPage=3; public List<Ticket> getData() {
return data;
} public void setData(List<Ticket> data) {
this.data = data;
} public Integer getCurrentPage() {
return currentPage;
} public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
} public Integer getPageSize() {
return pageSize;
} public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
} public Integer getTotalNum() {
return totalNum;
} public void setTotalNum(Integer totalNum) {
this.totalNum = totalNum;
} public Integer getTotalPage() {
return totalPage;
} public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
}
总结:分页的重点在于:
1.前端传递两个参数 当前页和每页显示条数
2.后端必须发送两条sql语句 一条查询分页列表,另一条查询总条数
51.js函数补充
js代码:
52.字符串比较是否相等
04_web基础(八)之车票实现增删改查初级版本的更多相关文章
- Vc数据库编程基础MySql数据库的表增删改查数据
Vc数据库编程基础MySql数据库的表增删改查数据 一丶表操作命令 1.查看表中所有数据 select * from 表名 2.为表中所有的字段添加数据 insert into 表名( 字段1,字段2 ...
- Hadoop基础-HDFS的API实现增删改查
Hadoop基础-HDFS的API实现增删改查 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客开发IDE使用的是Idea,如果没有安装Idea软件的可以去下载安装,如何安装 ...
- Java数据库连接--JDBC基础知识(操作数据库:增删改查)
一.JDBC简介 JDBC是连接java应用程序和数据库之间的桥梁. 什么是JDBC? Java语言访问数据库的一种规范,是一套API. JDBC (Java Database Connectivit ...
- JDBC基础学习(一)—JDBC的增删改查
一.数据的持久化 持久化(persistence): 把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,数据持久化意味着将内存中的数据保存到硬盘上加以固化,而持久化的实现过程大多通过各 ...
- Django框架(八)--单表增删改查,在Python脚本中调用Django环境
一.数据库连接配置 如果连接的是pycharm默认的Sqlite,不用改动,使用默认配置即可 如果连接mysql,需要在配置文件中的setting中进行配置: 将DATABASES={} 更新为 DA ...
- python基础学习之类的属性 增删改查
类中的属性如何在类外部使用代码进行增删改查呢 增加.改变: setattr内置函数以及 __setattr__魔法方法 class A: aaa = '疏楼龙宿' a = A() setattr(a, ...
- python基础之list列表的增删改查以及循环、嵌套
Python的列表在JS中又叫做数组,是基础数据类型之一,以[]括起来,以逗号隔开,可以存放各种数据类型.嵌套的列表.对象.列表是有序的,即有索引值,可切片,方便取值.列表的操作和对字符串的操作是一样 ...
- MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录
分组排序查询最大记录 //对 "catagory" 不等于 null 的数据进行分组查询,且查询结果倒序 db.getCollection('userAccount').aggre ...
- java基础: ArrayList集合应用, ArrayList增删改查详解,综合java基础实现学生管理系统,
1.ArrayList 集合和数组的区别 : 共同点:都是存储数据的容器 不同点:数组的容量是固定的,集合的容量是可变的 1.1 -ArrayList的构造方法和添加方法 public ArrayLi ...
随机推荐
- RPC通信原理
什么是 RPCRPC(Remote Procedure Call Protocol)远程过程调用协议.通俗的描述是:客户端在不知道调用细节的情况下,调用存在于远程计算上的某个过程或函数,就像调用本地应 ...
- ADO.net之综合演练
using ConsoleApplication4.App_Code; using System; using System.Collections.Generic; using System.Lin ...
- mybatis中模糊查询的方式
<!--Mapper.xml中如何进行模糊查询--> <sql id="brand_columns"> id, name, firstChar,brandN ...
- python3下全自动快速度在线安装所有插件
把下面的内容复制存为pip.conf文件, [global]timeout = 60index-url = https://pypi.python.org/simple/no-cache-dir = ...
- mezzanine的page表
class Orderable(with_metaclass(OrderableBase, models.Model)): """ Abstract model that ...
- template.js 数据渲染引擎
template.js 数据渲染引擎 template.js是一款JavaScript模板引擎,用来渲染页面的. 原理:提前将Html代码放进编写模板 <script id="tpl& ...
- <记录> PHP 缓存区ob
介绍: ob是output buffering的简称,输出缓冲区,缓冲区是通过php.ini中的output_buffering变量控制的.其默认值是off,可以设置为on来打开buffer.打来bu ...
- Ubuntu12.04 内核树建立
先查看自己使用的内核版本 lin@lin-virtual-machine:~$ uname -r --generic 如果安装系统时,自动安装了源码.在 /usr/src 目录下有对应的使用的版本目录 ...
- day30-模块和包
一.模块介绍 1.什么是模块 在python中,一个函数封装一个功能,当一个文件中包含很多个函数,而我们在其他程序中经常会用到这个文件中的功能时,那么我们就可以将这个包含多个函数的文件封装成一个模块, ...
- python 阿狸的进阶之路(8)
异常处理 http://www.cnblogs.com/linhaifeng/articles/6232220.html(转) 网络编程socket http://www.cnblogs.com/li ...