2018.7.23 oracle中的CLOB数据类型

Oarcle中的LOB类型

1.在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了。因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这种类型的字段,很灵活,适用于数据量非常大的业务领域(如图象、档案等)。


2.LOB类型分为BLOB和CLOB两种:BLOB即二进制大型对象(Binary Large Object),适用于存贮非文本的字节流数据(如程序、图象、影音等)。


3.而CLOB,即字符型大型对象(Character Large Object),则与字符集相关,适于存贮文本型的数据(如历史档案、大部头著作等)。

DB中使用CLOB类型字段

(一)、创建表(使用sql或者直接在PL/SQL客户端创建),字段类型CLOB

create table TEMP
(
  name      VARCHAR2(200),
  age       NUMBER,
  temp_clob CLOB
)
tablespace INSIGHTDATA3_TS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 160K
    next 1M
    minextents 1
    maxextents unlimited
  );

package com.lanqiao.shopping.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.lanqiao.shopping.utils.DBHelper;

public class Test {
	static PreparedStatement pstmt = null;
	static ResultSet rs = null;
	/*
	 * 创建图片的表myPicture
	 * 	   create  table myPicture(id number(4) primary key,img blob);
	 */
	
	//将图片写进数据库
	public static void writeImgToBolb(){
		Connection conn = DBHelper.getConn();
		String sql = "insert into myPicture(id,img) values(?,?)";
		
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1,3);//id
			
			//将图片转为输入流
			File file = new File("D:\Eclipse\ImagesSave\one.jpg");
			InputStream in = new FileInputStream(file);
			
			/*
			String a = "sss";
			System.out.println(a.length());//字符串是length方法
			int[] i = new int[5];//数组是length属性
			System.out.println(i.length);*/
			
			
			//将输入流写入到myPicture表
			pstmt.setBinaryStream(2, in,(int)file.length());
			
			int result = pstmt.executeUpdate();
			System.out.println("111");
			if(result>0){
				System.out.println("图片写入成功");
			}else{
				System.out.println("图片写入失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBHelper.closeConn(null, pstmt, conn);
		}
		
	}
	
	//从数据库读取图片
	public static void readImgToBlob(){
		Connection conn = DBHelper.getConn();
		String sql = "select * from myPicture where id=?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, 1);//id =1
			rs = pstmt.executeQuery();
			if(rs.next()){
				//将图片从数据库中读取出来,类型为InputStream
				InputStream imgIn = rs.getBinaryStream("img");
				
				//通过IO流,将图片写到项目中(硬盘)
				InputStream in = new BufferedInputStream(imgIn);
				
				//将图片的输出路径设置为src(相对路径),图片名为myPic.png
				OutputStream imgOut = new FileOutputStream("D:\Eclipse\ImagesSave\myPic.jpg");
				OutputStream out = new BufferedOutputStream(imgOut);
				
				int len = -1;
				while((len=in.read())!=-1){
					out.write(len);
				}
				
				imgOut.close();
				out.close();
				imgIn.close();
				in.close();
				System.out.println("照片读取成功");
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBHelper.closeConn(rs, pstmt, conn);
		}
		
	}
	
	public static void main(String[] args) {
		//writeImgToBolb();
		readImgToBlob();
	}
}

原文地址:https://www.cnblogs.com/qichunlin/p/9349808.html