【SSH网上商城】Incorrect datetime value: '' for column 'pdate' at row 1
2016-01-16 20:30
531 查看
【项目说明】
在做网上商城的项目的时候,向数据库中插入数据插不进去,显示pdate(pdate为上传商品的日期)的错误,查看了一下数据库中的pdate格式为2014-11-0220:18:00,需要引用的也都引用了,具体错误提示:
【错误提示】
ERROR JDBCExceptionReporter:234 - Data truncation: Incorrectdatetime value: '' for column 'pdate' at row 1org.springframework.dao.DataIntegrityViolationException:could not insert: [cn.itcast.shop.product.vo.Product]; SQL [insert into product(pname, market_price, shop_price, image,
pdesc, is_hot, pdate, csid) values (?,?, ?, ?, ?, ?, ?, ?)]; nested exception isorg.hibernate.exception.DataException: could not insert:[cn.itcast.shop.product.vo.Product]
【解决方案】
遇到问题后查到了几种解决方案:方法一:
在jdbc里添加“&useOldAliasMetadataBehavior=true”添加了以后还是没有解决,排除!
方法二:
错误原因:一般是数据库表中的字段包含了数据库的关键字(保留字)处理方法:修改数据库中和关键字产生冲突的字段名,重新映射字段名和bean中的类名
查询了一下发现没有冲突的字段名,排除!
方法三:
对pdate使用SimpleDateFormat进行转换,转换成和数据库一致的格式:yyyy-MM-ddhh:mm:ss
先看原代码如下:
<strong>// 查询所有的商品: public String findAll() { PageBean<Product> pageBean = productService.findByPage(page); // 将PageBean数据存入到值栈中. ActionContext.getContext().getValueStack().set("pageBean", pageBean); // 页面跳转 return "findAll"; } // 跳转到添加页面的方法: public String addPage() { // 查询所有的二级分类: List<CategorySecond> csList = categorySecondService.findAll(); // 将二级分类的数据显示到页面上 ActionContext.getContext().getValueStack().set("csList", csList); // 页面跳转 return "addPageSuccess"; } // 保存商品的方法: public String save() throws IOException { // 将提交的数据添加到数据库中. product.setPdate(new Date()); // product.setImage(image); if(upload != null){ // 将商品图片上传到服务器上. // 获得上传图片的服务器端路径. String path = ServletActionContext.getServletContext().getRealPath( "/products"); // 创建文件类型对象: File diskFile = new File(path + "//" + uploadFileName); // 文件上传: FileUtils.copyFile(upload, diskFile); product.setImage("products/" + uploadFileName); } productService.save(product); return "saveSuccess"; } </strong>
改后的代码:
<strong>// 保存商品的方法: public String save() throws IOException, ParseException { // 将提交的数据添加到数据库中. Date date=new Date(); SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String date2=temp.format(date); Date date3=temp.parse(date2); product.setPdate(date3); // product.setImage(image); if(upload != null){ // 将商品图片上传到服务器上. // 获得上传图片的服务器端路径. String path = ServletActionContext.getServletContext().getRealPath( "/products"); // 创建文件类型对象: File diskFile = new File(path + "//" + uploadFileName); // 文件上传: FileUtils.copyFile(upload, diskFile); product.setImage("products/" + uploadFileName); } productService.save(product); return "saveSuccess"; }</strong>
将两个代码放到一块对比一下,看看输出的结果有什么不一样的:
// 保存商品的方法:
public String save() throws IOException, ParseException {
// 将提交的数据添加到数据库中.
Date date=new Date();
SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date2=temp.format(date);
Date date3=temp.parse(date2);
System.out.println(date3);
System.out.println(date);
if (date == date3) {
String a=null;
}
product.setPdate(date);
// product.setImage(image);
if(upload != null){
// 将商品图片上传到服务器上.
// 获得上传图片的服务器端路径.
String path = ServletActionContext.getServletContext().getRealPath(
"/products");
// 创建文件类型对象:
File diskFile = new File(path + "//" + uploadFileName);
// 文件上传:
FileUtils.copyFile(upload, diskFile);
product.setImage("products/" + uploadFileName);
}
productService.save(product);
return "saveSuccess";
}
从输出的结果上来看,没有什么不一样的,好在在这边添加了一句判断他们的输出结果是否一样,结果真没走这段代码,看来用肉眼是看不出来不同了。就这样解决了!出现这种情况原因还是没有想清楚,有遇到这种情况的可以一块探讨!
相关文章推荐
- UESTC--1272--Final Pan's prime numbers(水题)
- 内核发送uevent的API,用户空间解析uevent
- UESTC--1272--Final Pan's prime numbers(水题)
- 批处理文件:实现将写好的Android Uiautomator工程打包成jar包放到手机中执行
- UI小结.(一)
- UESTC--1269--ZhangYu Speech(模拟)
- UESTC--1269--ZhangYu Speech(模拟)
- UESTC--1265--宝贵资源(简单数学)
- UESTC--1265--宝贵资源(简单数学)
- UESTC--1264
- UESTC--1263--The Desire of Asuna(贪心)
- UESTC--1263--The Desire of Asuna(贪心)
- UESTC--1264--人民币的构造(数学规律)
- UESTC--1264--人民币的构造(数学规律)
- UESTC--1262--Memory(dfs)
- Codeforces Gym 100015G Guessing Game 差分约束
- UESTC--1262--Memory(dfs)
- UESTC 1272-Final Pan's prime numbers
- UESTC Final Pan's prime numbers 1272 (坑)
- UESTC ZhangYu Speech 1269 (模拟&技巧转换)