【Oracle/clob】在Sqlplus中借助dbms_lob.substr()显示clob字段中的长文本

在SqlPlus中显示clob字段时,如果其内容稍长便会截取前面一小段显示,稍有不便;

若借助dbms_lob.substr()函数,长文本便可一览无余,颇为方便;

以下SQL对比很明显:

SQL> select * from emp0520;  -- 直接显示只出现前一小段

        ID
----------
CONTENT
--------------------------------------------------------------------------------
         1
莫虚青山多障碍,风牙嘎风牙ging;

把丸过山峰牙活群情,莫虚水中多宾碗;

水牙清水


SQL> select dbms_lob.substr(content) from emp0520 where id=1; --用了dbms_lob.substr后便一览无余

DBMS_LOB.SUBSTR(CONTENT)
--------------------------------------------------------------------------------
莫虚青山多障碍,风牙嘎风牙ging;

把丸过山峰牙活群情,莫虚水中多宾碗;

水牙清水牙静,窑情启水爱共wing;

梅怕弓风吹散流倚爱,漫水亲山总系情;

醉散牙瑶听居定,八云听八云命;

但靠瑶山水弓作京。

DBMS_LOB.SUBSTR(CONTENT)
--------------------------------------------------------------------------------

莫虚青山多障碍,风牙嘎风牙ging;

把丸过山峰牙活群情,莫虚水中多宾碗;

水牙清水牙静,窑情启水爱共wing;

歌曲:万水千山总是情
歌手:汪明荃
莫说青山多障碍
(莫虚青山多障碍)

DBMS_LOB.SUBSTR(CONTENT)
--------------------------------------------------------------------------------
风也急风也劲
(风牙嘎风牙ging)
白云过山峰也可传情
(把丸过山峰牙活群情)
莫说水中多变幻
(莫虚水中多宾碗)
水也清水也静
(水牙清水牙静)
柔情似水爱共永
(窑情启水爱共wing)


DBMS_LOB.SUBSTR(CONTENT)
--------------------------------------------------------------------------------
未怕罡风吹散了热爱
(梅怕弓风吹散流倚爱)
万水千山总是情
(漫水亲山总系情)
聚散也有天注定
(醉散牙瑶听居定)
不怨天不怨命
(八云听八云命)


SQL>

主题讲完了,下面附上给这个字段充值的程序:

package com.hy.lab.clob;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.*;

public class ClobWriter {
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
            String user = "luna";
            String pass = "1234";
            conn = DriverManager.getConnection(url, user, pass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static String readFromFile(String path) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));

        String line = null;
        StringBuilder sb=new StringBuilder();
        while( ( line = br.readLine() ) != null ) {
            sb.append(line+"\n");
        }
        br.close();

        return sb.toString();
    }

    public static void main(String[] args){
        String insertSql="insert into emp0520(id,content) values(?,?)";

        try(Connection conn =getConnection();
            PreparedStatement pstmt =conn.prepareStatement(insertSql);){
            conn.setAutoCommit(false);

            pstmt.setInt(1,1);
            Clob clob=conn.createClob();
            clob.setString(1,readFromFile("c:\\hy\\daily\\220520.txt"));
            pstmt.setClob(2,clob);

            pstmt.addBatch();

            pstmt.executeBatch();
            conn.commit();
            System.out.println("完成写入");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

END

posted @ 2022-05-20 05:38  逆火狂飙  阅读(954)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东