如何把 DB2 数据迁移到 oracle 中
发布网友
发布时间:2022-04-22 19:17
我来回答
共2个回答
懂视网
时间:2022-05-02 15:34
package com.oracle.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil { //连接oracle数据库的应用
private static final String jdbcName="oracle.jdbc.driver.OracleDriver";
private static final String url="jdbc:oracle:thin:@localhost:1521:ORAC";
private static final String user="scott";
private static final String password="tiger";
public Connection getConn() throws Exception {
Class.forName(jdbcName);
/**
*DriverManager(唯一一个用到的类)
*Connection(接口)
*/
Connection conn=DriverManager.getConnection(url, user, password);
return conn;
}
public void closeConn(Connection conn) throws Exception {
if(conn!=null){
conn.close();
}
}
public static void main(String[] args) throws Exception {
DbUtil dbUtil=new DbUtil();
Connection conn=dbUtil.getConn();
if(conn!=null){
System.out.println("Oracle数据库连接成功");
}else{
System.out.println("Oracle数据库连接失败");
}
dbUtil.closeConn(conn);
}
}
2.
package com.oracle.test;
import static org.junit.Assert.*;
import java.sql.Connection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.oracle.util.DbUtil;
public class JunitTestOracle { //在Junit中连接oracle数据库
DbUtil dbUtil=new DbUtil();
@Before
public void setUp() throws Exception { //setUp()在Junit中用到的方法
}
@After
public void tearDown() throws Exception { //tearDown()在Junit中用到的方法
}
@Test
public void test() throws Exception { // 在test()方法中写内容
Connection conn=dbUtil.getConn();
if(conn!=null){System.out.println("Oracle连接成功");}
}
}
Junit连接oracle数据库
标签:unittest span 失败 return pack size junit .sql setup
热心网友
时间:2022-05-02 12:42
数据迁移:
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.;
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+"秒");
}
}
}