电力局自助缴费系统——试题讲解
本试题使用Jsp+Servlet+Mysql+JDBC等技术实现电力局自助缴费系统。
语言和环境
A、实现语言
Java
B、环境要求
JDK1.8、Eclipse、Tomcat7、JSP、Servlet、Mysql
功能要求
某电力局计划开发一在线缴费系统,实现在线通过银联卡缴费的功能,系统界面功能如下:
1)客户输入客户号,可以查询欠费信息
2)如果确有欠费,可以输入银联卡号,在线扣款。
界面如下:
1)登录界面
2)用户查询结果:
如果没有欠费则显示如下界面:
缴费成功界面如下:
要求
1)卡号不存在需要给出提示
2)如果无欠费信息,则让缴费按钮变成不可用
3)如果银联卡号错误,或者余额不足,要给出提示
表结构如下:
视频讲解及源代码地址
https://download.youkuaiyun.com/download/pcbhyy/10763248
主要源代码
工具类:JDBCUitls
package com.yy.dao;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
//重构
public class JDBCUitls {
private static String driverClassName;
private static String url;
private static String username;
private static String password;
static {
try {
Properties props = new Properties();
InputStream in = JDBCUitls.class.getResourceAsStream("/db.properties");
props.load(in);
driverClassName = props.getProperty("driverClassName");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
Class.forName(driverClassName);
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
//得到连接对象
public static Connection getConnection() throws SQLException {
Connection conn = DriverManager.getConnection(url,username,password);
return conn;
}
//关闭连接对象
public static void closeConnection(Connection conn) throws SQLException {
if(conn != null) {
conn.close();
}
}
public static int executeUpdate(String sql,Object... params) throws SQLException {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
if(params != null) {
//给占位符赋值
for(int i = 0;i < params.length;i++) {
ps.setObject(i+1, params[i]);
}
}
int n = ps.executeUpdate();
conn.close();
return n;
}
public static ResultSet executeQuery(Connection conn,String sql,Object... params) throws SQLException {
PreparedStatement ps = conn.prepareStatement(sql);
if(params != null) {
//给占位符赋值
for(int i = 0;i < params.length;i++) {
ps.setObject(i+1, params[i]);
}
}
ResultSet rs = ps.executeQuery();
return rs;
}
}
属性文件:db.properties(放在classpath路径下)
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydata
username=root
password=root
数据访问层:EleCustDao
package com.yy.dao;
import com.yy.entity.EleCust;
public interface EleCustDao {
public EleCust getById(String cardId) throws Exception;
public int update(String cardId) throws Exception;
}
数据访问层:EleCustDaoImpl
package com.yy.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import com.yy.entity.EleCust;
public class EleCustDaoImpl implements EleCustDao {
@Override
public EleCust getById(String cardId) throws Exception {
String sql = "select * from Ele_cust where card_id = ?";
Connection conn = JDBCUitls.getConnection();
ResultSet rs = JDBCUitls.executeQuery(conn, sql, cardId);
EleCust eleCust = null;
String uname;
String addr;
Double ownMoney;
if(rs.next()) {
uname = rs.getString("uname");
addr = rs.getString("addr");
ownMoney = rs.getDouble("own_money");
eleCust = new EleCust(cardId, uname, addr, ownMoney);
}
JDBCUitls.closeConnection(conn);
return eleCust;
}
@Override
public int update(String cardId) throws Exception {
String sql = "update Ele_cust set own_money = 0 where card_id = ?";
int n = JDBCUitls.executeUpdate(sql, cardId);
return n;
}
}
数据访问层:BankCustDao
package com.yy.dao;
import com.yy.entity.BankCust;
public interface BankCustDao {
public BankCust getById(String cardId) throws Exception;
public int update(String cardId,double money) throws Exception;
}
数据访问层:BankCustDaoImpl
package com.yy.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import com.yy.entity.BankCust;
public class BankCustDaoImpl implements BankCustDao {
@Override
public BankCust getById(String cardId) throws Exception {
String sql = "select * from bank_cust where card_id = ?";
Connection conn = JDBCUitls.getConnection();
ResultSet rs = JDBCUitls.executeQuery(conn, sql, cardId);
BankCust bankCust = null;
Double balance;
if(rs.next()) {
balance = rs.getDouble("balance");
bankCust = new BankCust(cardId, balance);
}
JDBCUitls.closeConnection(conn);
return bankCust;
}
@Override
public int update(String cardId, double money) throws Exception {
String sql = "update bank_cust set balance = balance - ? where card_id = ?";
return JDBCUitls.executeUpdate(sql, money,cardId);
}
}
业务逻辑层:EleCustService
package com.yy.service;
import com.yy.entity.EleCust;
public interface EleCustService {
public EleCust getById(String cardId) throws Exception;
public int update(String cardId) throws Exception;
}
业务逻辑层:EleCustServiceImpl
package com.yy.service;
import com.yy.dao.EleCustDao;
import com.yy.dao.EleCustDaoImpl;
import com.yy.entity.EleCust;
public class EleCustServiceImpl implements EleCustService {
private EleCustDao eleCustDao = new EleCustDaoImpl();
@Override
public EleCust getById(String cardId) throws Exception {
return eleCustDao.getById(cardId);
}
@Override
public int update(String cardId) throws Exception {
return eleCustDao.update(cardId);
}
}
业务逻辑层:BankCustService
package com.yy.service;
import com.yy.entity.BankCust;
public interface BankCustService {
public BankCust getById(String cardId) throws Exception;
public int update(String cardId,double money) throws Exception;
}
业务逻辑层:BankCustServiceImpl
package com.yy.service;
import com.yy.dao.BankCustDao;
import com.yy.dao.BankCustDaoImpl;
import com.yy.entity.BankCust;
public class BankCustServiceImpl implements BankCustService {
private BankCustDao bankCustDao = new BankCustDaoImpl();
@Override
public BankCust getById(String cardId) throws Exception {
return bankCustDao.getById(cardId);
}
@Override
public int update(String cardId, double money) throws Exception {
return bankCustDao.update(cardId, money);
}
}
控制器:EleCustServlet
package com.yy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yy.entity.EleCust;
import com.yy.service.EleCustService;
import com.yy.service.EleCustServiceImpl;
/**
* Servlet implementation class EleCustServlet
*/
public class EleCustServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EleCustServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
try {
if("getSearch".equals(method)) {
doGetSearch(request,response);
}else if("search".equals(method)) {
doSearch(request,response);
}
}catch(Exception e) {
e.printStackTrace();
}
}
private void doSearch(HttpServletRequest request, HttpServletResponse response) throws Exception {
String cardId = request.getParameter("cardId");
EleCustService eleCustService = new EleCustServiceImpl();
EleCust eleCust = eleCustService.getById(cardId);
request.setAttribute("eleCust", eleCust);
request.getRequestDispatcher("/WEB-INF/elecust/result.jsp").forward(request, response);
}
private void doGetSearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/elecust/search.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
控制器:BankCustServlet
package com.yy.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yy.entity.BankCust;
import com.yy.entity.EleCust;
import com.yy.service.BankCustService;
import com.yy.service.BankCustServiceImpl;
import com.yy.service.EleCustService;
import com.yy.service.EleCustServiceImpl;
/**
* Servlet implementation class BankCustServlet
*/
public class BankCustServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BankCustServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
if("pay".equals(method)) {
try {
doPay(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void doPay(HttpServletRequest request, HttpServletResponse response) throws Exception {
String cardId = request.getParameter("cardId");
Double ownMoney = Double.parseDouble(request.getParameter("ownMoney"));
BankCustService bankCustService = new BankCustServiceImpl();
BankCust bankCust = bankCustService.getById(cardId);
String message = null;
if(bankCust == null) {
message = "银行卡号不存在,请重新输入!";
}else if(bankCust.getBalance() < ownMoney){
message = "银行卡上余额不足!";
}else {
int n = bankCustService.update(cardId, ownMoney);
EleCustService eleCustService = new EleCustServiceImpl();
String eleCardId = request.getParameter("eleCardId");
int m = eleCustService.update(eleCardId);
if(n == 1 && m == 1) {
message = "缴费成功!";
EleCust cust = eleCustService.getById(eleCardId);
request.setAttribute("payMoney", ownMoney);
request.setAttribute("eleCust", cust);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
request.setAttribute("date", f.format(new Date()));
}else {
message = "缴费失败!";
}
}
request.setAttribute("message", message);
request.getRequestDispatcher("/WEB-INF/elecust/payresult.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
实体类:EleCust
package com.yy.entity;
public class EleCust {
private String cardId;
private String uname;
private String addr;
private Double ownMoney;//欠费金额
public EleCust() {
super();
// TODO Auto-generated constructor stub
}
public EleCust(String cardId, String uname, String addr, Double ownMoney) {
super();
this.cardId = cardId;
this.uname = uname;
this.addr = addr;
this.ownMoney = ownMoney;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public Double getOwnMoney() {
return ownMoney;
}
public void setOwnMoney(Double ownMoney) {
this.ownMoney = ownMoney;
}
}
实体类:BankCust
package com.yy.entity;
public class BankCust {
private String cardId;
private Double balance;
public BankCust() {
super();
// TODO Auto-generated constructor stub
}
public BankCust(String cardId, Double balance) {
super();
this.cardId = cardId;
this.balance = balance;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
}
页面:search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="text-align: center;">
<form action="${ pageContext.request.contextPath }/EleCustServlet?method=search" method="post">
单号:<input type="text" name="cardId" >
<input type="submit" value="查询">
</form>
</div>
</body>
</html>
页面:result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${ pageContext.request.contextPath }/BankCustServlet?method=pay" method="post">
<table border="1" width="500" align="center">
<tr>
<td>用户姓名</td>
<td>${ eleCust.uname }</td>
</tr>
<tr>
<td>单号</td>
<td>${ eleCust.cardId }</td>
<input type="hidden" name="eleCardId" value="${ eleCust.cardId }">
</tr>
<tr>
<td>地址</td>
<td>${ eleCust.addr }</td>
</tr>
<tr>
<td>目前欠费</td>
<td>${ eleCust.ownMoney }元</td>
<input type="hidden" name="ownMoney" value="${ eleCust.ownMoney }">
</tr>
<tr>
<td>银行卡号</td>
<td>
<input type="text" name="cardId">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<c:if test="${ eleCust.ownMoney == 0 }">
您没有欠费记录,感谢您的支持
</c:if>
<c:if test="${ eleCust.ownMoney > 0 }">
</c:if>
</td>
</tr>
</table>
<input type="submit" value="缴费" ${ (eleCust.ownMoney == 0)?"disabled":"" }>
</form>
</body>
</html>
页面:payresult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${ message == '缴费成功!' }">
尊敬的 ${ eleCust.uname },您于 ${ date } 号 为 用电单号 ${ eleCust.cardId } 缴费为 ${ payMoney }元,感谢您对供电事业的支持
</c:if>
<c:if test="${ message != '缴费成功!' }">
${ message }
</c:if>
</body>
</html>