0
在MySQL中,每列都有二進制,無符號和Zerofill標誌。有沒有辦法用Java 1.8獲得這個標誌的值?正如我所見,DatabaseMetaData.getColumns()不返回這個值。如何使用Java從MySQL獲取列規範?
在MySQL中,每列都有二進制,無符號和Zerofill標誌。有沒有辦法用Java 1.8獲得這個標誌的值?正如我所見,DatabaseMetaData.getColumns()不返回這個值。如何使用Java從MySQL獲取列規範?
假設一個測試表與
CREATE TABLE tableA
( id int auto_increment primary key,
w INT(8) ZEROFILL NOT NULL,
x INT(8) NOT NULL,
y int signed not null,
z int unsigned not null,
shortBinaryCharString char(10) binary not null,
myBlob blob(10000) null
);
如從here大多借用爲起點產生。
我會建議使用標準查詢在INFORMATION_SCHEMA
數據庫中的表。如
select table_schema, table_name, column_name, ordinal_position, is_nullable, data_type,
character_set_name, collation_name, column_type, column_key, extra
from INFORMATION_SCHEMA.columns
where table_schema = 'so_gibberish' -- your database/schema name
and table_name='tableA'
+--------------+------------+-----------------------+------------------+-------------+-----------+--------------------+----------------+--------------------------+------------+----------------+
| table_schema | table_name | column_name | ordinal_position | is_nullable | data_type | character_set_name | collation_name | column_type | column_key | extra |
+--------------+------------+-----------------------+------------------+-------------+-----------+--------------------+----------------+--------------------------+------------+----------------+
| so_gibberish | tablea | id | 1 | NO | int | NULL | NULL | int(11) | PRI | auto_increment |
| so_gibberish | tablea | w | 2 | NO | int | NULL | NULL | int(8) unsigned zerofill | | |
| so_gibberish | tablea | x | 3 | NO | int | NULL | NULL | int(8) | | |
| so_gibberish | tablea | y | 4 | NO | int | NULL | NULL | int(11) | | |
| so_gibberish | tablea | z | 5 | NO | int | NULL | NULL | int(10) unsigned | | |
| so_gibberish | tablea | shortBinaryCharString | 6 | NO | char | utf8 | utf8_bin | char(10) | | |
| so_gibberish | tablea | myBlob | 7 | YES | blob | NULL | NULL | blob | | |
+--------------+------------+-----------------------+------------------+-------------+-----------+--------------------+----------------+--------------------------+------------+----------------+
您還可能有運氣與執行
SHOW FIELDS FROM tableA;
或
show create table tableA
用標準的Java結果集,你最有可能已經在這樣做就足夠了。
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("show fields from tableA");
while (rs.next()) {
System.out.println(rs.getString(1)+": "+rs.getString(2));
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(myTest.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
注:字符集,並顯示出對比檢驗列的原因在於,存在具有與某個字符集排序順序炭二進制列之間的細微差別的差別。與沒有二進制排序順序的真正二進制blob/text列相比。請參閱標題爲The BINARY and VARBINARY Types的Mysql手冊頁,並確定它如何適用於「二元」概念。通常,必須解析一列輸出以確定是否打開zerofill。