oracle存入图片和文本

转贴

java操作Oracle数据库中的Clob,Blob字段

Java代码 复制代码
  1. 说明1:首先所有的文件都是以二进制存储   
  2.        2:二进制文件有.doc .xls .jpg   
  3.          文本文件有   .txt .html .xml   
  4.   
  5. 先在oracle数据库中建一张表用与测试   
  6.   
  7. create table CDL_TEST   
  8. (   
  9.   SID  VARCHAR2(20) not null,   
  10.   IMG  BLOB,       //存储二进制   
  11.   DOC  CLOB,     //存储文本   
  12.   DATA NUMBER   
  13. )   
  14. -- 约束   
  15.   
  16. alter table CDL_TEST   
  17.   add constraint CDL_SID primary key (SID)   
  18.   
  19.   
  20. 测试代码如下:   
  21.   
  22. package DataBaseUtil;   
  23. import java.sql.*;   
  24. import java.util.Scanner;   
  25. import java.io.*;   
  26. import oracle.sql.BLOB;   
  27.   
  28. class InitDB{   
  29.  private static Connection con=null;   
  30.  private static Statement stmt=null;   
  31.  private static ResultSet rs=null;   
  32.  InitDB(){   
  33.   try{   
  34.   Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();     
  35.       String url="jdbc:oracle:thin:@localhost:1521:ORCL";  //ORCL 是sid   
  36.   String user="cdl";     
  37.   String password="1";     
  38.   con= (Connection) DriverManager.getConnection(url,user,password);   
  39.   InitDB.setCon(con);   
  40.   }catch(Exception e){   
  41.    e.printStackTrace();   
  42.   }   
  43.  }   
  44.     
  45.  public void closCon(){   
  46.   try{   
  47.    con.close();   
  48.   }catch(Exception e){   
  49.    e.printStackTrace();   
  50.   }   
  51.  }   
  52.  public void stmt(){   
  53.   try{   
  54.    con.close();   
  55.   }catch(Exception e){   
  56.    e.printStackTrace();   
  57.   }   
  58.  }   
  59.  public void rs(){   
  60.   try{   
  61.    con.close();   
  62.   }catch(Exception e){   
  63.    e.printStackTrace();   
  64.   }   
  65.  }   
  66.   
  67.  public static Connection getCon() {   
  68.   return con;   
  69.  }   
  70.   
  71.  public static void setCon(Connection con) {   
  72.   InitDB.con = con;   
  73.  }   
  74.   
  75.  public static ResultSet getRs() {   
  76.   return rs;   
  77.  }   
  78.   
  79.  public static void setRs(ResultSet rs) {   
  80.   InitDB.rs = rs;   
  81.  }   
  82.   
  83.  public static Statement getStmt() {   
  84.   return stmt;   
  85.  }   
  86.   
  87.  public static void setStmt(Statement stmt) {   
  88.   InitDB.stmt = stmt;   
  89.  }   
  90. }   
  91.   
  92. /*  
  93.  * 插入Blob数据 如:图片  
  94.  */  
  95. class InsertBlobData{   
  96.  private ResultSet rs=null;   
  97.  private InitDB idb=null;   
  98.  InsertBlobData(){   
  99.    idb=new InitDB();   
  100.  }   
  101.  public  void insertBlob(String sql1) throws SQLException{   
  102.   Connection con=idb.getCon();   
  103.   try{   
  104.    con.setAutoCommit(false);//不设置自动提交   
  105.    BLOB blob = null//插入空的Blob   
  106.    PreparedStatement pstmt = con.prepareStatement("insert into cdl_test(sid,img) values(?,empty_blob())");   
  107.    pstmt.setString(1,"100");    
  108.    pstmt.executeUpdate();    
  109.    pstmt.close();    
  110.    rs=con.createStatement().executeQuery(sql1);   
  111.    while(rs.next()){   
  112.      System.out.println("rs length is:");   
  113.      oracle.sql.BLOB  b=(oracle.sql.BLOB )rs.getBlob("img");   
  114.      System.out.println("cloblength is:"+b.getLength());   
  115.      File f=new File("d:\\img\\1.jpg");   
  116.      System.out.println("file path is:"+f.getAbsolutePath());   
  117.      BufferedInputStream in=new BufferedInputStream(new FileInputStream(f));     
  118.      BufferedOutputStream  out=new BufferedOutputStream(b.getBinaryOutputStream());    
  119.      int   c;      
  120.      while ((c=in.read())!=-1)   {      
  121.       out.write(c);      
  122.      }      
  123.      in.close();      
  124.      out.close();   
  125.    }   
  126.    con.commit();   
  127.   }catch(Exception e){   
  128.    con.rollback();//出错回滚   
  129.    e.printStackTrace();   
  130.   }    
  131.  }   
  132. }   
  133. /*  
  134.  * 插入大文本如:1.txt  
  135.  */  
  136. class InsertClobData{   
  137.  private ResultSet rs=null;   
  138.  private InitDB idb=null;   
  139.  InsertClobData(){   
  140.    idb=new InitDB();   
  141.  }   
  142.  public  void insertClob(String sql1) throws SQLException{   
  143.   Connection con=idb.getCon();   
  144.   try{   
  145.    con.setAutoCommit(false);//不设置自动提交   
  146.    BLOB blob = null//插入空的Clob   
  147.    PreparedStatement pstmt = con.prepareStatement("insert into cdl_test(sid,doc) values(?,empty_clob())");   
  148.    pstmt.setString(1,"101");    
  149.    pstmt.executeUpdate();    
  150.    pstmt.close();    
  151.    rs=con.createStatement().executeQuery(sql1);   
  152.    while(rs.next()){   
  153.     System.out.println("sdfasdfas");   
  154.     oracle.sql.CLOB  cb=(oracle.sql.CLOB)rs.getClob("doc");   
  155.     File f=new File("d:\\doc\\1.txt");   
  156.     System.out.println("file path is:"+f.getAbsolutePath());   
  157.     BufferedWriter out = new BufferedWriter(cb.getCharacterOutputStream());   
  158.     BufferedReader in = new BufferedReader(new FileReader(f));   
  159.     int c;   
  160.     while ((c=in.read())!=-1) {   
  161.         out.write(c);   
  162.     }   
  163.     in.close();      
  164.     out.close();   
  165.    }   
  166.    con.commit();   
  167.   }catch(Exception e){   
  168.    con.rollback();//出错回滚   
  169.    e.printStackTrace();   
  170.   }    
  171.  }   
  172. }   
  173. /*  
  174.  * 读取图片  
  175.  */  
  176. class ReadBlobData{   
  177.  private ResultSet rs=null;   
  178.  private InitDB idb=null;   
  179.  ReadBlobData(){   
  180.    idb=new InitDB();   
  181.  }   
  182.  public void getBlob(String sql2) throws SQLException{   
  183.   Connection con=idb.getCon();   
  184.   con.setAutoCommit(false);   
  185.   try{   
  186.    System.out.println("sq2 is:"+sql2);   
  187.    System.out.println("stmt is:"+con);   
  188.    rs=con.createStatement().executeQuery(sql2);   
  189.    while(rs.next()){   
  190.     System.out.println("rs length is:");   
  191.     Blob b=(Blob)rs.getBlob("img");   
  192.     File f=new File("D:\\saveimg\\1.jpg");   
  193.     FileOutputStream fos=new FileOutputStream(f);   
  194.     InputStream is=b.getBinaryStream();//读出数据后转换为二进制流   
  195.     byte[] data=new byte[1024];   
  196.     while(is.read(data)!=-1){   
  197.      fos.write(data);   
  198.     }   
  199.     fos.close();   
  200.     is.close();   
  201.    }   
  202.   con.commit();//正式提交   
  203.   }catch(Exception e){   
  204.    e.printStackTrace();   
  205.   }finally{   
  206.    //rs.close();   
  207.   }   
  208.  }   
  209. }   
  210. /*  
  211.  * 读取大文本  
  212.  */  
  213. class ReadClobData{   
  214.  private ResultSet rs=null;   
  215.  private InitDB idb=null;   
  216.  ReadClobData(){   
  217.    idb=new InitDB();   
  218.  }   
  219.  public void getClob(String sql2) throws SQLException{   
  220.   Connection con=idb.getCon();   
  221.   try{   
  222.    con.setAutoCommit(false);//不设置自动提交   
  223.    System.out.println("sq2 is:"+sql2);   
  224.    rs=con.createStatement().executeQuery(sql2);   
  225.    while(rs.next()){   
  226.     oracle.sql.CLOB clob=(oracle.sql.CLOB)rs.getClob("doc");   
  227.     File f=new File("d:\\savedoc\\1.txt");   
  228.     BufferedReader in = new BufferedReader(clob.getCharacterStream());   
  229.     //setCharacterStream()方法,可用于将CLOB字段与字节流相关联,   
  230.              BufferedWriter out = new BufferedWriter(new FileWriter(f));   
  231.              int c;   
  232.              while ((c=in.read())!=-1) {   
  233.                   out.write(c);   
  234.               }   
  235.     out.close();   
  236.     in.close();   
  237.    }   
  238.   con.commit();//正式提交   
  239.   rs.close();   
  240.   }catch(Exception e){   
  241.    e.printStackTrace();   
  242.    con.rollback();   
  243.   }   
  244.  }   
  245. }   
  246. public class TestBlob {   
  247.  public static void main(String []args){   
  248.   String sql1="select * from cdl_test  for update";//悲观锁锁定需更新的行   
  249.   String sql2="select * from cdl_test";   
  250.   System.out.println("\t\t\t欢迎使用:");   
  251.   System.out.println("1:插入图片");   
  252.   System.out.println("2:插入文本");   
  253.   System.out.println("3:读取图片");   
  254.   System.out.println("4:读取文本");   
  255.   System.out.println("5:退出");   
  256.   System.out.println("请选择:")   
  257.   while(true){   
  258.    try{   
  259.     Scanner sc=new Scanner(System.in);   
  260.     int i=sc.nextInt();   
  261.     System.out.println("sss:"+i);   
  262.     switch(i){   
  263.      case 1:   
  264.       InsertBlobData isd=new InsertBlobData();   
  265.       isd.insertBlob(sql1); break;   
  266.      case 2:   
  267.       InsertClobData icd=new InsertClobData();   
  268.       icd.insertClob(sql1); break;   
  269.      case 3:   
  270.       ReadBlobData rb=new ReadBlobData();   
  271.       rb.getBlob(sql2); break;   
  272.      case 4:   
  273.       ReadClobData rc=new ReadClobData();   
  274.       rc.getClob(sql2); break;   
  275.      case 5:   
  276.       System.exit(0);   
  277.     }   
  278.    }catch(Exception e){   
  279.     e.printStackTrace();   
  280.    }   
  281.   }   
  282.  }   
  283. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值