基于java获取mysql表结构的方法
class.forname("com.mysql.jdbc.driver").newinstance();
connection conn = drivermanager
.getconnection("jdbc:mysql://localhost:3306/mall?user=root&password=123456");
databasemetadata meta = (databasemetadata) conn.getmetadata();
resultset rs = meta.getcolumns(null, "%", "t_mall_returnorderinfo", "%");
while (rs.next()) {
// table catalog (may be null)
string tablecat = rs.getstring("table_cat");
// table schema (may be null)
string tableschemaname = rs.getstring("table_schem");
// table name
string tablename_ = rs.getstring("table_name");
// column name
string columnname = rs.getstring("column_name");
// sql type from java.sql.types
int datatype = rs.getint("data_type");
// data source dependent type name, for a udt the type name is
// fully qualified
string datatypename = rs.getstring("type_name");
system.out.println(columnname + " " + datatypename);
// table schema (may be null)
int columnsize = rs.getint("column_size");
// the number of fractional digits. null is returned for data
// types where decimal_digits is not applicable.
int decimaldigits = rs.getint("decimal_digits");
// radix (typically either 10 or 2)
int numprecradix = rs.getint("num_prec_radix");
// is null allowed.
int nullable = rs.getint("nullable");
// comment describing column (may be null)
string remarks = rs.getstring("remarks");
// default value for the column, which should be interpreted as
// a string when the value is enclosed in single quotes (may be
// null)
string columndef = rs.getstring("column_def");
//
int sqldatatype = rs.getint("sql_data_type");
//
int sqldatetimesub = rs.getint("sql_datetime_sub");
// for char types the maximum number of bytes in the column
int charoctetlength = rs.getint("char_octet_length");
// index of column in table (starting at 1)
int ordinalposition = rs.getint("ordinal_position");
// iso rules are used to determine the nullability for a column.
// yes --- if the parameter can include nulls;
// no --- if the parameter cannot include nulls
// empty string --- if the nullability for the parameter is
// unknown
string isnullable = rs.getstring("is_nullable");
// indicates whether this column is auto incremented
// yes --- if the column is auto incremented
// no --- if the column is not auto incremented
// empty string --- if it cannot be determined whether the
// column is auto incremented parameter is unknown
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 + "-");
}
conn.close();
?
申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!