标签:des style blog io color ar os 使用 sp
在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了。因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这种类型的字段,很灵活,适用于数据 量非常大的业务领域(如图象、档案等)。
LOB类型分为BLOB和CLOB两种:BLOB即二进制大型对象(Binary Large Object),适用于存贮非文本的字节流数据(如程序、图象、影音等)。而CLOB,即字符型大型对象(Character Large Object),则与字符集相关,适于存贮文本型的数据(如历史档案、大部头著作等)。
建表SQL语句为:
1 CREATE TABLE TEST_CLOB ( ID NUMBER(3), CLOBCOL CLOB)
2
3 CREATE TABLE TEST_BLOB ( ID NUMBER(3), BLOBCOL BLOB)
1 oracleDb_Driver=oracle.jdbc.driver.OracleDriver
2 oracleDb_Url=jdbc:oracle:thin:@localhost:1521:GACL
3 oracleDb_UserName=GACL_XDP
4 oracleDb_Password=P
1 package me.gacl.utils;
2
3 import java.io.InputStream;
4 import java.sql.Connection;
5 import java.sql.DriverManager;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.Properties;
10
11 public class JdbcUtils {
12
13 private static String oracleDb_Driver = null;
14 private static String oracleDb_Url = null;
15 private static String oracleDb_UserName = null;
16 private static String oracleDb_Password = null;
17
18 static{
19 try{
20 //读取db.properties文件中的数据库连接信息
21 InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
22 Properties prop = new Properties();
23 prop.load(in);
24
25 //获取数据库连接驱动
26 oracleDb_Driver = prop.getProperty("oracleDb_Driver");
27 //获取数据库连接URL地址
28 oracleDb_Url = prop.getProperty("oracleDb_Url");
29 //获取数据库连接用户名
30 oracleDb_UserName = prop.getProperty("oracleDb_UserName");
31 //获取数据库连接密码
32 oracleDb_Password = prop.getProperty("oracleDb_Password");
33
34 //加载数据库驱动
35 Class.forName(oracleDb_Driver);
36
37 }catch (Exception e) {
38 throw new ExceptionInInitializerError(e);
39 }
40 }
41
42 /**
43 * @Method: getOracleConnection
44 * @Description: 获取Oracle数据库连接对象
45 * @Anthor:孤傲苍狼
46 *
47 * @return Connection数据库连接对象
48 * @throws SQLException
49 */
50 public static Connection getOracleConnection() throws SQLException{
51 return DriverManager.getConnection(oracleDb_Url, oracleDb_UserName,oracleDb_Password);
52 }
53
54 /**
55 * @Method: release
56 * @Description: 释放资源,
57 * 要释放的资源包括Connection数据库连接对象,负责执行SQL命令的Statement对象,存储查询结果的ResultSet对象
58 * @Anthor:孤傲苍狼
59 *
60 * @param conn
61 * @param st
62 * @param rs
63 */
64 public static void release(Connection conn,Statement st,ResultSet rs){
65 if(rs!=null){
66 try{
67 //关闭存储查询结果的ResultSet对象
68 rs.close();
69 }catch (Exception e) {
70 e.printStackTrace();
71 }
72 rs = null;
73 }
74 if(st!=null){
75 try{
76 //关闭负责执行SQL命令的Statement对象
77 st.close();
78 }catch (Exception e) {
79 e.printStackTrace();
80 }
81 }
82
83 if(conn!=null){
84 try{
85 //关闭Connection数据库连接对象
86 conn.close();
87 }catch (Exception e) {
88 e.printStackTrace();
89 }
90 }
91 }
92 }
1 package me.gacl.demo;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.sql.Connection;
8 import java.sql.PreparedStatement;
9 import java.sql.ResultSet;
10 import org.junit.Test;
11 import me.gacl.utils.JdbcUtils;
12
13 /**
14 * @ClassName: JdbcOperaOracleClob
15 * @Description:Oracle中字符型大型对象(Character Large Object)数据处理
16 * @author: 孤傲苍狼
17 * @date: 2014-10-7 下午3:53:19
18 *
19 */
20 public class JdbcOperaOracleClob {
21
22 /**
23 CREATE TABLE TEST_CLOB ( ID NUMBER(3), CLOBCOL CLOB)
24 */
25 /**
26 * @Method: clobInsert
27 * @Description:往数据库中插入一个新的CLOB对象
28 * @Anthor:孤傲苍狼
29 *
30 * @throws Exception
31 */
32 @Test
33 public void clobInsert() throws Exception {
34 Connection conn = null;
35 PreparedStatement stmt = null;
36 ResultSet rs = null;
37 conn = JdbcUtils.getOracleConnection();
38 boolean defaultCommit = conn.getAutoCommit();
39 /*开启事务,设定不自动提交 */
40 conn.setAutoCommit(false);
41 try {
42 /* 插入一个空的CLOB对象 */
43 String sql = "INSERT INTO TEST_CLOB VALUES (?, EMPTY_CLOB())";
44 stmt = conn.prepareStatement(sql);
45 stmt.setInt(1, 1);
46 stmt.executeUpdate();
47 /* 查询此CLOB对象并锁定 */
48 sql = "SELECT CLOBCOL FROM TEST_CLOB WHERE ID=? FOR UPDATE";
49 stmt = conn.prepareStatement(sql);
50 stmt.setInt(1, 1);
51 rs = stmt.executeQuery();
52 if (rs.next()) {
53 /* 取出此CLOB对象 */
54 oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("CLOBCOL");
55 /* 向CLOB对象中写入数据 */
56 BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
57 //这种方式获取的路径,其中的空格会被使用“%20”代替
58 String path = JdbcOperaClob.class.getClassLoader().getResource("data.txt").getPath();
59 //将“%20”替换回空格
60 path = path.replaceAll("%20", " ");
61 BufferedReader in = new BufferedReader(new FileReader(path));
62 int c;
63 while ((c = in.read()) != -1) {
64 out.write(c);
65 }
66 in.close();
67 out.close();
68 }
69 /* 正式提交 */
70 conn.commit();
71 System.out.println("插入成功");
72 } catch (Exception ex) {
73 /* 出错回滚 */
74 conn.rollback();
75 throw ex;
76 }finally{
77 /* 恢复原提交状态 */
78 conn.setAutoCommit(defaultCommit);
79 JdbcUtils.release(conn,stmt,rs);
80 }
81
82 }
83
84 /**
85 * @Method: clobRead
86 * @Description: CLOB对象读取
87 * @Anthor:孤傲苍狼
88 *
89 * @throws Exception
90 */
91 @Test
92 public void clobRead() throws Exception {
93 Connection conn = null;
94 PreparedStatement stmt = null;
95 ResultSet rs = null;
96 conn = JdbcUtils.getOracleConnection();