比较两个数据库之间的差异
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* 对比两个数据库, 显示不同的表、不同的字段等信息
*/
public class ShowDBDifferences {
private String className;
// 数据库1的数据库url、用户名、密码
private String dbName1;
private String dbUrl1;
private String userName1;
private String password1;
// 数据库2的数据库url、用户名、密码
private String dbName2;
private String dbUrl2;
private String userName2;
private String password2;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getDbName1() {
return dbName1;
}
public void setDbName1(String dbName1) {
this.dbName1 = dbName1;
}
public String getDbUrl1() {
return dbUrl1;
}
public void setDbUrl1(String dbUrl1) {
this.dbUrl1 = dbUrl1;
}
public String getUserName1() {
return userName1;
}
public void setUserName1(String userName1) {
this.userName1 = userName1;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getDbName2() {
return dbName2;
}
public void setDbName2(String dbName2) {
this.dbName2 = dbName2;
}
public String getDbUrl2() {
return dbUrl2;
}
public void setDbUrl2(String dbUrl2) {
this.dbUrl2 = dbUrl2;
}
public String getUserName2() {
return userName2;
}
public void setUserName2(String userName2) {
this.userName2 = userName2;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
private Connection getConnection(String url, String userName,
String password) {
Driver driver = null;
try {
driver = (Driver) Class.forName(className).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
try {
DriverManager.registerDriver(driver);
connection = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
private Map getTableInfo(Connection conn, String tbName) {
// 查询所有表名
// String sql =
// "SELECT CREATOR, NAME FROM SYSIBM.SYSTABLES WHERE CREATOR NOT LIKE '%SYS%' order by name ";
// 查询所有表结构定义的信息
String sql = "select TBCREATOR, TBNAME, NAME, COLTYPE, TYPENAME, LENGTH, SCALE, COLNO, NULLS from sysibm.syscolumns where TBCREATOR NOT LIKE '%SYS%' and TBNAME like '%"
+ tbName + "%' order by TBNAME, COLNO ";
Map map = new HashMap();
PreparedStatement pstm = null;
ResultSet rs = null;
try {
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
String tableName = rs.getString(2);
String colName = rs.getString(3);
String colType = rs.getString(4);
int colLength = rs.getInt(6);
int colScale = rs.getInt(7);
int colNo = rs.getInt(8);
String isAllowNu