做DB Migration 将BLOB格式的数据从DB2转移到Oracle中

最近在做DB Migration 主要是做的是把DB2中的数据转移到Oracle中,系统很大,有很多类型的数据。图片格式的数据要通过写JAVA Program migrate。

连接数据库用的是JDBC这些代码就不贴了。

 表是Student 这个是数据库中的那个表

Record是表的主键,IMAGES是图片数据类型是BLOB。

在不同的数据库中才需要这样通过输入输出流操作BLOB格式的数据,在相同的数据库中就不需要了。

  1  1 package com.ncs.tools;
  2   2 
  3   3 import java.io.InputStream;
  4   4 import java.io.OutputStream;
  5   5 import java.sql.Connection;
  6   6 import java.sql.PreparedStatement;
  7   7 import java.sql.ResultSet;
  8   8 
  9   9 import com.ncs.pojo.Student;
 10  10 
 11  11 public class IDB_ARCHIVE_LOG_Tools {
 12  12 
 13  13     public void insert() {
 14  14         Connection conn = null;
 15  15         PreparedStatement ps = null;
 16  16         ResultSet rs = null;
 17  17         byte[] data = null;
 18  18         try {
 19  19             conn = JDBCTools.getDB2();
 20  20             //先将要迁移的数据从DB2中查询出来
 21  21             String sql = "select RECORD_ID, IMAGES from Student";
 22  22             ps = conn.prepareStatement(sql);
 23  23             rs = ps.executeQuery();
 24  24             while (rs.next()) {
 25  25                 System.out.println("ARC_SN:"+rs.getString(10));
 26  26                 Student student = new Student();
 27  27                 if (rs.getBlob("IMAGES") != null) {
 28  28                     java.sql.Blob blob = rs.getBlob("IMAGES");
 29  29                     InputStream inStream = blob.getBinaryStream();
 30  30                     // data是读出并需要返回的数据,类型是byte[]
 31  31                     data = new byte[(int) blob.length()];
 32  32                     inStream.read(data);
 33  33                     inStream.close();
 34  34                 }
 35  35                 student.setRECORD_ID(rs.getString(1));
 36  36                 student.setIMAGES(rs.getBlob(2));
 37  37                 if (rs.getBlob("IMAGES") == null) {
 38  38                     //如果查询的到的数据中没有图片,则直接将数据插入到Oracle中
 39  39                     this.update(student);
 40  40                 } else {
 41  41                     //如果有图片的话,执行下面的方法
 42  42                     this.update(data, student);
 43  43                 }
 44  44             }
 45  45         } catch (Exception e) {
 46  46             e.printStackTrace();
 47  47             System.out.println("false");
 48  48 
 49  49         } finally {
 50  50             JDBCTools.release(rs, ps, conn);
 51  51         }
 52  52         System.out.println("end");
 53  53     }
 54  54     //一条数据中有图片时执行的方法
 55  55     public void update(byte[] data, Student student) {
 56  56         Connection conn = null;
 57  57         PreparedStatement ps = null;
 58  58         ResultSet rs = null;
 59  59         try {
 60  60             conn = JDBCTools.getORACEL();
 61  61             JDBCTools.begin(conn);
 62  62             if(conn!=null){
 63  63                 String sql = "insert into Student(RECORD_ID,IMAGES)values(?,empty_blob())";
 64  64                 ps = conn.prepareStatement(sql);
 65  65                 ps.setString(1, student.getRECORD_ID());
 66  66                 ps.executeUpdate();
 67  67                 String sqlString = "select IMAGES from Student where RECORD_ID='"
 68  68                         + student.getRecordId() + "' for update";
 69  69                 rs = conn.prepareStatement(sqlString).executeQuery();
 70  70                 if (rs.next()) {
 71  71                     oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("IMAGES");
 72  72                     @SuppressWarnings("deprecation")
 73  73                     OutputStream outStream = blob.getBinaryOutputStream();
 74  74                     // data是传入的byte数组,定义:byte[] data
 75  75                     outStream.write(data, 0, data.length);
 76  76                     outStream.flush();
 77  77                     outStream.close();
 78  78                 }
 79  79                 JDBCTools.commit(conn);
 80  80                 
 81  81             }
 82  82         } catch (Exception e) {
 83  83             e.printStackTrace();
 84  84         } finally {
 85  85             JDBCTools.release(null, ps, conn);
 86  86         }
 87  87     }
 88  88     //一条数据中没有图片执行的方法
 89  89     public void update(Student student) {
 90  90         Connection conn = null;
 91  91         PreparedStatement ps = null;
 92  92         try {
 93  93             conn = JDBCTools.getORACEL();
 94  94             JDBCTools.begin(conn);
 95  95             String sql = "insert into Student(RECORD_ID,IMAGES)values(?,empty_blob())";
 96  96             ps = conn.prepareStatement(sql);
 97  97             ps.setString(1, student.getRECORD_ID());
 98  98             ps.executeUpdate();
 99  99             JDBCTools.commit(conn);
100 100         } catch (Exception e) {
101 101             e.printStackTrace();
102 102         } finally {
103 103             JDBCTools.release(null, ps, conn);
104 104         }
105 105     }
106 106 }

 

转载于:https://www.cnblogs.com/suzhou-zhanglei/p/6017562.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值