项目名称:中国电信运营支持系统-网络版(五)
删除资费功能:
案例演示:
工程案例目录结构
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" %>
案例-NetCTOSS
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 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 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 list = new ArrayList();
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 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 即可:如果没有错误,最终页面显示效果应如下图:
点击某条记录进行删除: