Java获取mysql数据库元数据
修改后的版本:
package com.genratesql.util; import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map; import com.genratesql.pojo.Field; public class DatabaseMetaUtil { private final static String DATABASE_NAME = "generatesql"; private DatabaseMetaData dbMetaData = null;
private Connection con = null; public DatabaseMetaUtil() {
this.getDatabaseMetaData();
} private void getDatabaseMetaData() {
try {
if (dbMetaData == null) {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/" + DATABASE_NAME;
String user = "root";
String password = "root";
con = DriverManager.getConnection(url, user, password);
dbMetaData = con.getMetaData();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得数据库的一些相关信息
*/
public void getDataBaseInformations() {
try {
System.out.println("数据库已知的用户: " + dbMetaData.getUserName());
System.out.println("数据库的系统函数的逗号分隔列表: " + dbMetaData.getSystemFunctions());
System.out.println("数据库的时间和日期函数的逗号分隔列表: " + dbMetaData.getTimeDateFunctions());
System.out.println("数据库的字符串函数的逗号分隔列表: " + dbMetaData.getStringFunctions());
System.out.println("数据库供应商用于 'schema' 的首选术语: " + dbMetaData.getSchemaTerm());
System.out.println("数据库URL: " + dbMetaData.getURL());
System.out.println("是否允许只读:" + dbMetaData.isReadOnly());
System.out.println("数据库的产品名称:" + dbMetaData.getDatabaseProductName());
System.out.println("数据库的版本:" + dbMetaData.getDatabaseProductVersion());
System.out.println("驱动程序的名称:" + dbMetaData.getDriverName());
System.out.println("驱动程序的版本:" + dbMetaData.getDriverVersion()); System.out.println();
System.out.println("数据库中使用的表类型");
ResultSet rs = dbMetaData.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
System.out.println();
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得该用户下面的所有表
*/
public void getAllTableList(String schemaName) {
try {
// table type. Typical types are "TABLE", "VIEW", "SYSTEM TABLE",
// "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
String[] types = { "TABLE" };
ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME"); // 表名
String tableType = rs.getString("TABLE_TYPE"); // 表类型
String remarks = rs.getString("REMARKS"); // 表备注
System.out.println(tableName + "-" + tableType + "-" + remarks);
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得该用户下面的所有视图
*/
public void getAllViewList(String schemaName) {
try {
String[] types = { "VIEW" };
ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);
while (rs.next()) {
String viewName = rs.getString("TABLE_NAME"); // 视图名
String viewType = rs.getString("TABLE_TYPE"); // 视图类型
String remarks = rs.getString("REMARKS"); // 视图备注
System.out.println(viewName + "-" + viewType + "-" + remarks);
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得数据库中所有方案名称
*/
public void getAllSchemas() {
try {
ResultSet rs = dbMetaData.getSchemas();
while (rs.next()) {
String tableSchem = rs.getString("TABLE_SCHEM");
System.out.println(tableSchem);
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 根据指定用户名与表名获取其中列字段名
*
* @param schemaName
* @param tableName
* @return
*/
public Map<String, Field> getColumnNameBySchemaNameAndTableName(String schemaName, String tableName) {
Map<String, Field> columns = null;
try {
columns = new HashMap<>();
ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");
while (rs.next()) {
String columnName = rs.getString("COLUMN_NAME");// 列名
String dataTypeName = rs.getString("TYPE_NAME");// java.sql.Types类型名称
int columnSize = rs.getInt("COLUMN_SIZE");// 列大小
Field field = new Field(columnName, dataTypeName, columnSize);
columns.put(columnName, field);
}
} catch (SQLException e) {
e.printStackTrace();
}
return columns;
} /**
* 获得一个表的主键信息
*/
public String getPrimaryKeyBySchemaNameAndTableName(String schemaName, String tableName) {
String primaryKey = null;
try {
ResultSet rs = dbMetaData.getPrimaryKeys(null, schemaName, tableName);
while (rs.next()) {
primaryKey = rs.getString("COLUMN_NAME");
}
} catch (SQLException e) {
e.printStackTrace();
}
return primaryKey;
} /**
* 获得表或视图中的所有列信息
*/
public void getTableColumns(String schemaName, String tableName) { try { ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");
while (rs.next()) {
String tableCat = rs.getString("TABLE_CAT");// 表目录(可能为空)
String tableSchemaName = rs.getString("TABLE_SCHEM");// 表的架构(可能为空)
String tableName_ = rs.getString("TABLE_NAME");// 表名
String columnName = rs.getString("COLUMN_NAME");// 列名
int dataType = rs.getInt("DATA_TYPE"); // 对应的java.sql.Types类型
String dataTypeName = rs.getString("TYPE_NAME");// java.sql.Types类型
// 名称
int columnSize = rs.getInt("COLUMN_SIZE");// 列大小
int decimalDigits = rs.getInt("DECIMAL_DIGITS");// 小数位数
int numPrecRadix = rs.getInt("NUM_PREC_RADIX");// 基数(通常是10或2)
int nullAble = rs.getInt("NULLABLE");// 是否允许为null
String remarks = rs.getString("REMARKS");// 列描述
String columnDef = rs.getString("COLUMN_DEF");// 默认值
int sqlDataType = rs.getInt("SQL_DATA_TYPE");// sql数据类型
int sqlDatetimeSub = rs.getInt("SQL_DATETIME_SUB"); // SQL日期时间分?
int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH"); // char类型的列中的最大字节数
int ordinalPosition = rs.getInt("ORDINAL_POSITION"); // 表中列的索引(从1开始) /**
* ISO规则用来确定某一列的为空性。 是---如果该参数可以包括空值; 无---如果参数不能包含空值
* 空字符串---如果参数为空性是未知的
*/
String isNullAble = rs.getString("IS_NULLABLE"); /**
* 指示此列是否是自动递增 是---如果该列是自动递增 无---如果不是自动递增列 空字串---如果不能确定它是否
* 列是自动递增的参数是未知
*/
String isAutoincrement = rs.getString("IS_AUTOINCREMENT"); System.out.println(tableCat + "-" + tableSchemaName + "-" + tableName_ + "-" + columnName + "-"
+ dataType + "-" + dataTypeName + "-" + columnSize + "-" + decimalDigits + "-" + numPrecRadix
+ "-" + nullAble + "-" + remarks + "-" + columnDef + "-" + sqlDataType + "-" + sqlDatetimeSub
+ charOctetLength + "-" + ordinalPosition + "-" + isNullAble + "-" + isAutoincrement + "-");
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得一个表的索引信息
*/
public void getIndexInfo(String schemaName, String tableName) {
try {
ResultSet rs = dbMetaData.getIndexInfo(null, schemaName, tableName, true, true);
while (rs.next()) {
boolean nonUnique = rs.getBoolean("NON_UNIQUE");// 非唯一索引(Can
// index values
// be
// non-unique.
// false when
// TYPE is
// tableIndexStatistic
// )
String indexQualifier = rs.getString("INDEX_QUALIFIER");// 索引目录(可能为空)
String indexName = rs.getString("INDEX_NAME");// 索引的名称
short type = rs.getShort("TYPE");// 索引类型
short ordinalPosition = rs.getShort("ORDINAL_POSITION");// 在索引列顺序号
String columnName = rs.getString("COLUMN_NAME");// 列名
String ascOrDesc = rs.getString("ASC_OR_DESC");// 列排序顺序:升序还是降序
int cardinality = rs.getInt("CARDINALITY"); // 基数
System.out.println(nonUnique + "-" + indexQualifier + "-" + indexName + "-" + type + "-"
+ ordinalPosition + "-" + columnName + "-" + ascOrDesc + "-" + cardinality);
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得一个表的主键信息
*/
public void getAllPrimaryKeys(String schemaName, String tableName) {
try {
ResultSet rs = dbMetaData.getPrimaryKeys(null, schemaName, tableName);
while (rs.next()) {
String columnName = rs.getString("COLUMN_NAME");// 列名
short keySeq = rs.getShort("KEY_SEQ");// 序列号(主键内值1表示第一列的主键,值2代表主键内的第二列)
String pkName = rs.getString("PK_NAME"); // 主键名称
System.out.println(columnName + "-" + keySeq + "-" + pkName);
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得一个表的外键信息
*/
public void getAllExportedKeys(String schemaName, String tableName) { try {
ResultSet rs = dbMetaData.getExportedKeys(null, schemaName, tableName);
while (rs.next()) {
String pkTableCat = rs.getString("PKTABLE_CAT");// 主键表的目录(可能为空)
String pkTableSchem = rs.getString("PKTABLE_SCHEM");// 主键表的架构(可能为空)
String pkTableName = rs.getString("PKTABLE_NAME");// 主键表名
String pkColumnName = rs.getString("PKCOLUMN_NAME");// 主键列名
String fkTableCat = rs.getString("FKTABLE_CAT");// 外键的表的目录(可能为空)出口(可能为null)
String fkTableSchem = rs.getString("FKTABLE_SCHEM");// 外键表的架构(可能为空)出口(可能为空)
String fkTableName = rs.getString("FKTABLE_NAME");// 外键表名
String fkColumnName = rs.getString("FKCOLUMN_NAME"); // 外键列名
short keySeq = rs.getShort("KEY_SEQ");// 序列号(外键内值1表示第一列的外键,值2代表在第二列的外键)。 /**
* hat happens to foreign key when primary is updated:
* importedNoAction - do not allow update of primary key if it
* has been imported importedKeyCascade - change imported key to
* agree with primary key update importedKeySetNull - change
* imported key to NULL if its primary key has been updated
* importedKeySetDefault - change imported key to default values
* if its primary key has been updated importedKeyRestrict -
* same as importedKeyNoAction (for ODBC 2.x compatibility)
*/
short updateRule = rs.getShort("UPDATE_RULE"); /**
* What happens to the foreign key when primary is deleted.
* importedKeyNoAction - do not allow delete of primary key if
* it has been imported importedKeyCascade - delete rows that
* import a deleted key importedKeySetNull - change imported key
* to NULL if its primary key has been deleted
* importedKeyRestrict - same as importedKeyNoAction (for ODBC
* 2.x compatibility) importedKeySetDefault - change imported
* key to default if its primary key has been deleted
*/
short delRule = rs.getShort("DELETE_RULE");
String fkName = rs.getString("FK_NAME");// 外键的名称(可能为空)
String pkName = rs.getString("PK_NAME");// 主键的名称(可能为空) /**
* can the evaluation of foreign key constraints be deferred
* until commit importedKeyInitiallyDeferred - see SQL92 for
* definition importedKeyInitiallyImmediate - see SQL92 for
* definition importedKeyNotDeferrable - see SQL92 for
* definition
*/
short deferRability = rs.getShort("DEFERRABILITY"); System.out.println(pkTableCat + "-" + pkTableSchem + "-" + pkTableName + "-" + pkColumnName + "-"
+ fkTableCat + "-" + fkTableSchem + "-" + fkTableName + "-" + fkColumnName + "-" + keySeq + "-"
+ updateRule + "-" + delRule + "-" + fkName + "-" + pkName + "-" + deferRability);
}
} catch (SQLException e) {
e.printStackTrace();
}
} public void colseCon() {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
DatabaseMetaUtil metaData = new DatabaseMetaUtil();
// metaData.getDataBaseInformations();
// metaData.getAllTableList(null);
// metaData.getAllViewList(null);
// metaData.getAllSchemas();
// metaData.getTableColumns("root", "gen_person");
// System.out.println("=======================");
// metaData.getIndexInfo("root", "gen_person");
// System.out.println("=======================");
metaData.getAllPrimaryKeys("root", "gen_person");
// System.out.println("=======================");
// metaData.getAllExportedKeys("root", "gen_person");
// System.out.println("=======================");
// Map<String, Field> columnNameBySchemaNameAndTableName =
// metaData.getColumnNameBySchemaNameAndTableName("root", "gen_person");
// Set<Entry<String, Field>> entrySet =
// columnNameBySchemaNameAndTableName.entrySet();
// for (Entry<String, Field> entry : entrySet) {
// System.out.println("key:" + entry.getKey());
// System.out.println("value:" + entry.getValue().toString());
// }
} }
新版本使用到的Field类:
package com.genratesql.pojo; public class Field {
private String fieldName;
private String fieldType;
private Integer fieldLength; public Field() {
} public Field(String fieldName, String fieldType, Integer fieldLength) {
this.fieldName = fieldName;
this.fieldType = fieldType;
this.fieldLength = fieldLength;
} public String getFieldName() {
return fieldName;
} public void setFieldName(String fieldName) {
this.fieldName = fieldName;
} public String getFieldType() {
return fieldType;
} public void setFieldType(String fieldType) {
this.fieldType = fieldType;
} public Integer getFieldLength() {
return fieldLength;
} public void setFieldLength(Integer fieldLength) {
this.fieldLength = fieldLength;
} @Override
public String toString() {
return "Field [fieldName=" + fieldName + ", fieldType=" + fieldType + ", fieldLength=" + fieldLength + "]";
} }
====
原始版本:
package com.zsw.test; import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException; public class DatabaseMetaDateApplication { private DatabaseMetaData dbMetaData = null;
private Connection con = null; public DatabaseMetaDateApplication() {
this.getDatabaseMetaData();
} private void getDatabaseMetaData() {
try {
if (dbMetaData == null) {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/creation_cms";
String user = "root";
String password = "root";
con = DriverManager.getConnection(url, user, password);
dbMetaData = con.getMetaData();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得数据库的一些相关信息
*/
public void getDataBaseInformations() {
try {
System.out.println("数据库已知的用户: "+ dbMetaData.getUserName());
System.out.println("数据库的系统函数的逗号分隔列表: "+ dbMetaData.getSystemFunctions());
System.out.println("数据库的时间和日期函数的逗号分隔列表: "+ dbMetaData.getTimeDateFunctions());
System.out.println("数据库的字符串函数的逗号分隔列表: "+ dbMetaData.getStringFunctions());
System.out.println("数据库供应商用于 'schema' 的首选术语: "+ dbMetaData.getSchemaTerm());
System.out.println("数据库URL: " + dbMetaData.getURL());
System.out.println("是否允许只读:" + dbMetaData.isReadOnly());
System.out.println("数据库的产品名称:" + dbMetaData.getDatabaseProductName());
System.out.println("数据库的版本:" + dbMetaData.getDatabaseProductVersion());
System.out.println("驱动程序的名称:" + dbMetaData.getDriverName());
System.out.println("驱动程序的版本:" + dbMetaData.getDriverVersion()); System.out.println();
System.out.println("数据库中使用的表类型");
ResultSet rs = dbMetaData.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
System.out.println();
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得该用户下面的所有表
*/
public void getAllTableList(String schemaName) {
try {
// table type. Typical types are "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
String[] types = { "TABLE" };
ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME"); //表名
String tableType = rs.getString("TABLE_TYPE"); //表类型
String remarks = rs.getString("REMARKS"); //表备注
System.out.println(tableName + "-" + tableType + "-" + remarks);
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得该用户下面的所有视图
*/
public void getAllViewList(String schemaName) {
try{
String[] types = { "VIEW" };
ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);
while (rs.next()){
String viewName = rs.getString("TABLE_NAME"); //视图名
String viewType = rs.getString("TABLE_TYPE"); //视图类型
String remarks = rs.getString("REMARKS"); //视图备注
System.out.println(viewName + "-" + viewType + "-" + remarks);
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 获得数据库中所有方案名称
*/
public void getAllSchemas(){
try{
ResultSet rs = dbMetaData.getSchemas();
while (rs.next()){
String tableSchem = rs.getString("TABLE_SCHEM");
System.out.println(tableSchem);
}
} catch (SQLException e){
e.printStackTrace();
}
} /**
* 获得表或视图中的所有列信息
*/
public void getTableColumns(String schemaName, String tableName) { try{ ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");
while (rs.next()){
String tableCat = rs.getString("TABLE_CAT");//表目录(可能为空)
String tableSchemaName = rs.getString("TABLE_SCHEM");//表的架构(可能为空)
String tableName_ = rs.getString("TABLE_NAME");//表名
String columnName = rs.getString("COLUMN_NAME");//列名
int dataType = rs.getInt("DATA_TYPE"); //对应的java.sql.Types类型
String dataTypeName = rs.getString("TYPE_NAME");//java.sql.Types类型 名称
int columnSize = rs.getInt("COLUMN_SIZE");//列大小
int decimalDigits = rs.getInt("DECIMAL_DIGITS");//小数位数
int numPrecRadix = rs.getInt("NUM_PREC_RADIX");//基数(通常是10或2)
int nullAble = rs.getInt("NULLABLE");//是否允许为null
String remarks = rs.getString("REMARKS");//列描述
String columnDef = rs.getString("COLUMN_DEF");//默认值
int sqlDataType = rs.getInt("SQL_DATA_TYPE");//sql数据类型
int sqlDatetimeSub = rs.getInt("SQL_DATETIME_SUB"); //SQL日期时间分?
int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH"); //char类型的列中的最大字节数
int ordinalPosition = rs.getInt("ORDINAL_POSITION"); //表中列的索引(从1开始) /**
* ISO规则用来确定某一列的为空性。
* 是---如果该参数可以包括空值;
* 无---如果参数不能包含空值
* 空字符串---如果参数为空性是未知的
*/
String isNullAble = rs.getString("IS_NULLABLE"); /**
* 指示此列是否是自动递增
* 是---如果该列是自动递增
* 无---如果不是自动递增列
* 空字串---如果不能确定它是否
* 列是自动递增的参数是未知
*/
String isAutoincrement = rs.getString("IS_AUTOINCREMENT"); System.out.println(tableCat + "-" + tableSchemaName + "-" + tableName_ + "-" + columnName + "-"
+ dataType + "-" + dataTypeName + "-" + columnSize + "-" + decimalDigits + "-" + numPrecRadix
+ "-" + nullAble + "-" + remarks + "-" + columnDef + "-" + sqlDataType + "-" + sqlDatetimeSub
+ charOctetLength + "-" + ordinalPosition + "-" + isNullAble + "-" + isAutoincrement + "-");
}
} catch (SQLException e){
e.printStackTrace();
}
} /**
* 获得一个表的索引信息
*/
public void getIndexInfo(String schemaName, String tableName) {
try{
ResultSet rs = dbMetaData.getIndexInfo(null, schemaName, tableName, true, true);
while (rs.next()){
boolean nonUnique = rs.getBoolean("NON_UNIQUE");//非唯一索引(Can index values be non-unique. false when TYPE is tableIndexStatistic )
String indexQualifier = rs.getString("INDEX_QUALIFIER");//索引目录(可能为空)
String indexName = rs.getString("INDEX_NAME");//索引的名称
short type = rs.getShort("TYPE");//索引类型
short ordinalPosition = rs.getShort("ORDINAL_POSITION");//在索引列顺序号
String columnName = rs.getString("COLUMN_NAME");//列名
String ascOrDesc = rs.getString("ASC_OR_DESC");//列排序顺序:升序还是降序
int cardinality = rs.getInt("CARDINALITY"); //基数
System.out.println(nonUnique + "-" + indexQualifier + "-" + indexName + "-" + type + "-" + ordinalPosition + "-" + columnName + "-" + ascOrDesc + "-" + cardinality);
}
} catch (SQLException e){
e.printStackTrace();
}
} /**
* 获得一个表的主键信息
*/
public void getAllPrimaryKeys(String schemaName, String tableName) {
try{
ResultSet rs = dbMetaData.getPrimaryKeys(null, schemaName, tableName);
while (rs.next()){
String columnName = rs.getString("COLUMN_NAME");//列名
short keySeq = rs.getShort("KEY_SEQ");//序列号(主键内值1表示第一列的主键,值2代表主键内的第二列)
String pkName = rs.getString("PK_NAME"); //主键名称
System.out.println(columnName + "-" + keySeq + "-" + pkName);
}
}catch (SQLException e){
e.printStackTrace();
}
} /**
* 获得一个表的外键信息
*/
public void getAllExportedKeys(String schemaName, String tableName) { try{
ResultSet rs = dbMetaData.getExportedKeys(null, schemaName, tableName);
while (rs.next()){
String pkTableCat = rs.getString("PKTABLE_CAT");//主键表的目录(可能为空)
String pkTableSchem = rs.getString("PKTABLE_SCHEM");//主键表的架构(可能为空)
String pkTableName = rs.getString("PKTABLE_NAME");//主键表名
String pkColumnName = rs.getString("PKCOLUMN_NAME");//主键列名
String fkTableCat = rs.getString("FKTABLE_CAT");//外键的表的目录(可能为空)出口(可能为null)
String fkTableSchem = rs.getString("FKTABLE_SCHEM");//外键表的架构(可能为空)出口(可能为空)
String fkTableName = rs.getString("FKTABLE_NAME");//外键表名
String fkColumnName = rs.getString("FKCOLUMN_NAME"); //外键列名
short keySeq = rs.getShort("KEY_SEQ");//序列号(外键内值1表示第一列的外键,值2代表在第二列的外键)。 /**
* hat happens to foreign key when primary is updated:
* importedNoAction - do not allow update of primary key if it has been imported
* importedKeyCascade - change imported key to agree with primary key update
* importedKeySetNull - change imported key to NULL if its primary key has been updated
* importedKeySetDefault - change imported key to default values if its primary key has been updated
* importedKeyRestrict - same as importedKeyNoAction (for ODBC 2.x compatibility)
*/
short updateRule = rs.getShort("UPDATE_RULE"); /**
* What happens to the foreign key when primary is deleted.
* importedKeyNoAction - do not allow delete of primary key if it has been imported
* importedKeyCascade - delete rows that import a deleted key
* importedKeySetNull - change imported key to NULL if its primary key has been deleted
* importedKeyRestrict - same as importedKeyNoAction (for ODBC 2.x compatibility)
* importedKeySetDefault - change imported key to default if its primary key has been deleted
*/
short delRule = rs.getShort("DELETE_RULE");
String fkName = rs.getString("FK_NAME");//外键的名称(可能为空)
String pkName = rs.getString("PK_NAME");//主键的名称(可能为空) /**
* can the evaluation of foreign key constraints be deferred until commit
* importedKeyInitiallyDeferred - see SQL92 for definition
* importedKeyInitiallyImmediate - see SQL92 for definition
* importedKeyNotDeferrable - see SQL92 for definition
*/
short deferRability = rs.getShort("DEFERRABILITY"); System.out.println(pkTableCat + "-" + pkTableSchem + "-" + pkTableName + "-" + pkColumnName + "-"
+ fkTableCat + "-" + fkTableSchem + "-" + fkTableName + "-" + fkColumnName + "-" + keySeq + "-"
+ updateRule + "-" + delRule + "-" + fkName + "-" + pkName + "-" + deferRability);
}
} catch (SQLException e){
e.printStackTrace();
}
} public void colseCon() {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
DatabaseMetaDateApplication metaData = new DatabaseMetaDateApplication();
// metaData.getDataBaseInformations();
// metaData.getAllTableList(null);
// metaData.getAllViewList(null);
// metaData.getAllSchemas();
// metaData.getTableColumns(null, "zsc_admin");
// metaData.getIndexInfo(null, "zsc_admin");
// metaData.getAllPrimaryKeys(null, "zsc_admin");
metaData.getAllExportedKeys(null, "zsc_admin"); } }
Java获取mysql数据库元数据的更多相关文章
- java获取mysql数据库表、字段、字段类型、字段注释
最近想要写一个根据数据库表结构生成实体.mapper接口.mapping映射文件.service类的简单代码生成工具,所以查阅了一些资料,怎样获取数据库的表.表中字段.字段类型.字段注释等信息. 最后 ...
- java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)
插播一段广告哈:我之前共享了两个自己写的小应用,见这篇博客百度地图开发的两个应用源码共享(Android版),没 想到有人找我来做毕设了,年前交付,时间不是很紧,大概了解了下就接下了,主要用到的就是和 ...
- Java连接MySQL数据库及简单操作代码
1.Java连接MySQL数据库 Java连接MySql需要下载JDBC驱动MySQL-connector-java-5.0.5.zip(举例,现有新版本).然后将其解压缩到任一目录.我是解压到D盘, ...
- Java连接MySQL数据库增删改查通用方法
版权声明:本文为博主原创文章,未经博主允许不得转载. Java连接MySQL数据库增删改查通用方法 运行环境:eclipse+MySQL 以前我们Java连接MySQL数据库都是一个数据库写一个类,类 ...
- Java进阶(二十五)Java连接mysql数据库(底层实现)
Java进阶(二十五)Java连接mysql数据库(底层实现) 前言 很长时间没有系统的使用java做项目了.现在需要使用java完成一个实验,其中涉及到java连接数据库.让自己来写,记忆中已无从搜 ...
- Java 连接 MySQL 数据库
最近想学习一下 Java 连接 MySQL 数据库,于是有了这样的一些问题&解决办法. 首先是解决 JDBC(Java Data Base Connectivity)驱动问题,因为默认安装的J ...
- Java连接MySQL数据库及操作
Java操作MySQL数据库,需要驱动mysql-connector-java 来进行操作,去下载对应的jar包 一.导入需要的jar包 我用的是maven对包进行管理,在maven中添加如下内容 ...
- MySQL学习(一)——Java连接MySql数据库
MySQL学习(一)——Java连接MySql数据库 API详解: 获得语句执行 String sql = "Insert into category(cid, cname) values( ...
- Java连接mysql数据库攻略
一. 软件下载 Mysql 下载版本:4.1.11 http://dev.mysql.com/downloads/mysql/4.1.html JDBC驱动 下载版本:3.1.8 http://dev ...
随机推荐
- Django基于正则表达式的URL(2)
Django基于正则表达式的URL(2) 1. 关于正则的说明 url(r'^detail-(\d+)-(\d+).html',views.detail), 当客户端输入 127.0.0.1:8000 ...
- 使用cronolog自动分割apache的日志。
为了方便分析网站的日志,需要将apache的日志按周来分割,网上搜索了下,cronolog可以完成这个功能,实际操作了下,操作过程中参考了这篇文章(http://www.lampbo.org/linu ...
- 关于QueryCache的一次打脸
[背景问题] 前一段时间给一套MySQL数据库加上了监控,运行一段时间后有人反馈监控到的insert,update,delete,select的数量中select的数量有像比 本应该的量少了不少! 我 ...
- Hadoop创建/删除文件夹出错
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...
- 【八】注入框架RoboGuice使用:(Your First Injected Fragment)
上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解 ...
- vim常忘命令
1.复制指定行到当前光标的下一行. #假设当前光标在10行,想把第5行复制到第11行 :5copy. #copy命令有2种简写'co'和't',所以也可以写成下面的格式 :5co. :5t. 参考:h ...
- Atitit 如何设置与安放知识的trap陷阱 知识聚合 rss url聚合工具 以及与trap的对比
Atitit 如何设置与安放知识的trap陷阱 知识聚合 rss url聚合工具 以及与trap的对比 1.1. 安放地点 垂直知识网站csdn cnblogs等特定频道栏目,大牛博客 1 1.2. ...
- spring cloud 路由Zuul的高可用
Zuul的高可用非常关键,因为外部请求到后端微服务的流量都会经过Zuul.故而在生产环境中,我们一般都需要部署高可用的Zuul以避免单点故障. 笔者分两种场景讨论Zuul的高可用. Zuul客户端也注 ...
- 使用sqoop将MySQL数据库中的数据导入Hbase
使用sqoop将MySQL数据库中的数据导入Hbase 前提:安装好 sqoop.hbase. 下载jbdc驱动:mysql-connector-java-5.1.10.jar 将 mysql-con ...
- 最NB的发现 LINUX 下玩teamviewer 命令行设置密码
cd /opt/teamviewer/tv_bin/ [root@666 tv_bin]# ls desktop script teamviewerd TVGuiSlave.32 xdg-utils ...