如何使用Java将我在db2数据库中的模式复制到具有完整表结构,列,数据等的oracle数据库中。
我编写java代码,但它没有选择所有表。 我不知道db2数据库中有多少个表,甚至不知道表的名称。我只想存储所有信息,并在oracle中制作相同的模式。
public class automateExport {
static String value;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// ResultSet rs = null;
Connection DB2 = getConnection ();
String sql = "SELECT * FROM SYSCAT.COLUMNS WHERE TABSCHEMA NOT LIKE 'SYS%'";
PreparedStatement mainStmt = DB2.prepareStatement(sql);
ResultSet rs = mainStmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String tableName = null;
StringBuilder sb = new StringBuilder( 1024 );
if ( columnCount > 0 ) {
sb.append( "Create table " ).append( rsmd.getTableName( 1 ) ).append( " ( " );
}
for ( int i = 1; i <= columnCount; i ++ ) {
if ( i > 1 ) sb.append( ", " );
String columnName = rsmd.getColumnLabel( i );
String columnType = rsmd.getColumnTypeName( i );
sb.append( columnName ).append( " " ).append( columnType );
int precision = rsmd.getPrecision( i );
if ( precision != 0 ) {
sb.append( "( " ).append( precision ).append( " )" );
}
} // for columns
sb.append( " ) " );
System.out.println( sb.toString() );
}
private static Connection getConnection() throws ClassNotFoundException, SQLException
{
Class. forName ( "COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver" );
Connection connection =
DriverManager.getConnection("jdbc:db2://localhost:50000/navid","navid","oracle");
return connection;
}
}