java.sql.Types
jdbc日期问题
java.sql包下的时间
他们都是java.util.Date的子类
- Date:表示日期,只有年月日,没有时分秒。会丢失时间;
- Time:表示时间,有年月日时分秒;
- Timestamp:表示时间戳,有年月日时分秒,以及毫秒。
java.util.Date
- java.util.Date – 年月日时分秒
- java.util.Calendar – Date getTime()
ps.setDate(2,new java.sql.Date(birthday.getName()))
插入时会截去时间部分
插入:
java.util.Date date = new java.util.Date();
ps.setDate(1, new Date(date.getTime()));
ps.setTime(2, new Time(date.getTime()));
ps.setTimestamp(3, new Timestamp(date.getTime()));
查出:
Date date = rs.getDate(1);
Time time = rs.getTime(2);
Timestamp timestamp = rs.getTimestamp(3);
因为继承关系:
java.util.Date date = rs.getDate(1);
java.util.Date time = rs.getTime(2);
java.util.Date timestamp = rs.getTimestamp(3);
jdbc文本文件 Clob
File file = new File("src/jdbc.java");
File file = new BufferedReader(new FileReader(file));
ps.setCharacterStream(1,reader,(int)file.lenght())
Clob clob = rs.getClob(1);
Reader reader = clob.getCharacterStream();
// 或者
// Reader reader = rs.getCharacterStream(1);
File file = new File("JdbcUtils_back.java");
Writer writer = new BufferedWriter(New FileWriter(file));
char[] buf = new char[1024];
for(int i=0;(i = reader.read(buf) > 0;)){
writer.write(buf,0,i);
}
writer.close(); // 必须关闭 最好放在finally里面
reader.close();
或者
// 将文本读取出来放进去,
// 前提是数据库必须定义为clob类型
ps.setString(1,x);
// 读取
rs.getString(1);
二进制Blob
// 存
File file = new File("");
InputStream in = new BufferedInputStream(new FileInputStream(file));
rs.setBinaryStream(1,in,(int)file.lenght());
// 取
Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream(1);
// 或者
// InputStream in = rs.getBinaryStream(1);
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[1024];
for(int i=0;(i = in.read(buf) > 0;)){
out.write(buf,0,i);
}
writer.close(); // 必须关闭 最好放在finally里面
reader.close();