介绍
首先通过下面的图来展示一下从oracle到db2的流程:
从上面的图可以看到把oracle的数据移植到db2数据库中我们的java程序起到一个转换器的作用,把oracle中的不同的数据类型映射为相应的db2数据库中的类型同时把从oracle中查询出的数据插入到db2数据库中从而完成整个移植过程。下面我就结合java程序给大家是如何实现这一功能的
1.导入必需的java类
import java.text.*;import java.util.*;import java.io.*;import java.sql.*;import java.lang.*;
2.下面是从属性文件中读取在转换器使用的配置信息
//下面的代码是读属性文件的信息properties props=new properties();file f=new file("oracletodb2.properties");fileinputstream in=new fileinputstream(f);props.load(in);string db2_dbname=props.getproperty("db2_dbname");string db2_username=props.getproperty("db2_username");string db2_password=props.getproperty("db2_password");string oracle_url=props.getproperty("oracle_url");string oracle_dbname=props.getproperty("oracle_dbname");string oracle_username=props.getproperty("oracle_username");string oracle_password=props.getproperty("oracle_password");string io_tables=props.getproperty("io_tables");通过java的输入流来读取文件中的内容
其中
- oracletodb2.properties为属性文件名;
- db2_dbname, db2_username, db2_password 分别为目标db2数据库的数据库名,数据库用户名,口令。
- oracle_url ,oracle_dbname,oracle_username,oracle_password分别为oracle数据库的地址,数据库名,用户名,口令。
- io_tables为从oracle移植到db2的表名
下面是属性文件的全部内容: db2_dbname=db2databasenamedb2_username=db2usernamedb2_password=db2passworddb2_host=127.0.0.1oracle_url=127.0.0.1oracle_dbname=oracledatabasenameoracle_username=oracleusernameoracle_password=oraclepasswordio_tables=io_tables
3.接着分别建立db2和oracle数据库的连接
其中的参数为从属性文件中读入的值,jdbc用的是廋客户机的type4:
drivermanager.registerdriver(new com.ibm.db2.jdbc.app.db2driver());class.forname ("oracle.jdbc.driver.oracledriver").newinstance ();connection connora = drivermanager.getconnection("jdbc:db2:"+db2_dbname, db2_username,db2_password); //connection connora = drivermanager.getconnection("jdbc:db2://"+db2_host+":"+db2_port+"/"+db2_dbname, db2_username,db2_password); connection conndb2 =drivermanager.getconnection("jdbc:oracle:thin:@"+oracle_url+":1521:"+oracle_dbname,oracle_username, oracle_password);4.转换器的核心java类
转换器的核心java类的方法为一个静态的java方法migratetable,我在代码中给解释具体的功能的内容和为什么:
//引入oracle和db2的连接及需要移植的表public static void migratetable(connection connora, connection conndb2, string strtablename) throws exception {//进行防错处理 if (strtablename.length() < 3) return; int nrows = 0; filewriter fw = null; printwriter pw = null; statement stmt = null; resultset rst = null; statement stmtdelete = null; preparedstatement stmtup = null; try { //记录转换过程中的信息 fw = new filewriter("oracletodb2.log", true); pw = new printwriter(fw); system.out.println("migrating table " + strtablename); pw.println("migrating table " + strtablename); //确定连接赋予sql语句 stmt = connora.createstatement(); //进行表内容的查询 rst = stmt.executequery("select * from " + strtablename); //读入表的元数据 resultsetmetadata meta = rst.getmetadata(); string strsql = "insert into " + strtablename; string strfields = "("; string strvalues = " values("; //根据读入的表的元数据的内容进行递归的读取 object arobjdata[] = new object[meta.getcolumncount()]; for (int i = 0; i < arobjdata.length; i ++) { if (i > 0) { strvalues = strvalues + ", "; strfields = strfields + ", "; } strvalues = strvalues + "?"; strfields = strfields + meta.getcolumnname(i + 1); } strvalues = strvalues + ")"; strfields = strfields + ")"; strsql = strsql + strfields + strvalues; //建立db2数据库的连接 stmtdelete = conndb2.createstatement(); //删除原db2数据库表中的数据 stmtdelete.executeupdate("delete from " + strtablename); stmtdelete.close(); stmtup = conndb2.preparestatement(strsql); while (rst.next()) { for (int i = 0; i < arobjdata.length; i ++) { arobjdata[i] = rst.getobject(i + 1);//进行二进制、字符类型的转换 if (arobjdata[i] != null && arobjdata[i] instanceof string) arobjdata[i] = (object)iso2gb((string)arobjdata[i]); } for (int i = 0; i < arobjdata.length; i ++) { if (meta.getcolumntype(i + 1) == java.sql.types.blob) { byte ardata[] = null; if (arobjdata[i] != null) ardata = ((blob)arobjdata[i]).getbytes(1l, (int)((blob)arobjdata[i]).length()); //读取二进制的数据 stmtup.setbytes(i + 1, ardata); } else if (meta.getcolumntype(i + 1) == java.sql.types.clob) { string strdata = null; if (arobjdata[i] != null) strdata = ((clob)arobjdata[i]).getsubstring(1l, (int)((clob)arobjdata[i]).length()); stmtup.setstring(i + 1, iso2gb(strdata)); } else { if (arobjdata[i] != null) stmtup.setobject(i + 1, arobjdata[i]); else stmtup.setstring(i + 1, null); } } stmtup.executeupdate(); //计数器表示一个表中移植了多少行 nrows ++; }上面的代码示整个转换器的关键部分,它实现了整个移植过程的大部分功能而且可以实现代码页,二进制的数据的移植是一个功能强大的移植工具。下面我就运行的步骤作详细的演示:
- 在操作系统上的类路径中加入jdk的路径。
- 配置属性文件。
- 把oracle数据库中的表结构倒成.ddl文件,并调整其中的数据类型映射为db2中的类型建立表。
- 编译整个java文件
- 执行
- 查看日志信息
下面是整个程序的完整代码:
import java.text.*;import java.util.*;import java.io.*;import java.sql.*;import java.lang.*;public class oracletodb2{ public static void main(string[] args) throws exception { //下面的代码是读属性文件的信息 properties props=new properties(); file f=new file("oracletodb2.properties"); fileinputstream in=new fileinputstream(f); props.load(in); string db2_dbname=props.getproperty("db2_dbname"); string db2_username=props.getproperty("db2_username"); string db2_password=props.getproperty("db2_password"); string oracle_url=props.getproperty("oracle_url"); string oracle_dbname=props.getproperty("oracle_dbname"); string oracle_username=props.getproperty("oracle_username"); string oracle_password=props.getproperty("oracle_password"); string io_tables=props.getproperty("io_tables"); //建立db2和oracle数据库的分别连接 // string db2_url = "jdbc:db2://"+db2_host+":"+db2_port+"/"+db2_dbname; drivermanager.registerdriver(new com.ibm.db2.jdbc.app.db2driver()); //drivermanager.registerdriver(new com.ibm.db2.jdbc.net.db2driver()); class.forname ("oracle.jdbc.driver.oracledriver").newinstance (); connection connora = drivermanager.getconnection("jdbc:db2:"+db2_dbname, db2_username,db2_password); //connection connora = drivermanager.getconnection(" jdbc:db2://"+db2_host+":"+db2_port+"/"+db2_dbname, db2_username,db2_password); connection conndb2 = drivermanager.getconnection(" jdbc:oracle:thin:@"+oracle_url+":1521:"+oracle_dbname,oracle_username, oracle_password); filereader reader = new filereader(io_tables); linenumberreader lreader = new linenumberreader(reader); string strtable = null; while ((strtable = lreader.readline()) != null) { try { migratetable(connora, conndb2, strtable); } catch (exception e) { e.printstacktrace(); } } reader.close(); connora.close(); conndb2.close(); system.out.println("ok"); }public static void migratetable(connection connora, connection conndb2, string strtablename) throws exception{ if (strtablename.length() < 3) return; int nrows = 0; filewriter fw = null; printwriter pw = null; statement stmt = null; resultset rst = null; statement stmtdelete = null; preparedstatement stmtup = null; try { fw = new filewriter("oracletodb2.log", true); pw = new printwriter(fw); system.out.println("migrating table " + strtablename); pw.println("migrating table " + strtablename); stmt = connora.createstatement(); rst = stmt.executequery("select * from " + strtablename); resultsetmetadata meta = rst.getmetadata(); string strsql = "insert into " + strtablename; string strfields = "("; string strvalues = " values("; object arobjdata[] = new object[meta.getcolumncount()]; for (int i = 0; i < arobjdata.length; i ++) { if (i > 0) { strvalues = strvalues + ", "; strfields = strfields + ", "; } strvalues = strvalues + "?"; strfields = strfields + meta.getcolumnname(i + 1); } strvalues = strvalues + ")"; strfields = strfields + ")"; strsql = strsql + strfields + strvalues; stmtdelete = conndb2.createstatement(); stmtdelete.executeupdate("delete from " + strtablename); stmtdelete.close(); stmtup = conndb2.preparestatement(strsql); while (rst.next()) { for (int i = 0; i < arobjdata.length; i ++) { arobjdata[i] = rst.getobject(i + 1); if (arobjdata[i] != null && arobjdata[i] instanceof string) arobjdata[i] = (object)iso2gb((string)arobjdata[i]); } for (int i = 0; i < arobjdata.length; i ++) { if (meta.getcolumntype(i + 1) == java.sql.types.blob) { byte ardata[] = null; if (arobjdata[i] != null) ardata = ((blob)arobjdata[i]).getbytes(1l, (int)((blob)arobjdata[i]).length()); stmtup.setbytes(i + 1, ardata); } else if (meta.getcolumntype(i + 1) == java.sql.types.clob) { string strdata = null; if (arobjdata[i] != null) strdata = ((clob)arobjdata[i]).getsubstring(1l, (int)((clob)arobjdata[i]).length()); stmtup.setstring(i + 1, iso2gb(strdata)); } else { if (arobjdata[i] != null) stmtup.setobject(i + 1, arobjdata[i]); else stmtup.setstring(i + 1, null); } } stmtup.executeupdate(); nrows ++; } pw.println("" + nrows + " rows migrated"); system.out.println("" + nrows + " rows migrated"); } catch(exception e) { if (pw != null) e.printstacktrace(pw); e.printstacktrace(); } finally { try { if (rst != null) rst.close(); } catch (exception ee) { ee.printstacktrace(pw); } try { if (stmt != null) stmt.close(); } catch (exception ee) { ee.printstacktrace(pw); } try { if (stmtdelete != null) stmtdelete.close(); } catch (exception ee) { ee.printstacktrace(pw); } try { if (stmtup != null) stmtup.close(); } catch (exception ee) { ee.printstacktrace(pw); } if (fw != null) { fw.flush(); fw.close(); } }}