版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coder_Boy_/article/details/82533643
项目名称:中国电信运营支持系统-网络版(一)
开发规范说明:
1. NETCTOSS
NET 网络
C China
T Telecom 电信
O Operation 运营
S Support 支持
S System 系统
中国电信运营支持系统-网络版
2.开发步骤
1)创建项目
2)导包
- javaee tomcat
- jstl jstl
- jdbc ojdbc
- dbcp commons-dbcp
3.Servlet路径规范
1)查询资费: /findCost.do
4.JSP命名规范
1)查询资费: /WEB-INF/cost/find.jsp
资费管理部 开发图示
案例演示:
工程案例目录结构
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study</groupId>
<artifactId>netctoss</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>netctoss</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>web.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
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{
//错误的路径
throw new RuntimeException("没有这个页面");
}
}
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);
}
}
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());
}
}
}
Cost.java
package entity;
import java.io.Serializable;
import java.sql.Timestamp;
public class Cost implements Serializable {
private Integer costId;
private String name;
//基本时长
private Integer baseDuration;
//基本费用
private Double baseCost;
//单位费用
private Double unitCost;
//状态:0-启用;1-禁用
private String status;
//描述
private String descr;
//创建时间
private Timestamp creatime;
//开通时间
private Timestamp startime;
//资费类型:1-包月;2-套餐;3-计时
private String costType;
public Integer getCostId() {
return costId;
}
public void setCostId(Integer costId) {
this.costId = costId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBaseDuration() {
return baseDuration;
}
public void setBaseDuration(Integer baseDuration) {
this.baseDuration = baseDuration;
}
public Double getBaseCost() {
return baseCost;
}
public void setBaseCost(Double baseCost) {
this.baseCost = baseCost;
}
public Double getUnitCost() {
return unitCost;
}
public void setUnitCost(Double unitCost) {
this.unitCost = unitCost;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr = descr;
}
public Timestamp getCreatime() {
return creatime;
}
public void setCreatime(Timestamp creatime) {
this.creatime = creatime;
}
public Timestamp getStartime() {
return startime;
}
public void setStartime(Timestamp startime) {
this.startime = startime;
}
public String getCostType() {
return costType;
}
public void setCostType(String costType) {
this.costType = costType;
}
}
DBUtil.java
package util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 该类用来管理连接
* 数据库连接信息,保存在属性文件中
* 使用连接池获取连接
* @author Cher_du
*
*/
public class DBUtil {
private static BasicDataSource ds;
static{
//加载属性文件数据
Properties prop = new Properties();
try {
prop.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
String driverclass = prop.getProperty("jdbc.driverclass");
String url=prop.getProperty("jdbc.url");
String user = prop.getProperty("jdbc.user");
String password = prop.getProperty("jdbc.password");
String strMaxActive =prop.getProperty("dbcp.maxActive");
String strInitSize =prop.getProperty("dbcp.initSize");
//实例化,并初始化连接池
ds = new BasicDataSource();
ds.setDriverClassName(driverclass);
ds.setUrl(url);
ds.setUsername(user);
ds.setPassword(password);
ds.setMaxActive(Integer.parseInt(strMaxActive));
ds.setInitialSize(Integer.parseInt(strInitSize));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("读取属性文件错误",e);
}
}
//2、创建连接
public static Connection getConnection() throws SQLException{
return ds.getConnection();
}
//3、归还连接
public static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("归还连接错误",e);
}
}
}
//测试
public static void main(String[] args) throws SQLException {
Connection conn = getConnection();
System.out.println(conn.getClass().getName());
close(conn);
}
}
db.properties
jdbc.driverclass=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.user=learn
jdbc.password=learn
dbcp.maxActive=1
dbcp.initSize=1
后台数据库sql脚本:
--资费信息表
create table cost(
cost_id number(4) primary key,
name varchar(50) not null,
base_duration number(11),
base_cost number(7,2),
unit_cost number(7,4),
status char(1),
descr varchar2(100),
creatime date default sysdate ,
startime date,
cost_type char(1)
);
create sequence cost_seq start with 100;
INSERT INTO COST VALUES (1,'5.9元套餐',20,5.9,0.4,0,'5.9元20小时/月,超出部分0.4元/时',DEFAULT,DEFAULT,'2');
INSERT INTO COST VALUES (2,'6.9元套餐',40,6.9,0.3,0,'6.9元40小时/月,超出部分0.3元/时',DEFAULT,DEFAULT,'2');
INSERT INTO COST VALUES (3,'8.5元套餐',100,8.5,0.2,0,'8.5元100小时/月,超出部分0.2元/时',DEFAULT,DEFAULT,'2');
INSERT INTO COST VALUES (4,'10.5元套餐',200,10.5,0.1,0,'10.5元200小时/月,超出部分0.1元/时',DEFAULT,DEFAULT,'2');
INSERT INTO COST VALUES (5,'计时收费',null,null,0.5,0,'0.5元/时,不使用不收费',DEFAULT,DEFAULT,'3');
INSERT INTO COST VALUES (6,'包月',null,20,null,0,'每月20元,不限制使用时间',DEFAULT,DEFAULT,'1');
COMMIT;
将netctoss工程部署到Tomcat上,运行Tomcat启动案例工程,
浏览器录入http://localhost:8080/netctoss/findCost.do 即可:如果没有错误,最终页面显示效果应如下图:
点击启用与删除按钮: