设为首页 收藏本站
查看: 871|回复: 0

[经验分享] 关于 oracle blob 写入读出修改

[复制链接]

尚未签到

发表于 2018-9-26 06:28:55 | 显示全部楼层 |阅读模式
  最近因为自己个人的一个需求,就是把文件存入数据库中,以方便备份,所以就写了这个类,这个类也不完全原创,有参考网上的一些资料,但通过自己的测试,完全可以实现自己的需求,所以把代码贴出来分享一下!
  以下的一些路径、数据库表名、字段名 需要自己修改一下,也可以根据自己的需要改成配置的!这个看自己的啦!还有就是那个jdbc工具类自己改一下!
  1:WriteAndReadFile 类
  


  • package com.grg.johnny.work;

  • import java.io.*;
  • import java.sql.*;

  • import oracle.sql.BLOB;

  • public class WriteAndReadFile {

  •     public static void main(String[] args){
  •         //1:先写入
  •         String path = "D://oracle2.zip";
  •         saveFile(path);
  •         //2:再读出
  •         //getFile("1");

  •     }

  •     /**
  •      * 写入文件
  •      * 往blob里插入文件要先插入空值empty_blob(),再进行修改
  •      * @param filePath
  •      * @return
  •      */
  •     public static boolean saveFile(String filePath) {
  •         File file = new File(filePath);
  •         Connection conn = JdbcUtil.getConnection();
  •         try {
  •             java.sql.Statement st = conn.createStatement();
  •             conn.setAutoCommit(false);
  •             System.out.println("=====================save file begin========================");
  • //          st.execute("insert into johnny_file values(1,'日常生活',empty_blob(),'这是一个很强大的文件',to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'))");
  •             st.execute("insert into johnny_file values(2,'日常生活2',empty_blob(),'这是一个很强大的文件test',to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'))");
  •             ResultSet rs = st
  •                     .executeQuery("select id,file_blob from johnny_file where id=2 for update");
  •             if (rs.next()) {
  •                 BLOB blob = (BLOB) rs.getBlob("file_blob");
  •                 OutputStream outStream = blob.getBinaryOutputStream();
  •                 InputStream fin = new FileInputStream(file);
  •                 byte[] b = new byte[blob.getBufferSize()];
  •                 int len = 0;
  •                 while ((len = fin.read(b)) != -1) {
  •                     outStream.write(b, 0, len);
  •                 }
  •                 fin.close();
  •                 outStream.flush();
  •                 outStream.close();
  •                 conn.commit();
  •                 conn.close();
  •             }
  •         } catch (SQLException e) {
  •             // TODO Auto-generated catch block
  •             e.printStackTrace();
  •             return false;
  •         } catch (FileNotFoundException e) {
  •             // TODO Auto-generated catch block
  •             e.printStackTrace();
  •             return false;
  •         } catch (IOException e) {
  •             // TODO Auto-generated catch block
  •             e.printStackTrace();
  •             return false;
  •         }
  •         System.out.println("=====================save file end========================");
  •         return true;
  •     }

  •     /**
  •      * 读取
  •      * 读取出的路径根据自己的需要修改
  •      * @param id
  •      */
  •     public static void getFile(String id) {
  •         Connection conn = JdbcUtil.getConnection();
  •         java.sql.Statement st;
  •         try {
  •             st = conn.createStatement();
  •             System.out.println("=====================get file begin========================");
  •             ResultSet rs = st
  •                     .executeQuery("select id,file_blob from johnny_file where id='"
  •                             + id + "'");
  •             if (rs.next()) {
  •                 BLOB blob = (BLOB) rs.getBlob("file_blob");
  •                 File file = new File("D://oracle.zip");
  •                 FileOutputStream output = new FileOutputStream(file);
  •                 InputStream input = blob.getBinaryStream();
  •                 byte[] buffer = new byte[1024];
  •                 int i = 0;
  •                 while ((i = input.read(buffer)) != -1) {
  •                     output.write(buffer, 0, i);
  •                 }
  •             }
  •             System.out.println("=====================get file end========================");
  •         } catch (Exception e) {
  •             // TODO Auto-generated catch block
  •             e.printStackTrace();
  •         }
  •     }

  •     /**
  •      * 修改blob内容
  •      */
  •     public static void updateblob(String id) {
  •         Connection conn = JdbcUtil.getConnection();
  •         Statement stem = null;
  •         ResultSet rs = null;
  •         try {
  •             conn.setAutoCommit(false);
  •             stem = conn.createStatement();
  •             System.out.println("=====================update file begin========================");
  •             rs = stem.executeQuery("select file_blob from johnny_file where id='"
  •                     + id + "' for update");

  •             if (rs.next()) {
  •                 BLOB blob = (BLOB) rs.getBlob("file_blob");
  •                 OutputStream outStream = blob.getBinaryOutputStream();
  •                 InputStream fin = new FileInputStream("D://ok.zip");
  •                 byte[] b = new byte[blob.getBufferSize()];
  •                 int len = 0;
  •                 while ((len = fin.read(b)) != -1) {
  •                     outStream.write(b, 0, len);
  •                 }
  •                 fin.close();
  •                 outStream.flush();
  •                 outStream.close();
  •                 conn.commit();
  •                 conn.close();
  •             }
  •             System.out.println("=====================update file end========================");
  •         } catch (Exception ex) {
  •             try {
  •                 conn.rollback();
  •             } catch (SQLException e) {
  •                 // TODO Auto-generated catch block
  •                 e.printStackTrace();
  •             }
  •             System.out.println(ex.getMessage());
  •         }
  •     }
  • }
  

  2:以下这个是我自己的jdbc工具类
  


  • package com.grg.johnny.work;

  • import java.sql.*;

  • public class JdbcUtil {
  •     /**
  •      * driverName
  •      * */
  •     static{
  •         String driverName="oracle.jdbc.driver.OracleDriver";
  •         try{
  •             Class.forName(driverName);
  •         }catch(Exception e){
  •             e.printStackTrace();
  •         }
  •     }

  •     /**
  •      * getConnection
  •      * */
  •     public static Connection getConnection(){
  •         String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
  •         String userName="XXXXX";
  •         String pwd="XXXXXX";
  •         Connection con = null;
  •         try{
  •             con = DriverManager.getConnection(url,userName,pwd);
  •         }catch(Exception ee){
  •             ee.printStackTrace();
  •         }
  •         return con;
  •     }

  •     /**
  •      * close
  •      * */
  •     public static void close(ResultSet rs,Statement stmt,Connection con){
  •         try{
  •             if(rs!=null){rs.close();}
  •         }catch(Exception ee){
  •             ee.printStackTrace();
  •         }
  •         try{
  •             if(stmt!=null){stmt.close();}
  •         }catch(Exception ee){
  •             ee.printStackTrace();
  •         }
  •         try{
  •             if(con!=null){con.close();}
  •         }catch(Exception ee){
  •             ee.printStackTrace();
  •         }
  •     }

  • }
  




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-602072-1-1.html 上篇帖子: ORACLE经典查询语句 下篇帖子: Oracle自动备份(Windows平台)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表