java 操作ORACLE Clob字段

来源:互联网 发布:知商金融 预警 编辑:程序博客网 时间:2024/05/25 19:59

package net.esoon.text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.sql.CLOB;

public class MyText {
 String url="jdbc:oracle:thin:@218.56.105.30:1521:myora"; //数据库联接字符串
// jdbc:oracle:thin:@固定字符
// 218.56.105.30数据库所在服务器IP地址
// :1521端口号
// :myora     ORACLE服务名

 String sa="skgl";//数据库登陆用户名

 String pwd="skgl";//用户名密码
 
 public void exeInsert(){
  String counttext="这是要插入到CLOB里面的数据";

  String sql="insert into article_add (id,content,modifydate) values ('7',?,sysdate)";//要执行的SQL语句(添加)

  try {//联接数据库的代码必须加异常捕获否则编译不通过

  /***以下为连接数据库的代码**/

  Class.forName("oracle.jdbc.driver.OracleDriver");//JAVA反射机制此格式固定
  Connection conn=java.sql.DriverManager.getConnection(url,sa,pwd);//得到连接对象

//  执行SQL有两种方式PreparedStatement 对象和Statement对象,本人使用 PreparedStatement 对象
  PreparedStatement stmt=conn.prepareStatement(sql);//加载SQL语句
//  由于PreparedStatement 支持SQL带有问号“?”可以动态替换?的内容。以下是替换?的方法
////  替换?的方法 “1”代表第一为“?”(参数的设置是从1开始的,不是0开始)

//  正常的插入方式
  Reader   clobReader   =   new   StringReader(counttext); //将 counttext转成流形式
  stmt.setCharacterStream(1,   clobReader,   counttext.length());

//  插入方法2,直接插入也可以,我试过没有错误
//  stmt.setString(1, counttext);


  int num=stmt.executeUpdate();//执行SQL

  if(num>0){
      System.out.println("ok");
     }else{
      System.out.println("NO");
     }
  }catch (Exception e) {
      //TODO Auto-generated catch block
     e.printStackTrace();
    }

 }
 public void exeQuery(){
  CLOB   clob=null;
  ResultSet rs=null;
  String sql="select * from article_add where id=7";
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   Connection conn=java.sql.DriverManager.getConnection(url,sa,pwd);
   java.sql.PreparedStatement stmt=conn.prepareStatement(sql);
   //stmt.setCharacterStream(1,   clobReader,   counttext.length()); 
   rs=stmt.executeQuery();
   String id="";
   String content="";
   String time="";
   if(rs.next()){
    id=rs.getString("id");//获得ID
    
    clob=(oracle.sql.CLOB)rs.getClob("content"); //获得CLOB字段content
    //注释: 用 rs.getString("content")无法得到 数据 ,返回的 是 NULL;
    
    /**将字CLOB转成STRING类型
     * start
     * */
//    Reader is = clob.getCharacterStream();//得到流
//    BufferedReader br = new BufferedReader(is);
//    String s = br.readLine();
//    StringBuffer sb = new StringBuffer();
//    while (s != null) {//执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
//     sb.append(s);
//     s = br.readLine();
//    }
//    content=sb.toString();
    
    content=ClobToString(clob);
    /*****
     * end
     * */
    time=rs.getString("modifydate");//获得 时间
    
   }
   //输出结果
   System.out.println(id);
   System.out.println(content);
   System.out.println(time);
   //关闭对象
   stmt.close();
   conn.close();
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public String ClobToString(CLOB clob){
  String reString="";
  try {
   Reader is = clob.getCharacterStream();//得到流
   BufferedReader br = new BufferedReader(is);
   String s = br.readLine();
   StringBuffer sb = new StringBuffer();
   while (s != null) {//执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
    sb.append(s);
    s = br.readLine();
   }
   reString=sb.toString();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return reString;
 }

 public static void main(String[] args){
  MyText myText=new MyText();
  myText.exeInsert();//插入数据
  myText.exeQuery();//查询数据
  
 }
}

原创粉丝点击