model层操作时,数据库中的一些列不能为null,一些列不能接受String类型的存入时,一些解决的办法
来源:互联网 发布:网络文化建设重点内容 编辑:程序博客网 时间:2024/05/09 08:22
在电信资费管理系统中
数据库中的套餐名不能为空
如果用户增加新的套餐,但并未输入套餐名,那么就会导致DAO操作出现错误
解决办法:
if(name==null ||name.equals("")){
res.sendRedirect("findAll.go");
return;
}
则后续代码都不执行。
如果套餐中,有一些套餐属性默认为空,那么需要加上判断来避免空指针异常
解决办法:
if(basecost!=null && !basecost.equals("")){
c.setBaseCost(new Double(basecost));
}
if(baseduration!=null && !baseduration.equals("")){
c.setBaseDuration(new Integer(baseduration));
}
if(unitcost!=null && !unitcost.equals("")){
c.setUnitCost(new Double(unitcost));
}
同时在DAO操作时
这三个Integer和Double类型的数据存入时,可能为空
那么需要:
ps.setObject(2, c.getBaseDuration());
ps.setObject(3, c.getBaseCost());
ps.setObject(4, c.getUnitCost());
DAO操作的完整方法
public void save(Cost c){
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,c.getName());
ps.setObject(2,c.getBaseDuration());
ps.setObject(3,c.getBaseCost());
ps.setObject(4,c.getUnitCost());
ps.setString(5,c.getDescr());
ps.setString(6,c.getCostType());
ps.executeUpdate();
} catch (Exceptione) {
e.printStackTrace();
thrownew RuntimeException("增加新套餐失败");
}finally{
DBUtil.closeConnection(conn);
}
}
servlet里的addCost方法protected void addCost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//设置JSP中提交form时的method为post,同时把请求编码设置为utf-8
req.setCharacterEncoding("utf-8");
//接受表单请求中附带的数据
String name =req.getParameter("name");
String descr =req.getParameter("descr");
String costType =req.getParameter("costtype");
String basecost =req.getParameter("basecost");
String baseduration =req.getParameter("baseduration");
String unitcost =req.getParameter("unitcost");
//实例化Cost对象并把接收获得的参数set给Cost
Cost c = new Cost();
if(name==null ||name.equals("")){
res.sendRedirect("findAll.go");
return;
}
c.setName(name);
c.setDescr(descr);
c.setCostType(costType);
if(basecost!=null && !basecost.equals("")){
c.setBaseCost(new Double(basecost));
}
if(baseduration!=null && !baseduration.equals("")){
c.setBaseDuration(new Integer(baseduration));
}
if(unitcost!=null && !unitcost.equals("")){
c.setUnitCost(new Double(unitcost));
}
//调用DAO里的save方法
CostDao dao =new CostDao();
dao.save(c);
//保存后,重定向到查询所有套餐页面
res.sendRedirect("findAll.go");
}
- model层操作时,数据库中的一些列不能为null,一些列不能接受String类型的存入时,一些解决的办法
- 今天在数据库中做插入时,提醒“当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'TB_Admin_Role' 中的标识列插入显式值。”的问题。
- 关于解决真机调试时DDMS里不能打开data文件夹时遇到的一些列问题
- 关于oracle采用pivot函数列转行后一些列的值为null处理
- 解决不能上Google的一些方法
- mysql 字段为NULL的一些操作
- Db2数据库中错误提示:23502,不能把null 直插入到定义为not null 的列中
- 来自数据源的string类型的给定值不能转换为指定目标列的类型nvarchar
- 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 nvarchar
- 索引的一些案列
- 对数据库中列的一些基本的操作的SQL命令
- 发布网站到IIS的一些列操作和问题
- 关于矩阵分行列的一些简单操作。
- 查看目标数据库的列名 我提供一些函数
- 解决内存不能为written的办法
- 散列的一些概念——基础中的基础
- 解决一些网站文字不能复制的问题?java应用
- 解决“当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'Category' 中的标识列插入显式值。”
- 集群中Ntp时间同步
- 第一次开通博客_____试水
- 拥抱区块链和金融科技,开启新金融时代
- 全球软件大会QCon上海2016—PDF分享
- ebs blank
- model层操作时,数据库中的一些列不能为null,一些列不能接受String类型的存入时,一些解决的办法
- Maven仓库镜像下载jar包好慢
- 51 Nod 1086 背包问题 V2
- 把中缀表达式转换为表达式树
- 集合
- 22 素数求和问题
- 《剑指》41
- 初涉Linux命令
- BurpSuite实现AES加密插件问题记录