• Oracle数据类型Clob读取


    代码
     //---插入CLOB的代码:

             
    import java.sql.*;
             
    import java.io.*;

            
    public class TestClob{
                  
    public void TestClob(){}
                          
    public static void main(String args[]){
                                  
    try{
                                       Class.forName(
    "oracle.jdbc.driver.OracleDriver");
                                       Connectionconn
    =DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:ora32","tjpt","tjpt");
      
                                    conn.setAutoCommit(
    false);
                                   
    //第一步:插入一个空的CLOB
                                    String sql1="insert into news(content,id) values (EMPTY_CLOB(),'1')";
                                    PreparedStatement ps1
    =conn.divpareStatement(sql1);
                                     ps1.executeUpdate();
                                   ps1.close();
      
                                    
    //第二步:取出该CLOB
                                     String sql2="select content from news for update";
                                    PreparedStatement ps2
    =conn.divpareStatement(sql2);
                                    ResultSet rs2
    =ps2.executeQuery();
                                    
    while (rs2.next()){
                                           oracle.sql.CLOB clob
    =(oracle.sql.CLOB)rs2.getClob(1);
                                            BufferedWriter out
    =new BufferedWriter(clob.getCharacterOutputStream());
                                            String content
    ="1234";//假定这是新闻的内容,当然可以也可以是其他的内容
                                            out.write(content,0,content.length());
                                            out.close();
                                                                  }
                                    conn.commit();
                                 }
         
    catch(Exception e){e.printStackTrace();}
                                                                                          }

                                                                    }

    //---读取CLOB的代码:

    import java.sql.*;
    import java.io.*;

    public class ReadClob{
           
    public void ReadClob(){}
                  
    public static void main(String args[]){
                 
    try{
                     Class.forName(
    "oracle.jdbc.driver.OracleDriver");Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora32","tjpt","tjpt");                                                                                
        
       String sql1
    ="select content from news";
       PreparedStatement ps1
    =conn.divpareStatement(sql1);
    ResultSet rs1
    =ps1.executeQuery();
      
       
    while (rs1.next()){

        oracle.sql.CLOB clob
    =(oracle.sql.CLOB)rs1.getClob(1);
        BufferedReader in
    =new BufferedReader(clob.getCharacterStream());
                StringWriter out
    =new StringWriter();
                
    int c;
                
    while((c=in.read())!=-1){
                out.write(c);
                                     }
                String content
    =out.toString();
                System.out.println (content);
    //输出CLOB内容
                   }
            }
         
    catch(Exception e){e.printStackTrace();}
         }}  
  • 相关阅读:
    输入框联想
    SyntaxError: missing ; before statement 错误的解决
    Oracle数据库DECODE函数的使用.
    MySQL ----命令总结!
    个介!
    递归函数
    闭包函数与装饰器
    函数对象
    力扣题
    函数基础
  • 原文地址:https://www.cnblogs.com/ding0910/p/1663594.html
Copyright © 2020-2023  润新知