JAVA-JDBC: (3)操作日期类型和CLOB、BLOB类型
2017-05-30 18:38
519 查看
1. JDBC操作oracle日期类型:
1.1 oracle数据库日期格式中常见的两个函数:
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的:
y 表示年的最后一位
yy 表示年的最后2位
yyy 表示年的最后3位
yyyy 用4位数表示年
表示month的:
mm 用2位数字表示月;
mon 用简写形式 比如11月或者nov ;
month 用全称 比如11月或者november
表示day的:
dd 表示当月第几天;
ddd表示当年第几天;
dy 当周第几天 简写 比如星期五或者fri;
day当周第几天 全写 比如星期五或者friday。
表示hour的:
hh 2位数表示小时 12进制;
hh24 2位数表示小时 24小时
表示minute的:
mi 2位数表示分钟
表示second的:
ss 2位数表示秒 60进制
表示季度的:
q 一位数 表示季度 (1-4)
另外还有ww 用来表示当年第几周 w用来表示当月第几周。
24小时制下的时间范围:00:00:00-23:59:59
12小时制下的时间范围:1:00:00-12:59:59
1.2 注意
我们在应用层用到的日期类型是 java.util包中,而在jdbc中的数据类型是java.sql包中,所以我们在处理时要做相应的转换。2. JDBC操作 大型文本 Clob 和大型字节 Blob
2.1.1 向数据库中 添加Clob数据
ps.setCharacterStream(int parameterIndex, Reader reader) throws SQLException
ps.setClob(int parameterIndex, Clob x) throws SQLException
ps.setClob(int parameterIndex, Reader reader) throws SQLException
2.1.2 从数据库中读取 Clob数据
InputStream is = rs.getAsciiStream(String columnLabel) throws SQLException
Reader reader = rs.getCharacterStream(String columnLabel) throws SQLException
Clob clob = rs.getClob(String columnLabel) throws SQLException
2.2.1 向数据库中添加 Blob数据
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
2.1.2 从数据库中读取 Clob数据
InputStream is = rs.getAsciiStream(String columnLabel) throws SQLException
InputStream ips = rs.getBinaryStream(String columnLabel) throws SQLException
Blob blob = rs.getBlob(String columnLabel) throws SQLException
3. 简单示例代码:
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 i 4000 nto 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); } }
相关文章推荐
- JAVA对Oracle中BLOB、CLOB类型字段的操作说明
- jdbc hibernate ibatis 操作Blob 和Clob类型字段(不断更新)
- jdbc实现对CLOB和BLOB数据类型操作
- Spring JDBC-使用Spring JDBC获取本地连接对象以及操作BLOB/CLOB类型数据
- 从 Oracle 大字段(blob,clob)的读、写认识 Java JDBC操作全攻略
- (转)通过JDBC 操作 ORACLE BLOB,CLOB字段类型
- JDBC 对oracle 的clob ,blob类型的操作方式
- JDBC 对oracle 的clob ,blob类型的操作方式
- Hibernate操作Blob类型字段出现java.lang.AbstractMethodError: oracle.jdbc.driver.OracleResultSetImpl.getBlob(Ljava/lang/String;)Ljava/sql/
- 通过JDBC 操作 ORACLE BLOB,CLOB字段类型
- 利用JDBC操作Oracle CLOB和BLOB类型数据
- 使用java JDBC方式操作oracle blob类型数据
- java操作 oracle 的 blob和clob类型
- 使用java JDBC方式操作oracle blob类型数据
- oracle对大对象类型操作:blob,clob,nclob,bfile
- JDBC操作DB2 Clob、Blob字段的Bug探究
- oracle对大对象类型操作:blob,clob,nclob
- oracle的CLOB和BLOB (jdbc实现对其操作示例代码)
- jdbc oracle clob blob long类型数据
- Java - oracle 日期类型字段的操作