转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50586379
最近碰到oracle数据库迁移,由于高级的迁移方法还不会,当前只会手动导入导出迁移。在迁移完所有数据表后,需要继续迁移索引、序列、视图、触发器、函数等。
本文主要说明批量迁移索引、序列、视图。触发器由于执行过程中异常,还没有找到方法自动处理;函数就只有几个手动处理掉了。
1. java程序
本次迁移面临问题:索引较多有68个,序列28个,视图8个。手动一个一个处理,看得眼睛都花了。就弄个java脚本处理。
不过各位碰到具体问题需要做微调。
@H_404_16@package com.autonavi.service; @H_404_16@import java.sql.Connection; @H_404_16@import java.sql.PreparedStatement; @H_404_16@import java.sql.ResultSet; @H_404_16@import java.sql.sqlException; @H_404_16@import java.sql.sqlSyntaxErrorException; @H_404_16@import java.sql.Statement; @H_404_16@import com.autonavi.db.DataBaseConnection; @H_404_16@public @H_404_16@@H_404_16@class @H_404_16@DealOracle { @H_404_16@private @H_404_16@static DataBaseConnection cFactory = @H_404_16@new DataBaseConnection(); @H_404_16@static ResultSet rs = @H_404_16@null; @H_404_16@static Connection connection1 = @H_404_16@null; @H_404_16@static Connection connection2 = @H_404_16@null; @H_404_16@static Statement statement = @H_404_16@null; @H_404_16@public @H_404_16@static @H_404_16@void @H_404_16@copyIndex(){ @H_404_16@// 1.查询出建索引语句 String sql = @H_404_16@"SELECT DBMS_MetaDATA.GET_DDL('INDEX',u.index_name) AS id FROM USER_INDEXES u"; @H_404_16@try { @H_404_16@int count = @H_404_16@0; PreparedStatement pState1 = @H_404_16@null; PreparedStatement pState2 = @H_404_16@null; pState1 = cFactory.createConnection4 ().prepareStatement(sql); ResultSet rs = pState1.executeQuery(); @H_404_16@while(rs.next()){ String str = rs.getString(@H_404_16@"ID"); String newstr = @H_404_16@""; @H_404_16@if(str.contains(@H_404_16@"(DEGREE 0 INSTANCES 0)") == @H_404_16@false){ count++; String substr = str.substring(@H_404_16@0,str.indexOf(@H_404_16@"TABLESPACE")); newstr = substr+@H_404_16@"TABLESPACE \"RUS\""; @H_404_16@//System.out.println("str:"+str); System.out.println(@H_404_16@"newstr:"+newstr); System.out.println(@H_404_16@"count:"+count); @H_404_16@try { pState2 = cFactory.createConnection3().prepareStatement(newstr); pState2.executeQuery(newstr); pState2.close(); System.out.println(@H_404_16@"新建索引成功"); } @H_404_16@catch (sqlSyntaxErrorException e) { e.printStackTrace(); } @H_404_16@catch (sqlException e) { e.printStackTrace(); } } } pState1.close(); } @H_404_16@catch (sqlException e) { @H_404_16@// TODO Auto-generated catch block e.printStackTrace(); } @H_404_16@finally { cFactory.releaseConnection4(); cFactory.releaseConnection3(); } } @H_404_16@public @H_404_16@static @H_404_16@void @H_404_16@copySequence(){ @H_404_16@// 1.查询出建序列语句 String sql = @H_404_16@"SELECT DBMS_MetaDATA.GET_DDL('SEQUENCE',u.sequence_name) AS id FROM USER_SEQUENCES u"; @H_404_16@try { @H_404_16@int count = @H_404_16@0; PreparedStatement pState1 = @H_404_16@null; PreparedStatement pState2 = @H_404_16@null; pState1 = cFactory.createConnection4 ().prepareStatement(sql); ResultSet rs = pState1.executeQuery(); @H_404_16@while(rs.next()){ String str = rs.getString(@H_404_16@"ID"); count++; System.out.println(@H_404_16@"sequence:"+str); System.out.println(@H_404_16@"count:"+count); @H_404_16@try { pState2 = cFactory.createConnection3().prepareStatement(str); pState2.executeQuery(str); pState2.close(); System.out.println(@H_404_16@"新建序列成功"); } @H_404_16@catch (sqlSyntaxErrorException e) { e.printStackTrace(); } @H_404_16@catch (sqlException e) { e.printStackTrace(); } } pState1.close(); } @H_404_16@catch (sqlException e) { @H_404_16@// TODO Auto-generated catch block e.printStackTrace(); } @H_404_16@finally { cFactory.releaseConnection4(); cFactory.releaseConnection3(); } } @H_404_16@public @H_404_16@static @H_404_16@void @H_404_16@copyView(){ @H_404_16@// 1.查询出建VIEW语句 String sql = @H_404_16@"SELECT DBMS_MetaDATA.GET_DDL('VIEW',u.view_name) AS id FROM USER_VIEWS u"; @H_404_16@try { @H_404_16@int count = @H_404_16@0; PreparedStatement pState1 = @H_404_16@null; PreparedStatement pState2 = @H_404_16@null; pState1 = cFactory.createConnection4 ().prepareStatement(sql); ResultSet rs = pState1.executeQuery(); @H_404_16@while(rs.next()){ String str = rs.getString(@H_404_16@"ID"); count++; System.out.println(@H_404_16@"view:"+str); System.out.println(@H_404_16@"count:"+count); @H_404_16@try { pState2 = cFactory.createConnection3().prepareStatement(str); pState2.executeQuery(str); pState2.close(); System.out.println(@H_404_16@"新建view成功"); } @H_404_16@catch (sqlSyntaxErrorException e) { e.printStackTrace(); } @H_404_16@catch (sqlException e) { e.printStackTrace(); } } pState1.close(); } @H_404_16@catch (sqlException e) { @H_404_16@// TODO Auto-generated catch block e.printStackTrace(); } @H_404_16@finally { cFactory.releaseConnection4(); cFactory.releaseConnection3(); } } @H_404_16@// 此方法有问题,不行。不过可以先通过下面的语句查询出触发器,手动创建。 @H_404_16@public @H_404_16@static @H_404_16@void @H_404_16@copyTriger(){ @H_404_16@// 1.查询出建触发器语句 String sql = @H_404_16@"SELECT DBMS_MetaDATA.GET_DDL('TRIGGER',u.trigger_name) AS id FROM USER_TRIGGERS u"; @H_404_16@try { @H_404_16@int count = @H_404_16@0; PreparedStatement pState1 = @H_404_16@null; PreparedStatement pState2 = @H_404_16@null; pState1 = cFactory.createConnection4 ().prepareStatement(sql); ResultSet rs = pState1.executeQuery(); @H_404_16@while(rs.next()){ String str = rs.getString(@H_404_16@"ID"); String newstr1 = @H_404_16@""; String newstr2 = @H_404_16@""; System.out.println(@H_404_16@"str:"+str); newstr1 = str.substring(@H_404_16@0,str.indexOf(@H_404_16@"ALTER")); newstr2 = str.substring(str.indexOf(@H_404_16@"ALTER")); count++; System.out.println(@H_404_16@"trigger1:"+newstr1); System.out.println(@H_404_16@"trigger2:"+newstr2); System.out.println(@H_404_16@"count:"+count); @H_404_16@try { pState2 = cFactory.createConnection3().prepareCall(newstr1); pState2.executeUpdate(); @H_404_16@//pState2 = cFactory.createConnection3().prepareStatement(newstr2); @H_404_16@//pState2.execute(); pState2.close(); System.out.println(@H_404_16@"新建触发器成功"); } @H_404_16@catch (sqlSyntaxErrorException e) { e.printStackTrace(); } @H_404_16@catch (sqlException e) { e.printStackTrace(); } } pState1.close(); } @H_404_16@catch (sqlException e) { @H_404_16@// TODO Auto-generated catch block e.printStackTrace(); } @H_404_16@finally { cFactory.releaseConnection4(); cFactory.releaseConnection3(); } } @H_404_16@public @H_404_16@static @H_404_16@void @H_404_16@copyFunction(){ } @H_404_16@public @H_404_16@static @H_404_16@void @H_404_16@main(String[] args){ copyView(); } }
2. 处理结果
2.1. 创建索引
索引创建成功
如果之前已经有这个索引,提示重名。
2.2. 创建序列
序列创建成功
2.3. 创建视图
视图创建成功
3. 待解问题
java 处理触发器这块还没有弄清楚,望后续有时间了解下这方面的方法。