to_date("要转换的字符串","转换的格式")
两个参数的格式必须匹配,否则会报错。即按照第二个参数的格式解释第一个参数。
to_char(日期,"转换格式" )
即把给定的日期按照“转换格式”转换。
例如:
select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;
select to_date('2009-12-25 14:23:31','yyyy-mm-dd,hh24:mi:ss') from dual
注释:转换的格式:
表示year的:
表示month的:
表示day的:
表示hour的:
表示minute的:
表示second的:
表示季度的:
另外还有ww 用来表示当年第几周 w用来表示当月第几周。
24小时制下的时间范围:00:00:00-23:59:59
12小时制下的时间范围:1:00:00-12:59:59
我们在应用层用到的日期类型是 java.util包中,而在jdbc中的数据类型是java.sql包中,所以我们在处理时要做相应的转换。
ps.setCharacterStream(int parameterIndex,
Reader reader)
throws SQLException
ps.setClob(int parameterIndex,
Clob x)
throws SQLException
ps.setClob(int parameterIndex,
Reader reader)
throws SQLException
InputStream is = rs.getAsciiStream(String columnLabel)
throws SQLException
Reader reader = rs.getCharacterStream(String columnLabel)
throws SQLException
Clob clob = rs.getClob(String columnLabel)
throws SQLException
ps.setAsciiStream(int parameterIndex,
InputStream x)
throws SQLException
ps.setBinaryStream(int parameterIndex,
InputStream x,
int length)
throws SQLException
ps.setBlob(int parameterIndex,
Blob x)
throws SQLException
ps.setBlob(int parameterIndex,
InputStream inputStream)
throws SQLException
InputStream is = rs.getAsciiStream(String columnLabel)
throws SQLException
InputStream ips = rs.getBinaryStream(String columnLabel)
throws SQLException
Blob blob = rs.getBlob(String columnLabel)
throws SQLException
package jdbc.dateclobblob;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import oracle.sql.BLOB;
public class JDBCDateBlobClob {
/**
* 向数据库中插入日期类型数据
* @throws SQLException
* @throws IOException
*/
public static void createDCB(int id, String name,Date date,Reader reader,InputStream ins) throws SQLException, IOException{
Connection conn = null;
PreparedStatement ps = null;
int resultNum;
String sql;
//我们在应用层一般是针对的Util包中的时期类型、而数据库中的日期类型是java.sql中的日期类型;
try{
conn = JDBCUtils.getConnection();
sql = "insert into dateclobblob values(?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.setString(2, name);
//由于db中使用的是java.sql中的Date,所以要转化。
ps.setDate(3,new java.sql.Date(date.getTime()));
//clob是大型的文本,所以这里可以用
//ps.setAsciiStream(int, x);文本中都是Ascii码中的字符 或
//ps.setCharacterStream(int,x);
ps.setCharacterStream(4, reader);
//blob是大型的二进制(小于4G)
ps.setBinaryStream(5, ins);
resultNum = ps.executeUpdate();
System.out.println("resultNum = "+resultNum);
}finally{
reader.close();
JDBCUtils.free(null, ps, conn);
}
}
public static void readDCB(int id) throws SQLException, IOException{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Date date= null;
Reader reader = null;
InputStream ips = null;
File paperFile = new File("src/otherFile/clob.txt");
File photoFile = new File("src/otherFile/photo.jpg");
Writer writer = new BufferedWriter(new FileWriter(paperFile));
char[] cbuf = new char[1024];
int num;
OutputStream ops = new BufferedOutputStream(new FileOutputStream(photoFile));
byte[] obuf = new byte[1024];
int oNum;
String sql;
//我们在应用层一般是针对的Util包中的时期类型、而数据库中的日期类型是java.sql中的日期类型;
try{
conn = JDBCUtils.getConnection();
sql = "select name, birthday, paper, photo from dateclobblob where id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
rs = ps.executeQuery();
while(rs.next()){
date = new Date(rs.getDate("birthday").getTime());
System.out.println("date is :"+date);
//处理clob;
reader = rs.getCharacterStream("paper");
//Clob clob = rs.getClob("paper");
//reader = clob.getCharacterStream();
//处理blob
ips = rs.getBinaryStream("photo");
//Blob blob = rs.getBlob("photo");
//ips = blob.getBinaryStream();
while((num = reader.read(cbuf))!=-1){
writer.write(cbuf, 0, num);
writer.flush();
}
while((oNum = ips.read(obuf))!=-1){
ops.write(obuf, 0, oNum);
ops.flush();
}
}
}finally{
reader.close();
writer.close();
ops.close();
ips.close();
JDBCUtils.free(rs, ps, conn);
}
}
public static void main(String[] args) throws SQLException, IOException {
int id =3;
String name = "lisi";
Date date = new Date();
Reader reader = new BufferedReader(new FileReader(new File("src/jdbc/two/JDBCCRUD.java")));
InputStream ins = new BufferedInputStream(new FileInputStream(new File("src/otherFile/a3.jpg")));
createDCB(id,name,date,reader,ins);
readDCB(2);
}
}