时间
java.util.Date
子类:java.sql.Date 表示年月日
子类:java.sql.Time 表示时分秒
子类:java.sql.Timestamp 表示年月日时分秒(时间戳)
定义:
//java.sql.Date
Date date = new Date(System.currentTimeMillis());//当前时间
//java.sql.Timestamp
Timestamp time = new Timestamp(System.currentTimeMillis());
操作:
数据库中要比较日期时,需要自己将日期格式转成long类型数据,再形成Date对象。
public long dateToLong(String dateStr){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long time = -1;
try {
time = format.parse(dateStr).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
文本
CLOB(Character Large Object)
- 用来储存大量的文本数据
- 通常以流的方式来处理。
Mysql中相关类型:
- TINYTEXT 最大长度为255(2^8 - 1)字符的Text列
- TEXT 最大长度为65535(2^16 - 1)字符的Text列
- MEDIUMTEXT 最大长度为16777215(2^24 - 1)字符的Text列
- LONGTEXT 最大长度为4GB(2^32 - 1)字符的Text列
在setClob()方法中,第二个参数是一个流。
getClob()得到Clob对象,Clob对象有getCharacterStream()方法获得字符流。
二进制
二进制BLOB的用法和大文本字符CLOB的用法完全一样(只要把CLOB改成BLOB)。
//存
ps = con.prepareStatement("insert into student(Name,head) value (?,?) ");
ps.setString(1,"王二狗");
ps.setBlob(2,new FileInputStream("d:/icon.jpg"));
ps.execute();
//取
ps = con.prepareStatement("select * from student where Name = ?");
ps.setString(1,"王二狗");
rs = ps.executeQuery();
while(rs.next()){
Blob b = rs.getBlob("head");
//需要定义输入流
is = b.getBinaryStream();
//需要定义输出流
os = new FileOutputStream("d:/a.jpg");
int temp = 0;
while((temp = is.read()) != -1){
os.write(temp);
}
}