3
3
import java .io .FileNotFoundException ;
4
4
import java .io .FileOutputStream ;
5
5
import java .io .IOException ;
6
+ import java .sql .Connection ;
6
7
import java .sql .ResultSet ;
7
8
import java .sql .SQLException ;
8
9
import java .util .ArrayList ;
@@ -31,7 +32,7 @@ public static void main( String[] args ) throws IOException
31
32
32
33
33
34
//使用jar的命令,参数有4个,如果没有4个直接退出,没得商量
34
- if (args .length <4 ){
35
+ if (args .length <6 ){
35
36
log .info ("参数:" );
36
37
log .info ("-n=数据库名称" );
37
38
log .info ("-u=用户名" );
@@ -57,89 +58,44 @@ public static void main( String[] args ) throws IOException
57
58
public static void Oracle (Map <String ,String > map ) throws IOException {
58
59
//默认生成的文件名
59
60
String outFile = map .get ("-d" )+"/数据库表结构(ORACLE).docx" ;
60
-
61
61
//查询表的名称以及一些表需要的信息
62
62
String oracleSql1 = "select ut.table_name as table_name,ut.tablespace_name as engine,ut.buffer_pool as table_collation, uc.table_type as table_type,uc.comments as table_comment,ut.last_analyzed as create_options from user_tables ut,user_tab_comments uc where ut.table_name=uc.table_name" ;
63
-
64
63
String oracleSql2 = "select rownum as ordinal_position,c.nullable as is_nullable,c.data_default as column_default,c.data_type as data_type,c.data_length as character_maximum_length,t.column_name as column_name,t.comments as column_comment from user_col_comments t,user_tab_columns c where c.column_name=t.column_name and c.table_name=t.table_name and t.table_name='" ;
65
-
66
- ResultSet rs = OracleUtils .getResultSet (OracleUtils .getConnnection (map .get ("-u" ), map .get ("-p" )),oracleSql1 );
67
-
68
- log .info ("开始生成文件" );
69
- List <Map <String , String >> list = getTableName (rs );
70
- RowRenderData header = getHeader ();
71
- Map <String ,Object > datas = new HashMap <String , Object >();
72
- datas .put ("title" , "数据结构表(ORACLE)" );
73
- List <Map <String ,Object >> tableList = new ArrayList <Map <String ,Object >>();
74
- int i = 0 ;
75
- for (Map <String , String > str : list ){
76
- log .info (str );
77
- i ++;
78
- String sql = oracleSql2 +str .get ("table_name" )+"'" ;
79
- ResultSet set = OracleUtils .getResultSet (OracleUtils .getConnnection (map .get ("-u" ), map .get ("-p" )),sql );
80
- List <RowRenderData > rowList = getRowRenderData (set );
81
- Map <String ,Object > data = new HashMap <String ,Object >();
82
- data .put ("no" , "" +i );
83
- data .put ("table_comment" ,str .get ("table_comment" )+"" );
84
- data .put ("engine" ,str .get ("engine" )+"" );
85
- data .put ("table_collation" ,str .get ("table_collation" )+"" );
86
- data .put ("table_type" ,str .get ("table_type" )+"" );
87
- data .put ("name" , new TextRenderData (str .get ("table_name" ), POITLStyle .getHeaderStyle ()));
88
- data .put ("table" , new MiniTableRenderData (header , rowList ));
89
- tableList .add (data );
90
- }
91
-
92
- datas .put ("tablelist" , new DocxRenderData (FileUtils .Base64ToFile (outFile ,false ), tableList ));
93
- XWPFTemplate template = XWPFTemplate .compile (FileUtils .Base64ToInputStream ()).render (datas );
94
-
95
- FileOutputStream out = null ;
96
- try {
97
- out = new FileOutputStream (outFile );
98
- log .info ("生成文件结束" );
99
- } catch (FileNotFoundException e ) {
100
- e .printStackTrace ();
101
- log .info ("生成文件失败" );
102
- }finally {
103
- try {
104
- template .write (out );
105
- out .flush ();
106
- out .close ();
107
- template .close ();
108
-
109
- } catch (IOException e ) {
110
- e .printStackTrace ();
111
- }
112
- }
64
+ ResultSet rs = OracleUtils .getResultSet (OracleUtils .getConnnection (String .format ("jdbc:oracle:thin:@%s:%s:ORCL" ,map .get ("h" ),map .get ("p" )),map .get ("-u" ), map .get ("-p" )),oracleSql1 );
65
+ Connection con = OracleUtils .getConnnection (String .format ("jdbc:oracle:thin:@%s:%s:ORCL" ,map .get ("h" ),map .get ("p" )),map .get ("-u" ), map .get ("-p" ));
66
+ createDoc (rs ,oracleSql2 ,map ,outFile ,false ,"Oracle数据库表结构" ,con );
113
67
}
114
68
115
69
public static void MySQL (Map <String ,String > map ) throws IOException {
116
70
//默认生成的文件名
117
71
String outFile = map .get ("-d" )+"/数据库表结构(MySQL).docx" ;
118
-
119
72
//查询表的名称以及一些表需要的信息
120
73
String mysqlSql1 = "SELECT table_name, table_type , ENGINE,table_collation,table_comment, create_options FROM information_schema.TABLES WHERE table_schema='" +map .get ("-n" )+"'" ;
121
-
122
74
//查询表的结构信息
123
75
String mysqlSql2 = "SELECT ordinal_position,column_name,column_type, column_key, extra ,is_nullable, column_default, column_comment,data_type,character_maximum_length "
124
76
+ "FROM information_schema.columns WHERE table_schema='" +map .get ("-n" )+"' and table_name='" ;
77
+ ResultSet rs = SqlUtils .getResultSet (SqlUtils .getConnnection (String .format ("jdbc:mysql://%s:%s" ,map .get ("h" ),map .get ("p" )),map .get ("-u" ), map .get ("-p" )),mysqlSql1 );
78
+ Connection con = SqlUtils .getConnnection (String .format ("jdbc:mysql://%s:%s" ,map .get ("h" ),map .get ("p" )),map .get ("-u" ), map .get ("-p" ));
79
+ createDoc (rs ,mysqlSql2 ,map ,outFile ,true ,"MySQL数据库表结构" ,con );
125
80
126
-
127
- ResultSet rs = SqlUtils .getResultSet (SqlUtils .getConnnection (map .get ("-u" ), map .get ("-p" )),mysqlSql1 );
128
-
81
+ }
82
+
83
+
84
+ private static void createDoc (ResultSet rs ,String sqls ,Map <String ,String > map ,String outFile ,boolean type ,String title ,Connection con ) throws IOException {
129
85
log .info ("开始生成文件" );
130
86
List <Map <String , String >> list = getTableName (rs );
131
87
RowRenderData header = getHeader ();
132
- Map <String ,Object > datas = new HashMap <String , Object >();
133
- datas .put ("title" , "数据结构表(MySQL)" );
88
+ Map <String ,Object > datas = new HashMap <>();
89
+ datas .put ("title" , title );
134
90
List <Map <String ,Object >> tableList = new ArrayList <Map <String ,Object >>();
135
91
int i = 0 ;
136
92
for (Map <String , String > str : list ){
137
93
log .info (str );
138
94
i ++;
139
- String sql = mysqlSql2 +str .get ("table_name" )+"'" ;
140
- ResultSet set = SqlUtils .getResultSet (SqlUtils . getConnnection ( map . get ( "-u" ), map . get ( "-p" )) ,sql );
95
+ String sql = sqls +str .get ("table_name" )+"'" ;
96
+ ResultSet set = SqlUtils .getResultSet (con ,sql );
141
97
List <RowRenderData > rowList = getRowRenderData (set );
142
- Map <String ,Object > data = new HashMap <String , Object >();
98
+ Map <String ,Object > data = new HashMap <>();
143
99
data .put ("no" , "" +i );
144
100
data .put ("table_comment" ,str .get ("table_comment" )+"" );
145
101
data .put ("engine" ,str .get ("engine" )+"" );
@@ -149,10 +105,10 @@ public static void MySQL(Map<String,String> map) throws IOException{
149
105
data .put ("table" , new MiniTableRenderData (header , rowList ));
150
106
tableList .add (data );
151
107
}
152
-
153
- datas .put ("tablelist" , new DocxRenderData (FileUtils .Base64ToFile (outFile ,true ), tableList ));
108
+
109
+ datas .put ("tablelist" , new DocxRenderData (FileUtils .Base64ToFile (outFile ,type ), tableList ));
154
110
XWPFTemplate template = XWPFTemplate .compile (FileUtils .Base64ToInputStream ()).render (datas );
155
-
111
+
156
112
FileOutputStream out = null ;
157
113
try {
158
114
out = new FileOutputStream (outFile );
@@ -166,20 +122,18 @@ public static void MySQL(Map<String,String> map) throws IOException{
166
122
out .flush ();
167
123
out .close ();
168
124
template .close ();
169
-
125
+
170
126
} catch (IOException e ) {
171
127
e .printStackTrace ();
172
- }
128
+ }
173
129
}
174
- }
130
+ }
175
131
176
132
/**
177
133
* 检查缺少的参数
178
- * @param args
179
- * @return
180
134
*/
181
135
private static Map <String , String > Check (String [] args ) {
182
- Map <String , String > map = new HashMap <String , String >();
136
+ Map <String , String > map = new HashMap <>();
183
137
for (String str : args ){
184
138
String [] split = str .split ("=" );
185
139
map .put (split [0 ], split [1 ]);
@@ -201,7 +155,14 @@ private static Map<String, String> Check(String[] args) {
201
155
log .info ("请输入保存文件的目录!" );
202
156
System .exit (0 );
203
157
}
204
-
158
+ if (!map .containsKey ("h" )){
159
+ log .info ("请输入地址!" );
160
+ System .exit (0 );
161
+ }
162
+ if (!map .containsKey ("p" )){
163
+ log .info ("请输入端口" );
164
+ System .exit (0 );
165
+ }
205
166
return map ;
206
167
}
207
168
@@ -224,11 +185,10 @@ private static RowRenderData getHeader(){
224
185
225
186
/**
226
187
* 获取一张表的结构数据
227
- * @param set
228
188
* @return List<RowRenderData>
229
189
*/
230
190
private static List <RowRenderData > getRowRenderData (ResultSet set ) {
231
- List <RowRenderData > result = new ArrayList <RowRenderData >();
191
+ List <RowRenderData > result = new ArrayList <>();
232
192
233
193
try {
234
194
int i = 0 ;
@@ -260,15 +220,14 @@ private static List<RowRenderData> getRowRenderData(ResultSet set) {
260
220
261
221
/**
262
222
* 获取数据库的所有表名及表的信息
263
- * @param rs
264
223
* @return list
265
224
*/
266
225
private static List <Map <String ,String >> getTableName (ResultSet rs ){
267
- List <Map <String ,String >> list = new ArrayList <Map < String , String > >();
226
+ List <Map <String ,String >> list = new ArrayList <>();
268
227
269
228
try {
270
229
while (rs .next ()){
271
- Map <String ,String > result = new HashMap <String , String >();
230
+ Map <String ,String > result = new HashMap <>();
272
231
result .put ("table_name" , rs .getString ("table_name" )+"" );
273
232
result .put ("table_type" , rs .getString ("table_type" )+"" );
274
233
result .put ("engine" , rs .getString ("engine" )+"" );
0 commit comments