如何把db2的数据导入到oracle
1个回答
2017-01-21 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
数据迁移:
1、在DB2数据库中通过以下表查询出表的结构
SELECT
TABNAME TAB, --表英文名称
COLNAME COL, --列名称
CASE
WHEN TYPENAME='VARCHAR' THEN 'VARCHAR2'
WHEN TYPENAME LIKE 'SMALLINT' OR TYPENAME LIKE 'BIGINT' THEN 'INTEGER'
WHEN TYPENAME ='CHARACTER' THEN 'CHAR'
WHEN TYPENAME='DECFLOAT' OR TYPENAME ='DECIMAL' THEN 'NUMBER'
ELSE TYPENAME
END TY
, --数据类型
LENGTH LEN , --列长度
scale S, --精度
CASE
WHEN NULLS='N' THEN '否'
WHEN NULLS='Y' THEN '是'
END N --是否为空
--TY||'('||LEN||')'
FROM
SYSCAT.COLUMNS S
WHERE
LEFT( TABSCHEMA, 3 ) <> 'SYS'
/* and s.SCALE <>0*/
ORDER BY
S.TABNAME,
S.COLNO
2、根据表的结构在oracle数据库中建表(脚本见《create_tab_onOracle.sql》)
3、通过编写java程序把数据从DB2导入到oracle数据库中(以下代码思想仅供参考)
3.1、 导入db2jcc_license_cu.jar 、db2jcc.jar 、ojdbc14.jar 三个jar包即可
3.2、 编写三个工具类
此类链接DB2数据库工具类
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionDB2 {
private static final String URL="jdbc:db2://192.168.0.98:50000/sem"; //DB2数据库url
private static final String USER="db2admin"; //DB2数据库账号
private static final String PASSWORD="dnhc9988"; //DB2数据库密码
static{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance(); //利用反射注册驱动
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
Connection conn=null;
try {
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
此类链接oracle数据库工具类
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionDB2 {
private static final String URL="jdbc:db2://192.168.0.98:50000/sem"; //DB2数据库url
private static final String USER="db2admin"; //DB2数据库账号
private static final String PASSWORD="dnhc9988"; //DB2数据库密码
static{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance(); //利用反射注册驱动
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
Connection conn=null;
try {
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
此类用来关闭数据库连接工具类
package com.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CloseFunction {
//关闭连接方法,释放资源
public static void closeConnection(Connection conn){
if (conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭执行sql,释放资源
public static void closeExecuteSQL(Statement preparedStatement){
if (preparedStatement !=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭查询SQL结果,释放资源
public static void closeResultSet(ResultSet resultSet){
if(resultSet !=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
关键在此类:需要从DB2数据库查询语句放在db2SQL变量中,往oracle插入数据的语句放在oracleSQL变量中。注意,查询和插入的语句中字段顺序要一致,在while循环里要对日期时间处理(用setDate),然后执行junit测试类就可以了。
package com.dao;
import java.security.interfaces.RSAKey;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import com.util.CloseFunction;
import com.util.ConnectionDB2;
import com.util.ConnectionOracle;
public class FromDB2ToOracle {
@Test
public void test1() throws SQLException{
long startTime=System.currentTimeMillis(); //记录开始时间
Connection connDB2=null; //链接DB2数据库
Connection connOracle=null; //链接oracle数据库
Statement statement=null;
ResultSet resultSet=null;
PreparedStatement preparedStatement=null;
try {
ConnectionOracle connectionOracle=new ConnectionOracle();
// System.out.println(connectionOracle+"链接oracle成功!");
ConnectionDB2 connectionDB2=new ConnectionDB2();
// System.out.println(connectionDB2+"-->>>链接DB2数据库成功!");
connDB2=connectionDB2.getConnection();
//执行DB2数据库sql语句,此处并非固定代码
String
db2SQL="SELECT ID,SENDNO,SENDTYPE,BRNUMBER
,SENDTIME,ALARMTIME,SENDCONTENT,ISSEND ,ELEALARMID from
DB2ADMIN.ET_SENDMESSAGEINFO";
//执行oracle数据库sql语句,此处并非固定代码
//String oracleSQL="INSERT INTO EPC.SYS_LOG(ID,REMARK) values(?,?)";
String
oracleSQL="INSERT INTO
EPC.ET_SENDMESSAGEINFO(ID,SENDNO,SENDTYPE,BRNUMBER
,SENDTIME,ALARMTIME,SENDCONTENT,ISSEND ,ELEALARMID )"
+ "VALUES(?,?,?,?,?,?,?,?,?)";
statement=connDB2.createStatement();
statement.execute(db2SQL); //执行sql语句
resultSet=statement.getResultSet(); //获取DB2数据库的结果集
connOracle=connectionOracle.getConnection(); //获取oracle数据库连接
connOracle.setAutoCommit(false); //把oracle数据库设置为非自动提交,以免在再循环里每次都提交,减低效率
preparedStatement=connOracle.prepareStatement(oracleSQL); //执行oracle语句,预编译
int num=0;
while (resultSet.next()) {
num ++;
preparedStatement.setString(1,resultSet.getString("ID"));
preparedStatement.setString(2, resultSet.getString("SENDNO"));
preparedStatement.setString(3, resultSet.getString("SENDTYPE"));
preparedStatement.setString(4, resultSet.getString("BRNUMBER"));
preparedStatement.setDate(5, resultSet.getDate("SENDTIME"));
preparedStatement.setDate(6, resultSet.getDate("ALARMTIME"));
preparedStatement.setString(7, resultSet.getString("SENDCONTENT"));
preparedStatement.setString(8, resultSet.getString("ISSEND"));
preparedStatement.setString(9, resultSet.getString("ELEALARMID"));/*
preparedStatement.setString(10, resultSet.getString("CONSUMETIME"));
preparedStatement.setString(11, resultSet.getString("TASKID"));
preparedStatement.setString(12, resultSet.getString("ISSYNCHRO"));
preparedStatement.setString(13,resultSet.getString("LOGTYPE"));
preparedStatement.setString(14, resultSet.getString("ISCACHE"));
preparedStatement.setString(15, resultSet.getString("LAST_RPORT_TIME"));
preparedStatement.setString(16, resultSet.getString("AUTO_REMEMBER"));
preparedStatement.setString(17, resultSet.getString("REMARK"));*/
preparedStatement.addBatch();
//每一万次在oracle数据库里提交事务
if(num>10000){
preparedStatement.executeBatch();
connOracle.commit();
num=0;
}
}
preparedStatement.executeBatch();
connOracle.commit();
} catch (Exception e) {
connOracle.rollback(); //oracle数据库事务回滚
e.printStackTrace();
}finally{
new CloseFunction().closeConnection(connOracle); //关闭oracle数据库,释放资源
new CloseFunction().closeConnection(connDB2); //关闭DB2数据库,释放资源
long endTime=System.currentTimeMillis(); //记录程序结束时间
System.out.println("总的时间:"+(endTime-startTime)/1000+"秒");
}
}
}
1、在DB2数据库中通过以下表查询出表的结构
SELECT
TABNAME TAB, --表英文名称
COLNAME COL, --列名称
CASE
WHEN TYPENAME='VARCHAR' THEN 'VARCHAR2'
WHEN TYPENAME LIKE 'SMALLINT' OR TYPENAME LIKE 'BIGINT' THEN 'INTEGER'
WHEN TYPENAME ='CHARACTER' THEN 'CHAR'
WHEN TYPENAME='DECFLOAT' OR TYPENAME ='DECIMAL' THEN 'NUMBER'
ELSE TYPENAME
END TY
, --数据类型
LENGTH LEN , --列长度
scale S, --精度
CASE
WHEN NULLS='N' THEN '否'
WHEN NULLS='Y' THEN '是'
END N --是否为空
--TY||'('||LEN||')'
FROM
SYSCAT.COLUMNS S
WHERE
LEFT( TABSCHEMA, 3 ) <> 'SYS'
/* and s.SCALE <>0*/
ORDER BY
S.TABNAME,
S.COLNO
2、根据表的结构在oracle数据库中建表(脚本见《create_tab_onOracle.sql》)
3、通过编写java程序把数据从DB2导入到oracle数据库中(以下代码思想仅供参考)
3.1、 导入db2jcc_license_cu.jar 、db2jcc.jar 、ojdbc14.jar 三个jar包即可
3.2、 编写三个工具类
此类链接DB2数据库工具类
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionDB2 {
private static final String URL="jdbc:db2://192.168.0.98:50000/sem"; //DB2数据库url
private static final String USER="db2admin"; //DB2数据库账号
private static final String PASSWORD="dnhc9988"; //DB2数据库密码
static{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance(); //利用反射注册驱动
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
Connection conn=null;
try {
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
此类链接oracle数据库工具类
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionDB2 {
private static final String URL="jdbc:db2://192.168.0.98:50000/sem"; //DB2数据库url
private static final String USER="db2admin"; //DB2数据库账号
private static final String PASSWORD="dnhc9988"; //DB2数据库密码
static{
try {
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance(); //利用反射注册驱动
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
Connection conn=null;
try {
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
此类用来关闭数据库连接工具类
package com.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CloseFunction {
//关闭连接方法,释放资源
public static void closeConnection(Connection conn){
if (conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭执行sql,释放资源
public static void closeExecuteSQL(Statement preparedStatement){
if (preparedStatement !=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭查询SQL结果,释放资源
public static void closeResultSet(ResultSet resultSet){
if(resultSet !=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
关键在此类:需要从DB2数据库查询语句放在db2SQL变量中,往oracle插入数据的语句放在oracleSQL变量中。注意,查询和插入的语句中字段顺序要一致,在while循环里要对日期时间处理(用setDate),然后执行junit测试类就可以了。
package com.dao;
import java.security.interfaces.RSAKey;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import com.util.CloseFunction;
import com.util.ConnectionDB2;
import com.util.ConnectionOracle;
public class FromDB2ToOracle {
@Test
public void test1() throws SQLException{
long startTime=System.currentTimeMillis(); //记录开始时间
Connection connDB2=null; //链接DB2数据库
Connection connOracle=null; //链接oracle数据库
Statement statement=null;
ResultSet resultSet=null;
PreparedStatement preparedStatement=null;
try {
ConnectionOracle connectionOracle=new ConnectionOracle();
// System.out.println(connectionOracle+"链接oracle成功!");
ConnectionDB2 connectionDB2=new ConnectionDB2();
// System.out.println(connectionDB2+"-->>>链接DB2数据库成功!");
connDB2=connectionDB2.getConnection();
//执行DB2数据库sql语句,此处并非固定代码
String
db2SQL="SELECT ID,SENDNO,SENDTYPE,BRNUMBER
,SENDTIME,ALARMTIME,SENDCONTENT,ISSEND ,ELEALARMID from
DB2ADMIN.ET_SENDMESSAGEINFO";
//执行oracle数据库sql语句,此处并非固定代码
//String oracleSQL="INSERT INTO EPC.SYS_LOG(ID,REMARK) values(?,?)";
String
oracleSQL="INSERT INTO
EPC.ET_SENDMESSAGEINFO(ID,SENDNO,SENDTYPE,BRNUMBER
,SENDTIME,ALARMTIME,SENDCONTENT,ISSEND ,ELEALARMID )"
+ "VALUES(?,?,?,?,?,?,?,?,?)";
statement=connDB2.createStatement();
statement.execute(db2SQL); //执行sql语句
resultSet=statement.getResultSet(); //获取DB2数据库的结果集
connOracle=connectionOracle.getConnection(); //获取oracle数据库连接
connOracle.setAutoCommit(false); //把oracle数据库设置为非自动提交,以免在再循环里每次都提交,减低效率
preparedStatement=connOracle.prepareStatement(oracleSQL); //执行oracle语句,预编译
int num=0;
while (resultSet.next()) {
num ++;
preparedStatement.setString(1,resultSet.getString("ID"));
preparedStatement.setString(2, resultSet.getString("SENDNO"));
preparedStatement.setString(3, resultSet.getString("SENDTYPE"));
preparedStatement.setString(4, resultSet.getString("BRNUMBER"));
preparedStatement.setDate(5, resultSet.getDate("SENDTIME"));
preparedStatement.setDate(6, resultSet.getDate("ALARMTIME"));
preparedStatement.setString(7, resultSet.getString("SENDCONTENT"));
preparedStatement.setString(8, resultSet.getString("ISSEND"));
preparedStatement.setString(9, resultSet.getString("ELEALARMID"));/*
preparedStatement.setString(10, resultSet.getString("CONSUMETIME"));
preparedStatement.setString(11, resultSet.getString("TASKID"));
preparedStatement.setString(12, resultSet.getString("ISSYNCHRO"));
preparedStatement.setString(13,resultSet.getString("LOGTYPE"));
preparedStatement.setString(14, resultSet.getString("ISCACHE"));
preparedStatement.setString(15, resultSet.getString("LAST_RPORT_TIME"));
preparedStatement.setString(16, resultSet.getString("AUTO_REMEMBER"));
preparedStatement.setString(17, resultSet.getString("REMARK"));*/
preparedStatement.addBatch();
//每一万次在oracle数据库里提交事务
if(num>10000){
preparedStatement.executeBatch();
connOracle.commit();
num=0;
}
}
preparedStatement.executeBatch();
connOracle.commit();
} catch (Exception e) {
connOracle.rollback(); //oracle数据库事务回滚
e.printStackTrace();
}finally{
new CloseFunction().closeConnection(connOracle); //关闭oracle数据库,释放资源
new CloseFunction().closeConnection(connDB2); //关闭DB2数据库,释放资源
long endTime=System.currentTimeMillis(); //记录程序结束时间
System.out.println("总的时间:"+(endTime-startTime)/1000+"秒");
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |