zoukankan      html  css  js  c++  java
  • 在oracle数据库中如何插入CLOB值

    轉貼:http://blog.csdn.net/cmtobby/

    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();}
     }
    }

    再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 相关阅读:
    反射
    注解
    file
    exception(异常)
    MySQL问题
    maven 中 遇到的问题
    Java读取文本数字
    人民币-欧元预测(ARIMA算法)代码
    云平台项目--学习经验--jsrender前端渲染模板
    云平台项目--学习经验--BootstrapValidate表单验证插件
  • 原文地址:https://www.cnblogs.com/skiwdhwhssh/p/10340805.html
Copyright © 2011-2022 走看看