JAVA-JDBC: (3)操作日期类型和CLOB、BLOB类型

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 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);
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值