java: MySQL Metadata

 

 

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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//https://www.baeldung.com/jdbc-database-metadata
 //https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/DatabaseMetaData.html
 
 /**
  * 获取某数据库中的表列表 涂聚文 Geovin Du geovindu
  * @param
  * @return  TablesMetadata集合
  * */
 public ArrayList<TablesMetadata> selectTablesMetadata()
 {
     ArrayList<TablesMetadata> list=new ArrayList<TablesMetadata>();
     TablesMetadata info=null;
     Connection connection=null;
     CallableStatement cstm =null;
     PreparedStatement pstmt = null;
     try {
         Class.forName(DRIVER_CLASS);
     }
     catch (ClassNotFoundException exception)
     {
         exception.printStackTrace();
     }
     try {
         connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
         // connection.getSchema();
         DatabaseMetaData databaseMetaData=connection.getMetaData();
 
         String productName = databaseMetaData.getDatabaseProductName();
         String productVersion = databaseMetaData.getDatabaseProductVersion();
         String driverName = databaseMetaData.getDriverName();
         String driverVersion = databaseMetaData.getDriverVersion();
         boolean supportsFullOuterJoins = databaseMetaData.supportsFullOuterJoins();
         boolean supportsStoredProcedures = databaseMetaData.supportsStoredProcedures();
         boolean supportsTransactions = databaseMetaData.supportsTransactions();
         boolean supportsBatchUpdates = databaseMetaData.supportsBatchUpdates();
         String userName = databaseMetaData.getUserName();
         /*
             catalog 包含目录名称的 String。对此参数提供 Null 值表示无需使用目录名称。
             schema 包含架构名称模式的 String 值。对此参数提供 Null 值表示无需使用架构名称。
             tableNamePattern 包含表名称模式的 String。
             types 含有要包含的表类型的字符串数组。Null 表示应包含所有表类型。
         * */
         ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[]{"TABLE"});
         while(resultSet.next()) {
             info=new TablesMetadata();
             info.setTableName(resultSet.getString("TABLE_NAME"));
             info.setRemarks(resultSet.getString("REMARKS"));
             info.setTableSchem(resultSet.getString("TABLE_SCHEM"));
             info.setTableType(resultSet.getString("TABLE_TYPE"));
             list.add(info);
         }
         resultSet.close();
         resultSet=null;
         connection.close();
 
     }
     catch (SQLException sqlException)
     {
         sqlException.printStackTrace();
 
     }
     return  list;
 }
 /**
  * 返回某表的列属性列表  涂聚文 Geovin Du geovindu
  * @param TableName
  * @return  返回集合
  * */
 public ArrayList<ColumnsMetadata> selectColumnsMetadata(String TableName)
 {
     ArrayList<ColumnsMetadata> list=new ArrayList<ColumnsMetadata>();
     ColumnsMetadata info=null;
     Connection connection=null;
     CallableStatement cstm =null;
     PreparedStatement pstmt = null;
     try {
         Class.forName(DRIVER_CLASS);
     }
     catch (ClassNotFoundException exception)
     {
         exception.printStackTrace();
     }
     try {
         connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
         // connection.getSchema();
         DatabaseMetaData databaseMetaData=connection.getMetaData();
         ResultSet resultSet = databaseMetaData.getColumns(null,null, TableName, null);
         while(resultSet.next()) {
             info=new ColumnsMetadata();
             info.setColumnName(resultSet.getString("COLUMN_NAME"));
            // String columnName = resultSet.getString("COLUMN_NAME");
 
             info.setClumnSize(resultSet.getString("COLUMN_SIZE"));
             info.setDataType(resultSet.getString("DATA_TYPE"));
             info.setIsNullable(resultSet.getString("IS_NULLABLE"));
             info.setIsAutoincRement(resultSet.getString("IS_AUTOINCREMENT"));
             list.add(info);
         }
        // resultSet.close();
         resultSet=null;
        // connection.close();
 
     }
     catch (SQLException sqlException)
     {
         sqlException.printStackTrace();
 
     }
     return  list;
 }
 /**
  * 主键
  * @param TableName
  * @return
  *
  * */
 public ArrayList<ColumnsPrimaryKeys> selectColumnsPrimaryKeys(String TableName)
 {
     ArrayList<ColumnsPrimaryKeys> list=new ArrayList<ColumnsPrimaryKeys>();
     ColumnsPrimaryKeys info=null;
     Connection connection=null;
     CallableStatement cstm =null;
     PreparedStatement pstmt = null;
     try {
         Class.forName(DRIVER_CLASS);
     }
     catch (ClassNotFoundException exception)
     {
         exception.printStackTrace();
     }
     try {
         connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
         // connection.getSchema();
         DatabaseMetaData databaseMetaData=connection.getMetaData();
         ResultSet resultSet =  databaseMetaData.getPrimaryKeys(null, null, TableName);
         while(resultSet.next()) {
             info=new ColumnsPrimaryKeys();
             info.setColumnName(resultSet.getString("COLUMN_NAME"));
             info.setPkName(resultSet.getString("PK_NAME"));
             list.add(info);
         }
         resultSet.close();
         resultSet=null;
         connection.close();
 
     }
     catch (SQLException sqlException)
     {
         sqlException.printStackTrace();
 
     }
     return  list;
 }
 
 /**
  * 外键
  * @param TableName
  * @return
  *
  * */
 public ArrayList<ColumnsForeignKeys> selectColumnsForeignKeys(String TableName)
 {
     ArrayList<ColumnsForeignKeys> list=new ArrayList<ColumnsForeignKeys>();
     ColumnsForeignKeys info=null;
     Connection connection=null;
     CallableStatement cstm =null;
     PreparedStatement pstmt = null;
     try {
         Class.forName(DRIVER_CLASS);
     }
     catch (ClassNotFoundException exception)
     {
         exception.printStackTrace();
     }
     try {
         connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
         // connection.getSchema();
         DatabaseMetaData databaseMetaData=connection.getMetaData();
         ResultSet resultSet =  databaseMetaData.getImportedKeys(null, null, TableName);
         while(resultSet.next()) {
             info=new ColumnsForeignKeys();
             info.setPkTableName(resultSet.getString("PKTABLE_NAME"));
             info.setPkColumnName(resultSet.getString("PKCOLUMN_NAME"));
             info.setFkTableName(resultSet.getString("FKTABLE_NAME"));
             info.setFkColumnName(resultSet.getString("FKCOLUMN_NAME"));
             list.add(info);
         }
         resultSet.close();
         resultSet=null;
         connection.close();
 
     }
     catch (SQLException sqlException)
     {
         sqlException.printStackTrace();
 
     }
     return  list;
 }
 /**
  *
  *
  * */
 public ArrayList<SchemasMetadata> selectSchemasMetadata()
 {
     ArrayList<SchemasMetadata> list=new ArrayList<SchemasMetadata>();
     SchemasMetadata info=null;
     Connection connection=null;
     CallableStatement cstm =null;
     PreparedStatement pstmt = null;
     try {
         Class.forName(DRIVER_CLASS);
     }
     catch (ClassNotFoundException exception)
     {
         exception.printStackTrace();
     }
     try {
         connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
         // connection.getSchema();
         DatabaseMetaData databaseMetaData=connection.getMetaData();
         ResultSet resultSet = databaseMetaData.getSchemas();
         while (resultSet.next()){
             info=new SchemasMetadata();
             System.out.println("Schem:"+resultSet.getString("TABLE_SCHEM"));
             info.setTableSchem(resultSet.getString("TABLE_SCHEM"));
             info.setTableCatalog(resultSet.getString("TABLE_CATALOG"));
             list.add(info);
         }
        // resultSet.close();
         //resultSet=null;
         connection.close();
 
     }
     catch (SQLException sqlException)
     {
         sqlException.printStackTrace();
 
     }
     return  list;
 }

  

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
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:实体类,连接MySQL
 *
 * 历史版本:  JDK 14.02
 * 数据库:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类
 * 2021-12-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuField.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/数据库名称
 *user=root
 *password=root
 *
 * */
 
 
 
package Geovin.Model;
 
 
 
 
/**
 * 数据库元数据的表属性列表
 * @author geovindu
 * @version 1.0
 *
 * */
public class TablesMetadata {
 
    //TABLE_SCHEM String=>表架构(可能为空)
    private  String TableSchem;
 
    //TABLE_NAME String=>表名
    private  String TableName;
    //REMARKS String => 表格注释
    private  String Remarks;
 
    //TABLE_TYPE String =>表类, 典型的类型有"TABLE","VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY","LOCAL TEMPORARY", "ALIAS", "SYNONYM"
    private String TableType;
 
    //TYPE_CAT String => 目录类型(可能为空)
    private  String TypeCat;
    //TYPE_SCHEM String => 架构类型(可能为空)
    private  String TypeSchem;
    //TYPE_NAME String => 类型名(可能为空)
     private String TypeName;
    //SELF_REFERENCING_COL_NAME String => 类型表的指定“标识符”列的名称(可能为空)
     private String SelfReferncingColName;
    //REF_GENERATION String => 指定如何创建引用_col_name的inself_值。值为"SYSTEM", "USER", "DERIVED"(可能为空)
    private String RefGenearation;
 
 
 
    /**
     *
     *
     * */
    public void setTableName(String tableName) {
        TableName = tableName;
    }
    /**
     *
     *
     * */
    public void setRemarks(String remarks) {
        Remarks = remarks;
    }
    /**
     *
     *
     * */
    public String getTableName() {
        return TableName;
    }
    /**
     *
     *
     * */
    public String getRemarks() {
        return Remarks;
    }
    /**
     *
     *
     * */
    public void setTableSchem(String tableSchem) {
        TableSchem = tableSchem;
    }
    /**
     *
     *
     * */
    public String getTableSchem() {
        return TableSchem;
    }
    /**
     *
     *
     * */
    public void setTypeSchem(String typeSchem) {
        TypeSchem = typeSchem;
    }
    /**
     *
     *
     * */
    public void setTypeName(String typeName) {
        TypeName = typeName;
    }
    /**
     *
     *
     * */
    public void setTypeCat(String typeCat) {
        TypeCat = typeCat;
    }
    /**
     *
     *
     * */
    public void setTableType(String tableType) {
        TableType = tableType;
    }
    /**
     *
     *
     * */
    public void setRefGenearation(String refGenearation) {
        RefGenearation = refGenearation;
    }
    /**
     *
     *
     * */
    public void setSelfReferncingColName(String selfReferncingColName) {
        SelfReferncingColName = selfReferncingColName;
    }
    /**
     *
     *
     * */
    public String getTypeSchem() {
        return TypeSchem;
    }
    /**
     *
     *
     * */
    public String getTypeName() {
        return TypeName;
    }
    /**
     *
     *
     * */
    public String getTypeCat() {
        return TypeCat;
    }
    /**
     *
     *
     * */
    public String getTableType() {
        return TableType;
    }
    /**
     *
     *
     * */
    public String getRefGenearation() {
        return RefGenearation;
    }
    /**
     *
     *
     * */
    public String getSelfReferncingColName() {
        return SelfReferncingColName;
    }
 
 
 
}

  

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
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:实体类,连接MySQL
 *
 * 历史版本:  JDK 14.02
 * 数据库:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类
 * 2021-12-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKind.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/数据库名称
 *user=root
 *password=root
 *
 * */
 
 
package Geovin.Model;
 
/**
 *
 * */
public class SchemasMetadata {
 
    //TABLE_SCHEM
    private  String TableSchem;
    //TABLE_CATALOG
 
    private  String TableCatalog;
    /**
     *
     * */
    public void setTableCatalog(String tableCatalog) {
        TableCatalog = tableCatalog;
    }
    /**
     *
     * */
    public void setTableSchem(String tableSchem) {
        TableSchem = tableSchem;
    }
    /**
     *
     * */
    public String getTableSchem() {
        return TableSchem;
    }
    /**
     *
     * */
    public String getTableCatalog() {
        return TableCatalog;
    }
 
}

  

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
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:实体类,连接MySQL
 *
 * 历史版本:  JDK 14.02
 * 数据库:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类
 * 2021-12-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKind.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/数据库名称
 *user=root
 *password=root
 *
 * */
 
package Geovin.Model;
 
 
/**
 *
 * @author geovindu
 * @version 1.0
 * */
public class ColumnsMetadata {
 
    //COLUMN_NAME
    private String ColumnName;
    //COLUMN_SIZE
    private String ClumnSize;
    //DATA_TYPE
    private String DataType;
    //IS_NULLABLE
    private  String IsNullable;
 
    //IS_AUTOINCREMENT
    private  String IsAutoincRement;
    /**
     *
     *
     * */
    public void setColumnName(String columnName) {
        ColumnName = columnName;
    }
    /**
     *
     *
     * */
    public void setClumnSize(String clumnSize) {
        ClumnSize = clumnSize;
    }
    /**
     *
     *
     * */
    public void setDataType(String dataType) {
        DataType = dataType;
    }
    /**
     *
     *
     * */
    public void setIsNullable(String isNullable) {
        IsNullable = isNullable;
    }
    /**
     *
     *
     * */
    public void setIsAutoincRement(String isAutoincRement) {
        IsAutoincRement = isAutoincRement;
    }
    /**
     *
     *
     * */
    public String getColumnName() {
        return ColumnName;
    }
    /**
     *
     *
     * */
    public String getClumnSize() {
        return ClumnSize;
    }
    /**
     *
     *
     * */
    public String getDataType() {
        return DataType;
    }
    /**
     *
     *
     * */
    public String getIsNullable() {
        return IsNullable;
    }
    /**
     *
     *
     * */
    public String getIsAutoincRement() {
        return IsAutoincRement;
    }
 
 
 
}

  

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
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:实体类,连接MySQL
 *
 * 历史版本:  JDK 14.02
 * 数据库:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类
 * 2021-12-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKind.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/数据库名称
 *user=root
 *password=root
 *
 * */
 
 
package Geovin.Model;
 
/**
 *
 *
 * */
public class ColumnsPrimaryKeys {
 
    //COLUMN_NAME
    private  String ColumnName;
 
 
    //PK_NAME
    private String PkName;
    /**
     *
     *
     * */
    public void setColumnName(String columnName) {
        ColumnName = columnName;
    }
    /**
     *
     *
     * */
    public void setPkName(String pkName) {
        PkName = pkName;
    }
    /**
     *
     *
     * */
    public String getColumnName() {
        return ColumnName;
    }
    /**
     *
     *
     * */
    public String getPkName() {
        return PkName;
    }
 
 
 
}

  

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
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:实体类,连接MySQL
 *
 * 历史版本:  JDK 14.02
 * 数据库:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类
 * 2021-12-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKind.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/数据库名称
 *user=root
 *password=root
 *
 * */
 
 
package Geovin.Model;
/**
 *
 *
 * */
public class ColumnsForeignKeys {
 
    //PKTABLE_NAME
    private  String PkTableName;
 
    //FKTABLE_NAME
 
    private String FkTableName;
 
    //PKCOLUMN_NAME
 
    private  String PkColumnName;
 
 
    //FKCOLUMN_NAME
 
    private  String FkColumnName;
    /**
     *
     *
     * */
    public void setPkTableName(String pkTableName) {
        PkTableName = pkTableName;
    }
    /**
     *
     *
     * */
    public void setPkColumnName(String pkColumnName) {
        PkColumnName = pkColumnName;
    }
    /**
     *
     *
     * */
    public void setFkTableName(String fkTableName) {
        FkTableName = fkTableName;
    }
    /**
     *
     *
     * */
    public void setFkColumnName(String fkColumnName) {
        FkColumnName = fkColumnName;
    }
    /**
     *
     *
     * */
    public String getPkTableName() {
        return PkTableName;
    }
    /**
     *
     *
     * */
    public String getPkColumnName() {
        return PkColumnName;
    }
    /**
     *
     *
     * */
    public String getFkTableName() {
        return FkTableName;
    }
    /**
     *
     *
     * */
    public String getFkColumnName() {
        return FkColumnName;
    }
 
 
}

  

 

posted @   ®Geovin Du Dream Park™  阅读(34)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2017-12-25 postgresql-10.1-3-windows-x64 安装之后,起动pgAdmin 4问题(win10)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示