jdbc 操作 Oracle 中的 BLOB 字段

java 代码
 
  1. package test;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.PrintStream;  
  6. import java.sql.Connection;  
  7. import java.sql.DriverManager;  
  8. import java.sql.ResultSet;  
  9. import java.sql.SQLException;  
  10. import java.sql.Statement;  
  11.   
  12. public class Test {  
  13.     private Connection conn;  
  14.   
  15.     /** 
  16.      * 得到一个数据库的连接 
  17.      * 
  18.      * @return 返加Connection对象 
  19.      */  
  20.     public Connection getConnection() {  
  21.         try {  
  22.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  23.             conn = DriverManager.getConnection(  
  24.                     "jdbc:oracle:thin:@localhost:1521:lyx""scott""tiger");  
  25.         } catch (ClassNotFoundException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         } catch (SQLException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.         return conn;  
  33.     }  
  34.   
  35.     /** 
  36.      * 向表中插入图片 
  37.      * 
  38.      * @param path图片所在的路径 
  39.      * @return 整形 判断成功或失败 
  40.      */  
  41.     public int insertImage(String path) throws Exception {  
  42.         int i = 0;  
  43.         Statement st = null;  
  44.         ResultSet rs = null;  
  45.         conn=this.getConnection();  
  46.           
  47.         conn.setAutoCommit(false);//设置数据库为不自动提交,必须的一步  
  48.         st = conn.createStatement();  
  49.         //先插入一个空对象,这里我调用了Empty_BLOB()函数  
  50.         i = st  
  51.                 .executeUpdate("insert into image (id,image) values (seq1.nextval,Empty_BLOB())");  
  52.         //以行的方式锁定  
  53.         rs = st  
  54.                 .executeQuery("select image from image where id=(select max(id) from image) for update");  
  55.         if (rs.next()) {  
  56.             //得到流  
  57.             oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);  
  58.             //从得到的低级流构造一个高级流  
  59.             PrintStream ps = new PrintStream(blob.getBinaryOutputStream());  
  60.             BufferedInputStream bis = new BufferedInputStream(  
  61.                     new FileInputStream(path));  
  62.             byte[] buff = new byte[1024];  
  63.             int n = 0;  
  64.             //从输入到输出  
  65.             while ((n = bis.read(buff)) != -1) {  
  66.                 ps.write(buff, 0, n);  
  67.   
  68.             }  
  69.             //清空流的缓存  
  70.             ps.flush();  
  71.             //关闭流,注意一定要关  
  72.             ps.close();  
  73.             bis.close();  
  74.         }  
  75.         rs.close();  
  76.         st.close();  
  77.         conn.close();  
  78.         return i;  
  79.     }  
  80.   
  81.     public static void main(String[] args) throws Exception {  
  82.         Test test=new Test();  
  83.         test.insertImage("e:\\3.jpg");  
  84.         System.out.println("OK");  
  85.     }  
  86. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值