使用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;
}
}
比较类的源码:
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;
}
}
相关文章推荐
- 关于两种将Java程序转化为.exe程序工具的使用及比较
- 关于三种将Java程序转化为.exe程序工具的使用及比较
- Mysql客户端可连接使用,但是java程序报错
- 【mysql】备份篇2:使用java程序定期备份mysql数据库
- SAE平台中使用java程序连接mysql方法
- JAVA入门.零基础学Java语言 MOOC 第二 第三 第四周 比较 判断 分支 循环 —————— 程序控制结构(顺序,分支,循环)
- SAE平台中使用java程序连接mysql方法
- Java中使用MySQL从安装、配置到实际程序测试详解
- 使用java理解程序逻辑 第三章 选择结构一
- mysql 使用java 程序 批量插入 很慢
- 解决Java程序使用MySQL时返回参数为乱码的示例教程
- 使用 WebSphere Studio Application Developer 开发 Java 程序: 与 VisualAge for Java 进行比较
- 使用递归删除树形结构的所有子节点(java和mysql实现)
- 解决Java程序使用MySQL时返回参数为乱码的示例教程
- java学习09--选择结构-switch练习、if和Switch使用比较
- 使用java程序导入导出mysql表
- Java中使用MySQL从安装、配置到实际程序测试详解
- 关于两种将Java程序转化为.exe程序工具的使用及比较
- SAE平台中使用java程序连接mysql方法
- 翻译:java中怎样使用树结构(How to Use Trees)(三)