在oracle数据库中如何插入CLOB值

        oracle中最常用的varcher2类型最多只能存储4000个字节的内容,一般情况下是能够满足用户的需求的。但是在一些特殊情况下(如要存储图片或者要存储的内容超过了4000个字节),varcher2就满足不了这个需求了。这个时候我们可以借助于oracle里面的大字段CLOB后者BLOB。举例如下:

        首先,在数据库中建一张表news,为了简单起见,只有一个字段content(CLOB)。做好准备工作后就可以开始我们的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.prepareStatement(sql1);
                                ps1.executeUpdate();
                                ps1.close();
  
                                //第二步:取出该CLOB
                                String sql2="select content from news for update";
                                PreparedStatement ps2=conn.prepareStatement(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.prepareStatement(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();}
     }}

你可能感兴趣的:(Java以及JSP技术,oracle,数据库,string,exception,import,sql)