0%

关于数据库中date类型时间处理

2017年9月17日 下午2:16

概述

  1. 首先明白一点:当数据库字段用date类型时,我们的一条sql针对于date类型时可以直接使用时间字符串表示的。
    1. 例如insert into student(name,sex,age,address,birthday,remark) values('陈志恒','男','22','太原','1995-01-01','无')
  2. 说一下整个关于日期的处理步骤
    1. 第一步:从前台到数据库
      1. 从前台取到的是string类型
      2. string类型——>java.util.Date date类型
      3. java.util.Date date类型——>java.sql.Date(date.getTime()) 类型
    2. 第二步:从数据库到前台
      1. java.sql.Date()类型 ——>java.util.Date(rs.getDate(i).getTime()))类型
  3. 当我们需要对按时间进行where查询的时候
    1. 在mysql中使用datediff(时间差)函数
    2. 一共有三个参数,参考下面的连接
    3. 值得注意的一点是:这里的参数直接使用字符串类型就行。这就省去了我们转换的麻烦
    4. 链接
      1. 关于SQL模糊查询日期时间的方法_百度知道
      2. Java String 转成 Mysql Date - Hi, Sun - ITeye博客

排错

错误:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
参考:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException - ye1992的专栏 - CSDN博客
关键:map.put(rs.getMetaData().getColumnName(i),new java.util.Date(rs.getDate(i).getTime()));

源码说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 按条件查询
* 按时间查询的完整例子
*/
@Override
public List<Map<String, Object>> queryForListOnCondition(Page p, String condition, String condition_date) {
String sql1 ="select count(1) from student where name like '%"+condition+"%' and datediff(day,date,'"+condition_date+"') < 0 ";
int count = dao.queryForCount(sql1);
p.setCount(count);

// String sql = "select * from student limit "+(p.getPageNum()-1)*p.getSize()+","+p.getSize();
// return dao.queryForList(sql);

String sql="select * from student where name like '%"+condition+"%' limit "+(p.getPageNum()-1)*p.getSize()+","+p.getSize() ;
System.out.println(sql);
return dao.queryForList(sql);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 增加保存
* 第一步:从前台到数据库(非预处理)
*/
@Override
public int addSave(String name, String sex, String age, String address, String birthday) {
java.util.Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
String sql = "insert into student(name,sex,age,address,birthday,remark) values('"+name+"','"+sex+"',"+age+",'"+address+"',"+"'"+new java.sql.Date(date.getTime())+"',"+"'无'"+")";
System.out.println(sql);
return dao.excuteUpdate(sql);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 更新保存
* 第一步:从前台到数据库(预处理)
*/
@Override
public int updateSave(String id, String name, String sex, String age, String address, String birthday) {
java.util.Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
String sql = "update student set name=?,sex=?,age=?,address=?,birthday=? where id=?";
int[] types={Types.VARCHAR,Types.VARCHAR,Types.INTEGER,Types.VARCHAR,Types.DATE,Types.INTEGER};
Object[] obj = {name,sex,age,address,new java.sql.Date(date.getTime()),id};
return dao.excuteUpdate(sql, types, obj);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 更新保存
* 第二步:从数据库到前台(放到map中)
*/
private List<Map<String, Object>> rsToList(ResultSet rs) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
while(rs.next()){
Map<String, Object> map = new HashMap<String,Object>();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
switch (rs.getMetaData().getColumnType(i)) {
case Types.INTEGER:
map.put(rs.getMetaData().getColumnName(i),rs.getInt(i));
break;
case Types.VARCHAR:
map.put(rs.getMetaData().getColumnName(i),rs.getString(i));
break;
default:
map.put(rs.getMetaData().getColumnName(i),new java.util.Date(rs.getDate(i).getTime()));
// rs.getObject(i)
}
}
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}