使用java程序比较mysql表结构
来源:互联网 发布:在家网络医疗咨询 编辑:程序博客网 时间:2024/06/10 16:58
数据库中有两张表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==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;
}
}
- 使用java程序比较mysql表结构
- 使用java程序导入导出mysql表
- java获取mysql表结构
- mysql数据库结构比较工具
- mysql比较数据库结构差异
- java中比较器在结构体中的简单使用
- 比较两个mysql数据库表结构的差异
- 比较两个mysql数据库表结构的差异
- PHP脚本快速比较两个mysql表结构的差异
- JAVA使用JDBC连接,修改MySQL数据库(比较乱)
- JAVA程序内存结构
- Java程序的结构
- JAVA程序内存结构
- JAVA程序的结构
- 【Java】程序基本结构
- JAVA基本结构程序
- mysql使用“.frm”文件恢复表结构
- 如何比较mysql数据库结构的不同?
- lucene构建restful风格的简单搜索引擎服务
- 经纬财富:乐山稳健投资之道,积小胜为大胜
- H-1431-素数回文
- Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)
- 学习php需要了解的点4 - PHP中file_get_contents函数抓取https地址出错的解决方法
- 使用java程序比较mysql表结构
- Cocos2d-x 3.1.1 学习日志11--一Windows下Android环境搭建(一定对你有用的!!)
- Android Handler通信
- 神奇的母函数(二) hdoj 1398 Square Coins 【母函数】
- UVA 10288 Coupons
- ExtJs桌面组件
- Mybatis
- hibernate缓存
- C++文件操作详解(ifstream、ofstream、fstream)