您的位置:首页 > 编程语言 > Java开发

使用java程序比较mysql表结构

2014-07-24 16:57 786 查看
        数据库中有两张表phone_job和phone_hisjob,phone_job是用来存储最近的职位信息的,当职位信息的发布时间过久,为保证phone_job表的查询速度,会将这些信息转存到phone_hisjob中,两张表应该有相同的表结构,但是由于历史原因,导致了一些数据库中的两张表的表结构不同,由于分布在不同的服务器,不同的数据库中,数量比较大,人工比较太费劲,所以就参照网上的例子,写了下面的类来进行比较,下面给出源码。

比较类的源码:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.Map;

import java.util.Map.Entry;

import com.dataBaseMatch.dto.TableStructure;      //此类是自己定义的,在下面有

public class Test {
{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
e.printStackTrace();
}
}

public void match(String url,String username,String pw){
Connection conn = null;
PreparedStatement ps = null;
ResultSet jobRs = null;
ResultSet hisJobRs = null;
ResultSetMetaData jobRsmd = null;  
ResultSetMetaData hisJobRsmd = null;

Map<String,TableStructure> jobMap = new HashMap<String,TableStructure>();
Map<String,TableStructure> hisJobMap = new HashMap<String,TableStructure>();
String jobSql = "select * from phone_job limit 1";
String hisJobSql = "select * from phone_hisjob limit 1";
System.out.println("正在比较数据库:"+url);
System.out.println("列名"+"
phone_job"+" phone_hisjob");
try {
conn = DriverManager.getConnection("jdbc:mysql://"+url, username, pw);
//获得phone_job的表结构
ps = conn.prepareStatement(jobSql);
jobRs = ps.executeQuery();
jobRsmd = jobRs.getMetaData();

//获取phone_hisjob的表结构
ps = conn.prepareStatement(hisJobSql);
hisJobRs = ps.executeQuery();
hisJobRsmd = hisJobRs.getMetaData();
//获得phone_job的表结构存入jobMap表中
for(int i = 1; i <= jobRsmd.getColumnCount(); i++){
String colName = jobRsmd.getColumnName(i);  
//获得对应列的列名
String colType = jobRsmd.getColumnTypeName(i);
//获得对应列的数据类型
int colLength = jobRsmd.getPrecision(i);
//获得数据长度
TableStructure tableStructure = new TableStructure();
tableStructure.setColName(colName);
tableStructure.setColType(colType);
tableStructure.setColLength(colLength);
jobMap.put(colName, tableStructure);
}

//获得phone_hisjob的表结构存入hisJobMap表中
for(int i = 1; i <= hisJobRsmd.getColumnCount(); i++){
String colName = hisJobRsmd.getColumnName(i);
String colType = hisJobRsmd.getColumnTypeName(i);
int colLength = hisJobRsmd.getPrecision(i);
TableStructure tableStructure = new TableStructure();
tableStructure.setColName(colName);
tableStructure.setColType(colType);
tableStructure.setColLength(colLength);
hisJobMap.put(colName, tableStructure);
}
//以phone_job的表结构为依据比较phone_hisjob的表结构
for(Entry<String,TableStructure> entry :jobMap.entrySet()){
String jobColName = entry.getKey();
TableStructure jobTable = entry.getValue();
TableStructure hisJobTable = hisJobMap.get(jobColName);
boolean isDifference = false;
if(hisJobTable==null){
System.out.println(jobColName+"
"+jobTable.getColType()+"("+jobTable.getColLength()+")"+"
null");
continue;
}
if(!jobTable.getColType().equals(hisJobTable.getColType())){
isDifference = true;
}
if(jobTable.getColLength()!=hisJobTable.getColLength()){
isDifference = true;
}
if(isDifference){
System.out.println(jobColName+"
"+jobTable.getColType()+"("+jobTable.getColLength()+")"+"
"+hisJobTable.getColType()+"("+hisJobTable.getColLength()+")");
}
}
//以phone_hisjob的表结构为依据比较phone_job的表结构,仅处理phone_job中没有的
for(Entry<String,TableStructure> entry :hisJobMap.entrySet()){
String jobColName = entry.getKey();
TableStructure hisJobTable = entry.getValue();
TableStructure jobTable = hisJobMap.get(jobColName);
//由于之前已经以phone_job为标本比较过phone_hisjob,所以在此只需找出在phone_hisjob中存在而在phone_job没有的
if(jobTable==
4000
null){
System.out.println(jobColName+"
null"+" "+hisJobTable.getColType()+"("+hisJobTable.getColLength()+")");
}
}

} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
jobRs.close();
hisJobRs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
 
}
}
//测试
public static void main(String[] args) {
Test test = new Test();
test.match("192.168.8.178:3306/fetchtext", "root", "123456");
}

}

用来存表结构信息的类:

public class TableStructure {

private String colName;
private String colType;
private int colLength;
public String getColName() {
return colName;
}
public void setColName(String colName) {
this.colName = colName;
}
public String getColType() {
return colType;
}
public void setColType(String colType) {
this.colType = colType;
}
public int getColLength() {
return colLength;
}
public void setColLength(int colLength) {
this.colLength = colLength;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql java 数据库