Java对比两个数据库中的表和字段,写个冷门的东西
转载的 来源网络
目前所在的项目组距离下个版本上线已经很近了,就面临了一个问题:开发人员在开发库上根据需要增加数据表、数据字段、或者变更了字段类型或者字段长度等等。
由于时间比较紧迫,导致在开发过程中不可能一一把DDL数据库脚本记录下来,在比较大的项目中,比如我所在项目开发的系统大概包含了800张左右的表,字段上10000个的情况下,人工处理明显不可行,所以我们就得通过程序来判断比对,哪些是我们需要新增加的表,哪些是我们需要新增加的字段,哪些是我们需要修改的字段。
因为我开发的项目是为银行工作的,所以数据量无疑很大,所以这个Java类可以用于几乎大多数情况了,当前情况是正在运行的生产服务器上有个数据库-->生产库,我们开发人员服务器上有个数据库-->开发库,就需要我们将两库差异对比出来,我差不多花了1个小时写了下面几个类,运行一下就可以将差异化的地方写入文件中,便于后续写DDL脚本处理。
首先是一个 Table 类,代表了我们数据库中的一张表,其中存在String类型的表名、和存放若干个各种字段的HashMap()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package test;
import java.util.HashMap;
public class Table {
public String tableName;
public HashMap columns = new HashMap();
public Table(String tableName) {
this.tableName = tableName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public HashMap getColumns() {
return columns;
}
public void setColumns(HashMap columns) {
this.columns = columns;
}
}
|
接着就是一个 Column 类,代表了数据库中的一个字段,其中属性就是字段名、字段类型、字段长度,当然可以根据自己的需求加入更多要素
Java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package test;
public class Column {
public String columnName;
public String dataType;
public int length;
public Column(String columnName, String dataType, int length) {
this.columnName = columnName;
this.dataType = dataType;
this.length = length;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
}
|
其实这个方法完全可以不用上面两个类的,但是为了写起来理解方便,所以就用了,执行效率其实还不错,几百张表几秒钟就跑完了
下面是实现这个需求的主要类,写出来的主要目的就是希望能帮我改进一下,毕竟自己写程序没有太多的设计理念和大局观,希望能者修改修改:
Java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
package test;
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.amarsoft.are.sql.ASResultSet;
import com.amarsoft.are.sql.Transaction;
import com.amarsoft.are.util.DataConvert;
public class CompareTable {
public static StringBuffer[] sb = { new StringBuffer(), new StringBuffer(),
new StringBuffer(), new StringBuffer(), new StringBuffer(),
new StringBuffer() };
public static Transaction getTransaction_product() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.1.1:1621:orcl", "demo1", "demo1");
if (conn != null)System.out.println("数据库加载成功!");
Transaction transaction = new Transaction(conn);
return transaction;
}
public static Transaction getTransaction_develop() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.1.2:1621:orcl", "demo2", "demo2");
if (conn != null)System.out.println("数据库加载成功!");
Transaction transaction = new Transaction(conn);
return transaction;
}
public static void main(String[] args) throws Exception {
compareTables(); // 比较数据库
writeFile(); // 写入文件
}
public static void compareTables() throws Exception {
// 生产数据库连接
Transaction trans_product = getTransaction_product();
Map<</SPAN>String, Table> map_product = getTables(trans_product);
// 开发数据库连接
Transaction trans_develop = getTransaction_develop();
Map<</SPAN>String, Table> map_develop = getTables(trans_develop);
// 遍历开发库Map
for (Iterator iter_table = map_develop.keySet().iterator(); iter_table
.hasNext();) {
String key_table = (String) iter_table.next();
Table table_develop = map_develop.get(key_table);// 获得开发库中的表
Table table_product = map_product.get(key_table);// 尝试从生产库中获得同名表
if (table_product == null) { // 如果获得表为空,说明开发存在,生产不存在
append(table_develop, null, 2);
} else { // 表相同,判断字段、字段类型、字段长度
for (Iterator iter_column = table_develop.columns
.keySet().iterator(); iter_column.hasNext();) {
String key_column = (String) iter_column.next();
Column column_develop = table_develop.columns.get(key_column);// 获得开发库中的列
Column column_product = table_product.columns.get(key_column);// 尝试从生产库中获得同名列
if (column_product == null) {// 如果列名为空,说明开发存在,生产不存在
append(table_develop, column_develop, 4);
} else {// 说明两者都存在
if (!column_develop.dataType.equals(column_product.dataType))// 字段类型不一致
append(table_develop, column_develop, 5);
if (column_develop.length != column_product.length)// 字段长度不一致
append(table_develop, column_develop, 6);
}
}
}
}
// 遍历生产库Map
for (Iterator iter_table = map_product.keySet().iterator(); iter_table
.hasNext();) {
String key_table = (String) iter_table.next();
Table table_product = map_product.get(key_table);// 尝试从生产库中获得同名表
Table table_develop = map_develop.get(key_table);// 获得开发库中的表
if (table_develop == null) { // 如果获得表为空,说明开发存在,生产不存在
append(table_product, null, 1);
} else { // 表相同,判断字段、字段类型、字段长度
for (Iterator iter_column = table_product.columns
.keySet().iterator(); iter_column.hasNext();) {
String key_column = (String) iter_column.next();
Column column_product = table_product.columns.get(key_column);// 获得生产库中的列
Column column_develop = table_develop.columns.get(key_column);// 尝试从开发库中获得同名列
if (column_develop == null) {// 如果列名为空,说明生产存在,开发不存在
append(table_product, column_product, 3);
}
}
}
}
}
public static Map<</SPAN>String, Table> getTables(Transaction transaction)
throws Exception {
String sSql = " select table_name,Column_Name,Data_Type,"
+ " DECODE(DATA_TYPE,'NUMBER',DATA_PRECISION,'VARCHAR2',"
+ " DATA_LENGTH,'VARCHAR',DATA_LENGTH,'CHAR',DATA_LENGTH,0) Length,"
+ " NVL(DATA_SCALE, 0) SCALE,DECODE(NULLABLE, 'N', '1', '0') NULLABLE "
+ " from user_tab_columns where 1=1 Order By table_name,column_name";
ASResultSet rs = transaction.getASResultSet(sSql);
Map<</SPAN>String, Table> map = new HashMap<</SPAN>String, Table>();
String tableName = "";
Table table = null;
while (rs.next()) {
if (!tableName.equals(rs.getString("table_name"))) {// 一张新表
tableName = rs.getString("table_name");
table = new Table(tableName);
Column column = new Column(rs.getString("Column_Name"),
rs.getString("Data_Type"), rs.getInt("Length"));
table.columns.put(column.columnName, column);
map.put(rs.getString("table_name"), table);
} else {// 已存在的表,增加字段
Column column = new Column(rs.getString("Column_Name"),
rs.getString("Data_Type"), rs.getInt("Length"));
table.columns.put(column.columnName, column);
}
}
if (null != rs)
rs.close();
transaction.finalize();
return map;
}
public static void append(Table table, Column column, int flag)
throws Exception {
switch (flag) {
case 1:
System.out.println("1、生产存在,开发不存在的表:" + table.getTableName());// 跳过
sb[0].append(table.getTableName() + "\n");
break;
case 2:
System.out.println("2、生产不存在,开发存在的表:" + table.getTableName());// 需要人工判断脚本
sb[1].append(table.getTableName() + "\n");
break;
case 3:
System.out.println("3、生产存在,开发不存在的字段:" + table.getTableName()
+ " | " + column.getColumnName());// 需人工判断如何处理
sb[2].append(table.getTableName() + " | " + column.getColumnName()
+ "\n");
break;
case 4:
System.out.println("4、生产不存在,开发存在的字段:" + table.getTableName()
+ " | " + column.getColumnName());// 需要人工判断脚本
sb[3].append(table.getTableName() + " | " + column.getColumnName()
+ "\n");
break;
case 5:
System.out.println("5、表和字段都相同,但字段类型不同的内容:" + table.getTableName()
+ " | " + column.getColumnName() + " | "
+ column.getDataType());// 需要人工判断脚本
sb[4].append(table.getTableName() + " | " + column.getColumnName()
+ " | " + column.getDataType() + "\n");
break;
case 6:
System.out.println("6、表和字段、字段类型都相同,但字段长度不同的内容:"
+ table.getTableName() + " | " + column.getColumnName()
+ " | " + column.getLength());// 需要人工判断脚本
sb[5].append(table.getTableName() + " | " + column.getColumnName()
+ " | " + column.getLength() + "\n");
break;
}
}
public static void writeFile() throws Exception {
String[] fileName = { "D://table//生产存在,开发不存在的表.txt",
"D://table//生产不存在,开发存在的表.txt", "D://table//生产存在,开发不存在的字段.txt",
"D://table//生产不存在,开发存在的字段.txt",
"D://table//表和字段都相同,但字段类型不同的内容.txt",
"D://table//表和字段、字段类型都相同,但字段长度不同的内容.txt" };
for (int i = 0; i <</SPAN> fileName.length; i++) {
File file = new File(fileName[i]);
OutputStream os = new FileOutputStream(file);
os.write(sb[i].toString().getBytes());
os.flush();
os.close();
}
}
}
|
尾声:整个程序其实并不复杂,感觉被我写得有些累赘了,希望以后能精简一点吧
Java对比两个数据库中的表和字段,写个冷门的东西的更多相关文章
- 使用sql查询mysql/oracle/sql server/gp数据库中指定表的字段信息(字段名/字段类型/字段长度/是否是主键/是否为空)
1,根据数据库类型拼接不同URL /** * 根据类型不同拼接连接的URL * @param dbType 1:mysql.2:oracle.3:sql server.4:gp * @param ip ...
- 【转】 mysql使用federated引擎实现远程访问数据库(跨网络同时操作两个数据库中的表)
原文转自:http://www.2cto.com/database/201412/358397.html 问题: 这里假设我需要在IP1上的database1上访问IP2的database数据库内的t ...
- WordPress数据库中的表、字段、类型及说明
wp_categories: 用于保存分类相关信息的表.包括了5个字段,分别是: cat_ID – 每个分类唯一的ID号,为一个bigint(20)值,且带有附加属性auto_increment. c ...
- mysql查同个实例两个数据库中的表名差异
select TABLE_NAME from ( select TABLE_NAME ,) as cnt from information_schema.tables where TABLE_SCHE ...
- 获取SQL Server数据库中的表和字段描述
获取所有dbo表的扩展属性: SELECT * FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', default, NULL, ...
- MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同
查询数据库中所有表名select table_name from information_schema.tables where table_schema='数据库名' and table_type= ...
- 两种获取MySql数据库中所有表的主键和外键约束信息的Sql语句
最近在写Rafy底层的一些东西,在数据库方面把MySql数据库集成到里面去,里面有一个需求,需要获取非系统数据库,也就是我们自己建立的数据库中所有表的主键和外键元数据列表. 第一种方法:是网上的方法, ...
- 点评阿里JAVA手册之MySQL数据库 (建表规约、索引规约、SQL语句、ORM映射)
下载原版阿里JAVA开发手册 [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文内容:MySQL数据库 (建表规约.索引规约.SQL语句.ORM映 ...
- 通过jdbc获取数据库中的表结构
通过jdbc获取数据库中的表结构 主键 各个表字段类型及应用生成实体类 1.JDBC中通过MetaData来获取具体的表的相关信息.可以查询数据库中的有哪些表,表有哪些字段,字段的属性等等.Met ...
随机推荐
- Trie图(AC自动机)总结
AC自动机构建完成后,某个节点沿着Fail链向上能从长到短走到自己的所有后缀.一般的,遍历主串进行匹配,就是在Trie图上定向移动的过程. 构造(一遍 BFS) void build_AC() { ; ...
- Shell函数!
1.作用:将命令序列按格式写在一起,可方便重复使用命令序列2.Shell 函数定义格式:[ function ] 函数名(){命令序列[ return x ]}3.调用函数的方法:函数名 [ 参数 1 ...
- 15、python面对对象之类和对象
前言:本文主要介绍python面对对象中的类和对象,包括类和对象的概念.类的定义.类属性.实例属性及实例方法等. 一.类和对象的概念 问题:什么是类?什么是实例对象? 类:是一类事物的抽象概念,不是真 ...
- html5 postMessage 消息传递问题
<script type="text/javascript"> $.fn.extend({ addEvent: function (type, handle, bool ...
- jquery-ajax的用法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 15 JavaScript弹窗(警告框alert、确认框confirm、提示框Promt)
警告框:window.alert().通常用于确认用户可以得到某些信息 <body> <script type="text/javascript" charset ...
- 最优矩阵连乘问题 区间DP
最优矩阵连乘积 Accepted: 10 Total Submit: 18Time Limit: 1000ms Memony Limit: 32768KB Description 在科学计算中经常要计 ...
- 建设基于TensorFlow的深度学习环境
一.使用yum安装git 1.查看系统是否已经安装git git --version 2.yum 安装git yum install git 3.安装成功 git --version 4.进入指定目录 ...
- 第1节 Scala基础语法:3、环境;4、插件
1. Scala编译器安装 1.1. 安装JDK 因为Scala是运行在JVM平台上的,所以安装Scala之前要安装JDK. 1.2. 安装Scala 1.2.1. Windows ...
- [POI 2014]PTA-Little Bird
Description 题库连接 给你 \(n\) 棵树,第 \(i\) 棵树的高度为 \(d_i\).有一只鸟从 1 号树出发,每次飞跃不能超过 \(k\) 的距离.若飞到下一棵树的高度大于等于这一 ...