项目名称:中国电信运营支持系统-网络版(五)
删除资费功能:
删除资费
案例演示:
工程案例目录结构
find.jsp
<%@page pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>案例-NetCTOSS</title>
<!--
当前:/netctoss/findCost.do
目标:/netctoss/styles/global.css
-->
<link type="text/css" rel="stylesheet" media="all" href="styles/global.css"/>
<link type="text/css" rel="stylesheet" media="all" href="styles/global_color.css"/>
<script language="javascript" type="text/javascript">
//排序按钮的点击事件
function sort(btnObj){
if(btnObj.className=="sort_desc"){
btnObj.className="sort_asc";
}else{
btnObj.className="sort_desc";
}
}
//启用
function startFee(){
var r = window.confirm("确定要启用此资费吗?资费启用后将不能修改和删除。")
document.getElementById("operate_result_info").style.display="block";
}
//删除
function deleteFee(costId){
var r = window.confirm("确定要删除此资费吗?");
if(r){
location.href="deleteCost.do?costId="+costId;
}
}
</script>
</head>
<body>
<!-- Logo区域开始 -->
<div id="header">
<img src="images/logo.png" alt="logo" class="left" />
<a href="#">[退出]</a>
</div>
<!-- Logo区域结束 -->
<!-- 导航区域开始 -->
<div id="navi">
<ul id="menu">
<li><a href="../index.html" class="index_off"></a></li>
<li><a href="../role/role_list.html" class="role_off"></a></li>
<li><a href="../admin/admin_list.html" class="admin_off"></a></li>
<li><a href="../fee/fee_list.html" class="fee_off"></a></li>
<li><a href="../account/account_list.html" class="account_off"></a></li>
<li><a href="../service/service_list.html" class="service_off"></a></li>
<li><a href="../bill/bill_list.html" class="bill_off"></a></li>
<li><a href="../report/report_list.html" class="report_off"></a></li>
<li><a href="../user/user_info.html" class="information_off"></a></li>
<li><a href="../user/user_modi_pwd.html" class="password_off"></a></li>
</ul>
</div>
<!-- 导航区域结束 -->
<!-- 主要区域开始 -->
<div id="main">
<form action="" method="">
<!-- 排序 -->
<div class="search_add">
<div>
<!-- <input type="button" value="月租" class="sort_asc" onclick="sort(this)"/>
<input type="button" value="基费" class="sort_asc" onclick="sort(this)"/>
<input type="button" value="时长" class="sort_asc" onclick="sort(this)"/> -->
</div>
<!--
当前:/netctoss/findCost.do
目标:/netctoss/toAddCost.do
-->
<input type="button" value="增加" class="btn_add" onclick="location.href='toAddCost.do';" />
</div>
<!-- 启用操作的操作提示 -->
<div id="operate_result_info" class="operate_success">
<img src="images/close.png" alt="" onclick="this.parentNode.style.display='none';"/>
删除成功!
</div>
<!-- 数据区域:用表格展示数据 -->
<div id="data">
<table id="datalist">
<tr>
<th>资费ID</th>
<th class="width100">资费名称</th>
<th>基本时长</th>
<th>基本费用</th>
<th>单位费用</th>
<th>创建时间</th>
<th>开通时间</th>
<th class="width50">状态</th>
<th class="width200"></th>
</tr>
<c:forEach items="${costs }" var="c">
<!-- <tr>
<td>1</td>
<td><a href="fee_detail.html">包 20 小时</a></td>
<td>20 小时</td>
<td>24.5 元</td>
<td>3.00 元</td>
<td>2013/01/01 00:00:00</td>
<td></td>
<td>暂停</td>
<td>
<input type="button" value="启用" class="btn_start" onclick="startFee();"/>
<input type="button" value="修改" class="btn_modify" onclick="location.href='fee_modi.html'"/>
<input type="button" value="删除" class="btn_delete" onclick="deleteFee();"/>
</td>
</tr>
<tr>
<td>2</td>
<td><a href="fee_detail.html">包 40 小时</a></td>
<td>40 小时</td>
<td>40.50 元</td>
<td>3.00 元/小时</td>
<td>2013/01/21 00:00:00</td>
<td>2013/01/23 00:00:00</td>
<td>开通</td>
<td></td>
</tr> -->
<tr>
<td>${c.costId }</td>
<td><a href="fee_detail.html">${c.name }</a></td>
<td>${c.baseDuration } 小时</td>
<td>${c.baseCost } 元</td>
<td>${c.unitCost } 元/小时</td>
<td><fmt:formatDate value="${c.creatime }" pattern="yyyy/MM/dd" /></td>
<td><fmt:formatDate value="${c.startime }" pattern="yyyy/MM/dd" /></td>
<td>
<c:if test="${c.status=='0' }">开通</c:if>
<c:if test="${c.status=='1' }">暂停</c:if>
</td>
<td>
<input type="button" value="启用" class="btn_start" onclick="startFee();"/>
<input type="button" value="修改" class="btn_modify" onclick="location.href='toUpdateCost.do?id=${c.costId }';"/>
<input type="button" value="删除" class="btn_delete" onclick="deleteFee(${c.costId });"/>
</td>
</tr>
</c:forEach>
</table>
<p>业务说明:<br />
1、创建资费时,状态为暂停,记载创建时间;<br />
2、暂停暂停下,可修改,可删除<br />
3、开通后,记载开通时间,且开通后不能修改、不能再停用、也不能删除<br />
4、业务账号修改资费时,在下月底统一触发,修改其关联的资费ID(此触发动作由程序处理)<br />
</p>
</div>
<!-- 分页 -->
<div id="pages">
<a href="#">上一页</a>
<a href="#" class="current_page">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">下一页</a>
</div>
</form>
</div>
<!-- 主要区域结束 -->
<div id="footer">
<p>[源自东北的技术,最牛逼的老师,最真实的企业环境,最实用的实战项目]</p>
<p>版权所有(C)牛逼学院IT培训集团公司</p>
</div>
</body>
</html>
MainServlet.java
package web;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.CostDao;
import entity.Cost;
public class MainServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
String path = req.getServletPath();
if("/findCost.do".equals(path)){
//查询资费
System.out.println(path);
findCost(req,res);
}else if("/toAddCost.do".equals(path)){
//打开增加资费页
toAddCost(req,res);
}else if("/addCost.do".equals(path)){
//增加保存资费
addCost(req,res);
}else if("/toUpdateCost.do".equals(path)){
//打开修改资费页
toUpdateCost(req,res);
}else if("/updateCost.do".equals(path)){
//修改资费
updateCost(req,res);
}else if("/deleteCost.do".equals(path)){
//删除资费
deleteCost(req,res);
}else{
//错误的路径
throw new RuntimeException("没有这个页面");
}
}
protected void deleteCost(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String costId = req.getParameter("costId");
CostDao dao = new CostDao();
dao.delete(new Integer(costId));
System.out.println("deleteCost......");
//3.重定向到查询
//当前:/netctoss/deleteCost.do
//目标:/netctoss/findCost.do
res.sendRedirect("findCost.do");
}
protected void updateCost(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//1.接收表单数据
String costId = req.getParameter("costId");
String name = req.getParameter("name");
String costType = req.getParameter("costType");
String descr = req.getParameter("descr");
String baseDuration = req.getParameter("baseDuration");
String baseCost = req.getParameter("baseCost");
String unitCost = req.getParameter("unitCost");
//2.保存这些数据
Cost c = new Cost();
c.setCostId(new Integer(costId));
c.setName(name);
c.setCostType(costType);
c.setDescr(descr);
if(baseDuration !=null && !baseDuration.equals("")){//
c.setBaseDuration(new Integer(baseDuration));
}
if(baseCost !=null && !baseCost.equals("")){
c.setBaseCost(new Double(baseCost));
}
if(unitCost !=null && !unitCost.equals("")){
c.setUnitCost(new Double(unitCost));
}
CostDao dao = new CostDao();
System.out.println(c);
dao.update(c);
//3.重定向到查询
//当前:/netctoss/addCost.do
//目标:/netctoss/findCost.do
res.sendRedirect("findCost.do");
}
protected void toUpdateCost(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
System.out.println("toUpdateCost.......");
//接收参数
String id = req.getParameter("id");
//查询要修改的资费
CostDao dao = new CostDao();
Cost cost = dao.findById(new Integer(id));
//转发到修改页
req.setAttribute("cost", cost);
req.getRequestDispatcher("WEB-INF/cost/update.jsp").forward(req, res);
}
protected void addCost(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//1.接收表单数据
String name = req.getParameter("name");
String costType = req.getParameter("costType");
String descr = req.getParameter("descr");
String baseDuration = req.getParameter("baseDuration");
String baseCost = req.getParameter("baseCost");
String unitCost = req.getParameter("unitCost");
//2.保存这些数据
Cost c = new Cost();
c.setName(name);
c.setCostType(costType);
c.setDescr(descr);
if(baseDuration !=null && !baseDuration.equals("")){//
c.setBaseDuration(new Integer(baseDuration));
}
if(baseCost !=null && !baseCost.equals("")){
c.setBaseCost(new Double(baseCost));
}
if(unitCost !=null && !unitCost.equals("")){
c.setUnitCost(new Double(unitCost));
}
CostDao dao = new CostDao();
dao.save(c);
//3.重定向到查询
//当前:/netctoss/addCost.do
//目标:/netctoss/findCost.do
res.sendRedirect("findCost.do");
}
protected void toAddCost(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//当前:/netctoss/toAddCost.do
//目标:/netctoss/WEB-INF/cost/add.jsp
req.getRequestDispatcher("WEB-INF/cost/add.jsp").forward(req, res);
}
protected void findCost(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//查询资费
CostDao dao = new CostDao();
List<Cost> list = dao.findAll();
//转发到查询页面
req.setAttribute("costs", list);
//当前:/netctoss/findCost.dao
//目标:/netctoss/WEB-INF/cost/find.jsp
System.out.println("into--findCost");
req.getRequestDispatcher("WEB-INF/cost/find.jsp").forward(req, res);
}
}
CostDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import entity.Cost;
import util.DBUtil;
public class CostDao {
public List<Cost> findAll(){
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from cost "
+"order by cost_id";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs= ps.executeQuery();
List<Cost> list = new ArrayList<Cost>();
while(rs.next()){
Cost c = new Cost();
c.setCostId(rs.getInt("cost_id"));
c.setName(rs.getString("name"));
c.setBaseDuration(rs.getInt("base_duration"));
c.setBaseCost(rs.getDouble("base_cost"));
c.setUnitCost(rs.getDouble("unit_cost"));
c.setStatus(rs.getString("status"));
c.setDescr(rs.getString("descr"));
c.setCreatime(rs.getTimestamp("creatime"));
c.setStartime(rs.getTimestamp("startime"));
c.setCostType(rs.getString("cost_type"));
list.add(c);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("查询资费失败",e);
}finally{
DBUtil.close(conn);
}
}
/**
* Alt+Shift+M
* @param rs
* @return
* @throws SQLException
*/
private Cost createCost(ResultSet rs) throws SQLException {
Cost c = new Cost();
c.setCostId(rs.getInt("cost_id"));
c.setName(rs.getString("name"));
c.setBaseDuration(rs.getInt("base_duration"));
c.setBaseCost(rs.getDouble("base_cost"));
c.setUnitCost(rs.getDouble("unit_cost"));
c.setStatus(rs.getString("status"));
c.setDescr(rs.getString("descr"));
c.setCreatime(rs.getTimestamp("creatime"));
c.setStartime(rs.getTimestamp("startime"));
c.setCostType(rs.getString("cost_type"));
return c;
}
/**
* 状态默认为暂停态1;
* 创建时间默认为系统时间
* 开通时间默认为null;
* @param cost
*/
public void save(Cost cost) {
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "insert into cost values("
+"cost_seq.nextval,"
+"?,?,?,?,'1',?,sysdate,null,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, cost.getName());
//setInt/SetDouble不允许传入null;
//但当前业务中这些字段是允许为null;
//将方法换成setObject
ps.setObject(2, cost.getBaseDuration());
ps.setObject(3, cost.getBaseCost());
ps.setObject(4, cost.getUnitCost());
ps.setString(5, cost.getDescr());
ps.setString(6, cost.getCostType());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("增加资费失败",e);
}finally{
DBUtil.close(conn);
}
}
public Cost findById(int id) {
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from cost "
+"where cost_id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if(rs.next()){
return createCost(rs);
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("查询资费失败",e);
}finally{
DBUtil.close(conn);
}
return null;
}
public void update(Cost cost) {
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "update cost set "
+"name=?,"
+"base_duration=?,"
+"base_cost=?,"
+"unit_cost=?,"
+"descr=?,"
+"cost_type=? "
+"where cost_id=?";
System.out.println(sql);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, cost.getName());
//setInt/SetDouble不允许传入null;
//但当前业务中这些字段是允许为null;
//将方法换成setObject
ps.setObject(2, cost.getBaseDuration());
ps.setObject(3, cost.getBaseCost());
ps.setObject(4, cost.getUnitCost());
ps.setString(5, cost.getDescr());
ps.setString(6, cost.getCostType());
ps.setInt(7, cost.getCostId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("修改资费失败",e);
}finally{
DBUtil.close(conn);
}
}
/**
* 删除指定资费ID 的资费记录
* @param integer
*/
public void delete(Integer costId) {
Connection conn = null;
System.out.println("delete.........");
try {
conn = DBUtil.getConnection();
String sql = "delete from cost where cost_Id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, costId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("删除资费失败",e);
}finally{
DBUtil.close(conn);//如果不加此代码 会出现网页宕住
}
}
public static void main(String[] args) {
// CostDao dao = new CostDao();
// List<Cost> list = dao.findAll();
// for(Cost c: list){
// System.out.println(c.getCostId()+","+c.getName());
// }
CostDao dao = new CostDao();
Cost c = new Cost();
c.setName("包月");
//c.setBaseDuration(600);
c.setBaseCost(800.0);
//c.setUnitCost(0.6);
c.setDescr("包月很爽");
c.setCostType("1");
dao.save(c);
}
}
将netctoss工程部署到Tomcat上,运行Tomcat启动案例工程,
浏览器录入http://localhost:8080/netctoss/findCost.do 即可:如果没有错误,最终页面显示效果应如下图:
点击某条记录进行删除:
删除成功!