日期:2014-05-16  浏览次数:20493 次

java操作oracle里面的clob
oracle里面存放打字段的文本信息,一般采用clob字段,该字段以字符存放在数据库中

下面是如何用java读取clob里面的内容:

1.package com.data;   
2.import java.io.BufferedReader;   
3.import java.io.Reader;   
4.import java.sql.Clob;   
5.import java.sql.Connection;   
6.import java.sql.DriverManager;   
7.import java.sql.ResultSet;   
8.import java.sql.Statement;   
9.public class ClobTest {   
10.    /**  
11.     * beckham 2009-12-7 下午09:47:36  
12.     */  
13.    public static void main(String[] args) throws Exception {   
14.          Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();     
15.          String url="jdbc:oracle:thin:@localhost:1521:menhu";     
16.          String user="zm";     
17.          String password="zm";     
18.        Connection conn= DriverManager.getConnection(url,user,password);   
19.        Statement sta = null;   
20.        ResultSet rs = null;   
21.        String sql = "select * from Test";   
22.        sta = conn.createStatement();   
23.        rs = sta.executeQuery(sql);   
24.          String s ="";   
25.          StringBuffer content = new StringBuffer();   
26.        while(rs.next()){   
27.            //获取clob对象   
28.            Clob clob = rs.getClob("cdata") ;   
29.            //获取字符流   
30.            Reader reader = clob.getCharacterStream() ;   
31.            BufferedReader br = new BufferedReader(reader) ;   
32.            //读取缓冲流里面的字符数据    
33.            while ((s=br.readLine()) != null )   
34.                {   
35.            //每次读取一行,在末尾加上换行符   
36.                content.append(s).append("\n") ;   
37.                }   
38.            br.close() ;   
39.        }   
40.          System.out.println(content);   
41.    }   
42.}