Mysql與java對應的型別表
1. 概述
在使用Java JDBC時,你是否有過這樣的疑問:MySQL裡的資料型別到底該選擇哪種Java型別與之對應?本篇將為你揭開這個答案。
2. 型別對映
java.sql.Types定義了常用資料庫(MySQL、Oracle、DB2等)所用到的資料型別,官名也叫JDBC型別。每個資料庫產品的資料型別定義各不相同,但都有JDBC型別與之對應,如MySQL的BIGINT-->JDBC的BIGINT。
而每個JDBC型別,都有預設的Java型別與之對應,即ResultSet.getObject()返回Object的具體型別,如JDBC的BIGINT-->Java的java.lang.Long;JDBC的BIGINT UNSIGNED-->Java的 java.math.BigInteger。
我們可以通過ResultSet.getMetaData().getColumnTypeName(columnIndex)獲取欄位的JDBC型別,通過ResultSet.getMetaData().getColumnClassName(columnIndex)獲取欄位的Java型別。
下圖展示了MySQL型別型別、JDBC型別、Java型別的對映關係(源自:5.3 Java, JDBC and MySQL Types)。
MySQL資料型別 | JDBC型別(getColumnTypeName) | 預設返回的Java型別(getColumnClassName) |
---|---|---|
BIT(1) (new in MySQL-5.0) |
BIT |
java.lang.Boolean |
BIT( > 1) (new in MySQL-5.0) |
BIT |
byte[] |
TINYINT |
TINYINT |
java.lang.Boolean if the configuration property tinyInt1isBit is set to true (the default) and the storage size is 1, or java.lang.Integer |
BOOL , BOOLEAN |
TINYINT |
See TINYINT , above as these are aliases for TINYINT(1) , currently. |
SMALLINT[(M)] [UNSIGNED] |
SMALLINT [UNSIGNED] |
java.lang.Integer (regardless of whether it is UNSIGNED or not) |
MEDIUMINT[(M)] [UNSIGNED] |
MEDIUMINT [UNSIGNED] |
java.lang.Integer (regardless of whether it is UNSIGNED or not) |
INT,INTEGER[(M)] [UNSIGNED] |
INTEGER [UNSIGNED] |
java.lang.Integer , if UNSIGNED java.lang.Long |
BIGINT[(M)] [UNSIGNED] |
BIGINT [UNSIGNED] |
java.lang.Long , if UNSIGNED java.math.BigInteger |
FLOAT[(M,D)] |
FLOAT |
java.lang.Float |
DOUBLE[(M,B)] |
DOUBLE |
java.lang.Double |
DECIMAL[(M[,D])] |
DECIMAL |
java.math.BigDecimal |
DATE |
DATE |
java.sql.Date |
DATETIME |
DATETIME |
java.sql.Timestamp |
TIMESTAMP[(M)] |
TIMESTAMP |
java.sql.Timestamp |
TIME |
TIME |
java.sql.Time |
YEAR[(2|4)] |
YEAR |
If yearIsDateType configuration property is set to false , then the returned object type is java.sql.Short . If set to true (the default), then the returned object is of type java.sql.Date with the date set to January 1st, at midnight. |
CHAR(M) |
CHAR |
java.lang.String (unless the character set for the column is BINARY , then byte[] is returned. |
VARCHAR(M) [BINARY] |
VARCHAR |
java.lang.String (unless the character set for the column is BINARY , then byte[] is returned. |
BINARY(M) |
BINARY |
byte[] |
VARBINARY(M) |
VARBINARY |
byte[] |
TINYBLOB |
TINYBLOB |
byte[] |
TINYTEXT |
VARCHAR |
java.lang.String |
BLOB |
BLOB |
byte[] |
TEXT |
VARCHAR |
java.lang.String |
MEDIUMBLOB |
MEDIUMBLOB |
byte[] |
MEDIUMTEXT |
VARCHAR |
java.lang.String |
LONGBLOB |
LONGBLOB |
byte[] |
LONGTEXT |
VARCHAR |
java.lang.String |
ENUM('value1','value2',...) |
CHAR |
java.lang.String |
SET('value1','value2',...) |
CHAR |
java.lang.String |
3. 型別轉換
上面我們看到MySQL的BIGINT預設轉為Java的java.lang.Long,那是不是就不能轉為String或其他數值型別了?答案是否定的!MySQL的JDBC(Connector/J)在欄位型別與Java型別之間的轉換是比較靈活的。一般來說,任何欄位型別都可以轉換為java.lang.String,任何數值欄位型別都可以轉換為Java的資料型別(當然會出現四捨五入、溢位、精度丟失的問題)。
下圖展示了MySQL JDBC允許的跨型別相互轉換。
MySQL資料型別 | 可以被轉換的Java型別 |
---|---|
CHAR, VARCHAR, BLOB, TEXT, ENUM, and SET |
java.lang.String, java.io.InputStream, java.io.Reader, java.sql.Blob, java.sql.Clob |
FLOAT, REAL, DOUBLE PRECISION, NUMERIC, DECIMAL, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT |
java.lang.String, java.lang.Short, java.lang.Integer, java.lang.Long, java.lang.Double, java.math.BigDecimal |
DATE, TIME, DATETIME, TIMESTAMP |
java.lang.String, java.sql.Date, java.sql.Timestamp |