Oracle中得blob字段导入到DB2的Blob字段

1.背景

在项目中使用blob大字段来存储报文,最近有客户需要使用DB2数据库来部署应用,所有只得把oracle中初始化脚本导入到DB2中,在制作DB2的初始化脚本

2.问题

怎么样把oracle中得含blob字段的表导入到DB2中呢,在网上转了一下没有发现,决定写程序解决

3.解决问题

使用JDBC连接先查出oracle中得表,然后插入到DB2中。先新建java project,然后添加oracle和DB2的驱动包,如下图:


DB2ConnectionFactory源码为:

[java]  view plain copy print ?
  1. package com.ylink.export;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6.   
  7. public class DB2ConnectionFactory {  
  8.   
  9.           
  10.   
  11.         final static String DRIVER = "com.ibm.db2.jcc.DB2Driver";  
  12.         final static String CONSTR = "jdbc:db2://172.168.6.212:50000/tps";  
  13.         final static String USERNAME = "db2admin";  
  14.         final static String USERPASS = "db2admin";  
  15.           
  16.         /** 
  17.          * 获得数据库连接 
  18.          * @return 
  19.          */  
  20.         public static Connection getConnection(){  
  21.             Connection con = null;  
  22.           
  23.             try {  
  24.                   
  25.                 Class.forName(DRIVER);  
  26.                 con = DriverManager.getConnection(CONSTR,USERNAME,USERPASS);  
  27.                 con.setAutoCommit(false);  //设置不自动提交事务  
  28.                   
  29.             }catch (SQLException e) {  
  30.                 System.out.println("sql语句错误"+e.getMessage());  
  31.             } catch (ClassNotFoundException e) {  
  32.                 System.out.println(e.getMessage());  
  33.             }  
  34.               
  35.             return con;  
  36.         }  
  37.     }  
IOperationCore接口源码为:
[java]  view plain copy print ?
  1. /** 
  2.  * 数据库常用操作封装 
  3.  */  
  4. package com.ylink.export;  
  5.   
  6. import java.lang.reflect.InvocationTargetException;  
  7. import java.sql.ResultSet;  
  8. import java.sql.SQLException;  
  9. import java.util.List;  
  10.   
  11. /** 类名:IOperationCore<br> 
  12.  *  
  13.  * 作用: 该接口封装了数据库操作的大部分方法<br> 
  14.  *  
  15.  */  
  16. public interface IOperationCore {  
  17.    /** sql更新语句 
  18.     *  
  19.     * @param queryString 查询语句 
  20.     * @return 返回一个<code>ResultSet</code>结果集 
  21.     *  
  22.     * @exception SQLException */  
  23.    ResultSet executeQuery(String queryString) throws SQLException;  
  24.    /**   
  25.     * sql更新语句 
  26.     *  
  27.     * @param updateString 数据库更新语句 
  28.     * @return 更新数据库影响行数 
  29.     *  
  30.     * @exception SQLException */  
  31.    int executeUpdate(String updateString) throws SQLException;  
  32.      
  33.      
  34.    @SuppressWarnings("unchecked")  
  35.     public <T> List<T> queryForList(String sql, Class<T> clazz,  
  36.             Object... params)throws SQLException, InstantiationException,  
  37.             IllegalAccessException, InvocationTargetException,  
  38.             ClassNotFoundException;  
  39.     
  40.    /**  
  41.     * 释放系统连接资源 
  42.     *  
  43.     * @exception SQLException 如果关闭失败将抛出<code>SQLException</code>*/  
  44.    void dispose() throws SQLException;  
  45.   
  46. }  

OperationCoreImpl源码为:

[java]  view plain copy print ?
  1. package com.ylink.export;  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4. import java.lang.reflect.Method;  
  5. import java.sql.Connection;  
  6. import java.sql.PreparedStatement;  
  7. import java.sql.ResultSet;  
  8. import java.sql.ResultSetMetaData;  
  9. import java.sql.SQLException;  
  10. import java.sql.Statement;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13.   
  14. /** 
  15.  * 类名:OperationCoreImplements<br> 
  16.  *  
  17.  * 作用: 该类实现IOperationCore接口的所有方法<br> 
  18.  */  
  19. public class OperationCoreImpl implements IOperationCore {  
  20.     protected Connection aConnection = null;  
  21.     protected Statement ps = null;  
  22.     protected ResultSet rs = null;  
  23.     protected ResultSetMetaData rsmd = null;  
  24.     protected static OperationCoreImpl m_instance = null;  
  25.   
  26.     /** 
  27.      * Singleton 即单例(态)模式,用来生成对象唯一实例的方法 
  28.      *  
  29.      * @return OperationCoreImplements的一个实例 
  30.      * @throws Exception 
  31.      */  
  32.     public static OperationCoreImpl createFactory() throws Exception {  
  33.         if (m_instance == null)  
  34.             m_instance = new OperationCoreImpl();  
  35.         return m_instance;  
  36.     }  
  37.   
  38.     /** @exception Exception */  
  39.     public OperationCoreImpl() throws Exception {  
  40.         init();  
  41.     }  
  42.   
  43.     private void init() throws Exception {  
  44.         aConnection = OracleConnectionFactory.getConnection();  
  45.     }  
  46.   
  47.     /** 
  48.      * 释放系统连接资源 
  49.      */  
  50.     public void dispose() {  
  51.         try {  
  52.             if (rs != null)  
  53.                 rs.close();  
  54.         } catch (SQLException e) {  
  55.   
  56.             e.printStackTrace();  
  57.         }  
  58.         try {  
  59.             if (ps != null)  
  60.                 ps.close();  
  61.         } catch (SQLException e) {  
  62.   
  63.             e.printStackTrace();  
  64.         }  
  65.         try {  
  66.             if (aConnection != null)  
  67.                 aConnection.close();  
  68.         } catch (SQLException e) {  
  69.             e.printStackTrace();  
  70.         }  
  71.   
  72.     }  
  73.   
  74.     /** 
  75.      * 返回ResultSet对象 
  76.      *  
  77.      * @param queryString 
  78.      *            查询语句 
  79.      * @return 返回一个<code>ResultSet</code>结果集 
  80.      *  
  81.      * @exception SQLException 
  82.      */  
  83.     public ResultSet executeQuery(String queryString) {  
  84.         try {  
  85.             ps = aConnection.createStatement();  
  86.             rs = ps.executeQuery(queryString);  
  87.         } catch (SQLException e) {  
  88.             rs = null;  
  89.             e.printStackTrace();  
  90.         }  
  91.         return rs;  
  92.     }  
  93.   
  94.     //返回list通用   JDBC直连  
  95.     @SuppressWarnings("unchecked")  
  96.     public <T> List<T> queryForList(String sql, Class<T> clazz,  
  97.             Object... params) throws SQLException, InstantiationException,  
  98.             IllegalAccessException, InvocationTargetException,  
  99.             ClassNotFoundException {  
  100.   
  101.         if (clazz == null) {  
  102.             throw new IllegalArgumentException("clazz is null");  
  103.         }  
  104.   
  105.         ResultSet rs = null;  
  106.         PreparedStatement ps = null;  
  107.   
  108.         try {  
  109.             List<T> resultList = new ArrayList<T>();  
  110.   
  111.             ps = aConnection.prepareStatement(sql);  
  112.             if (params != null) {  
  113.                 for (int i = 0; i < params.length; i++) {  
  114.                     ps.setObject(i + 1, params[i]);  
  115.                 }  
  116.             }  
  117.   
  118.             rs = ps.executeQuery();  
  119.             T t = null;  
  120.             Method[] allMethod = clazz.getMethods();  
  121.             List<Method> setterMethodList = new ArrayList<Method>();  
  122.             for (Method m : allMethod) {  
  123.                 if (m.getName().startsWith("set")) {  
  124.                     setterMethodList.add(m);  
  125.                 }  
  126.             }  
  127.   
  128.             String columnName = null;  
  129.             Class parameterType = null;  
  130.             if (rs != null) {  
  131.                 while (rs.next()) {  
  132.                     t = clazz.newInstance();  
  133.                     for (Method m : setterMethodList) {  
  134.                         columnName = m.getName().substring(34).toLowerCase()  
  135.                                 + m.getName()  
  136.                                         .substring(4, m.getName().length());  
  137.   
  138.                         parameterType = m.getParameterTypes()[0];  
  139.   
  140.                         if (parameterType.isPrimitive()) {  
  141.                             if (parameterType == Boolean.TYPE) {  
  142.                                 m.invoke(t, rs.getBoolean(columnName));  
  143.                             } else if (parameterType == Byte.TYPE) {  
  144.                                 m.invoke(t, rs.getByte(columnName));  
  145.                             } else if (parameterType == Short.TYPE) {  
  146.                                 m.invoke(t, rs.getShort(columnName));  
  147.                             } else if (parameterType == Character.TYPE) {  
  148.                                 m.invoke(t, rs.getString(columnName).charAt(0));  
  149.                             } else if (parameterType == Integer.TYPE) {  
  150.                                 m.invoke(t, rs.getInt(columnName));  
  151.                             } else if (parameterType == Long.TYPE) {  
  152.                                 m.invoke(t, rs.getLong(columnName));  
  153.                             } else if (parameterType == Float.TYPE) {  
  154.                                 m.invoke(t, rs.getFloat(columnName));  
  155.                             } else if (parameterType == Double.TYPE) {  
  156.                                 m.invoke(t, rs.getDouble(columnName));  
  157.                             }  
  158.   
  159.                         } else {  
  160.                             m.invoke(t, rs.getObject(columnName));  
  161.                         }  
  162.                     }  
  163.   
  164.                     resultList.add(t);  
  165.                 }  
  166.             }  
  167.   
  168.             return resultList;  
  169.   
  170.         } finally {  
  171.             dispose();  
  172.         }  
  173.     }  
  174.   
  175.     /** 
  176.      * 增、删、改操作 
  177.      *  
  178.      * @param updateString 
  179.      *            数据库更新语句 
  180.      * @return 更新数据库影响行数 
  181.      *  
  182.      * @exception SQLException 
  183.      */  
  184.     public int executeUpdate(String updateString) {  
  185.         int effectedRows = 0;  
  186.         try {  
  187.             aConnection.setAutoCommit(false);  
  188.             ps = aConnection.createStatement();  
  189.             effectedRows = ps.executeUpdate(updateString);  
  190.             aConnection.commit();  
  191.         } catch (SQLException ex) {  
  192.             System.out.println("数据库写操作失败!");  
  193.             if (aConnection != null) {  
  194.                 try {  
  195.                     aConnection.rollback();  
  196.                     System.out.println("JDBC事务回滚成功");  
  197.                 } catch (SQLException e) {  
  198.                     System.out.println("JDBC事务回滚失败");  
  199.                     e.printStackTrace();  
  200.                 }  
  201.             }  
  202.         }  
  203.         return effectedRows;  
  204.     }  
  205. }  


OracleConnectionFactory源码为:

[java]  view plain copy print ?
  1. package com.ylink.export;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6.   
  7. public class OracleConnectionFactory {  
  8.   
  9.       
  10.   
  11.     final static String DRIVER = "oracle.jdbc.driver.OracleDriver";  
  12.     final static String CONSTR = "jdbc:oracle:thin:@172.168.9.70:1521:tps";  
  13.     final static String USERNAME = "tps";  
  14.     final static String USERPASS = "tps";  
  15.       
  16.     /** 
  17.      * 获得数据库连接 
  18.      * @return 
  19.      */  
  20.     public static  Connection getConnection(){  
  21.         Connection con = null;  
  22.       
  23.         try {  
  24.               
  25.             Class.forName(DRIVER);  
  26.             con = DriverManager.getConnection(CONSTR,USERNAME,USERPASS);  
  27.             con.setAutoCommit(false);  //设置不自动提交事务  
  28.               
  29.         }catch (SQLException e) {  
  30.             System.out.println("sql语句错误"+e.getMessage());  
  31.         } catch (ClassNotFoundException e) {  
  32.             System.out.println(e.getMessage());  
  33.         }  
  34.           
  35.         return con;  
  36.     }  
  37. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值