當數據庫字段的值為NULL時,我們使用jdbc獲取到的值為什么呢?對於varchar、char等類型當使用getString時,根據常識輕松地知道值應該是NULL。但是,對於int、float等類型,當我們使用getInt、getFloat方法時應該返回的值也是NULL么。答案是否定的,我們根據這幾個的方法的注釋可以知道,當數據庫字段的值為NULL,通過jdbc獲取到的值為0。
float java.sql.ResultSet.getFloat(String columnLabel) throws SQLException方法的注釋如下。
Retrieves the value of the designated column in the current row of this ResultSet object as a float in the Java programming language.
Parameters:
columnLabel the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is 0
Throws:
SQLException - if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
但是當某一些字段的類型為INT、FLOAT時,比如表示價格的字段,我們想要用NULL值表示該字段未填寫值,那該怎么辦呢?這時我們可以使用getObject或者getString方法,當結果為null時表示未填寫值,當結果不為null時再將其轉換為相應的基本類型。
java中的類型和sql的類型的映射關系定義在JDBC規范中。接下來需要研究一下這個文檔。
jdbc類型映射到java類型
| JDBC Type | Java Type |
|---|---|
| CHAR | String |
| VARCHAR | String |
| LONGVARCHAR | String |
| NUMERIC | java.math.BigDecimal |
| DECIMAL | java.math.BigDecimal |
| BIT | boolean |
| BOOLEAN | boolean |
| TINYINT | byte |
| SMALLINT | short |
| INTEGER | int |
| BIGINT | long |
| REAL | float |
| FLOAT | double |
| DOUBLE | double |
| BINARY | byte[] |
| VARBINARY | byte[] |
| LONGVARBINARY | byte[] |
| DATE | java.sql.Date |
| TIME | java.sql.Time |
| TIMESTAMP | java.sql.Timestamp |
| CLOB | java.sql.Clob |
| BLOB | java.sql.Blob |
| ARRAY | java.sql.array |
| DISTINCT | Mapping of underlying type |
| STRUCT | java.sql.Struct |
| REF | java.sql.Ref |
| DATALINK | java.net.URL |
| JAVA_OBJECT | Underlying Java class |
| ROWID | java.sql.RowId |
| NCHAR | String |
| NVARCHAR | String |
| LONGNVARCHAR | String |
| NCLOB | java.sql.NClob |
| SQLXML | java.sql.SQLXML |
jdbc類型映射到java對象
| JDBC Type | Java Object Type |
|---|---|
| CHAR | String |
| VARCHAR | String |
| LONGVARCHAR | String |
| NUMERIC | java.math.BigDecimal |
| DECIMAL | java.math.BigDecimal |
| BIT | Boolean |
| BOOLEAN | Boolean |
| TINYINT | Integer |
| SMALLINT | Integer |
| INTEGER | Integer |
| BIGINT | Long |
| REAL | Float |
| FLOAT | Double |
| DOUBLE | Double |
| BINARY | byte[] |
| VARBINARY | byte[] |
| LONGVARBINARY | byte[] |
| DATE | java.sql.Date |
| TIME | java.sql.Time |
| TIMESTAMP | java.sql.Timestamp |
| DISTINCT | Object type of underlying type |
| CLOB | java.sql.Clob |
| BLOB | java.sql.Blob |
| ARRAY | java.sql.Array |
| STRUCT | java.sql.Struct or java.sql.SQLData |
| REF | java.sql.Ref |
| DATALINK | java.net.URL |
| JAVA_OBJECT | Underlying Java class |
| ROWID | java.sql.RowId |
| NCHAR | String |
| NVARCHAR | String |
| LONGNVARCHAR | String |
| NCLOB | java.sql.NClob |
| SQLXML | java.sql.SQLXML |
測試
創建測試用表,環境是mysql。
create table test(id int,price float,name varchar(2000));
插入數據
insert into test(id,price,name) values(12,12.3456,'阿里巴巴');
insert into test(id,price,name) values(1,312.3456,'阿里巴巴');
insert into test(id,price,name) values(null,142.3456,'阿里巴巴');
insert into test(id,price,name) values(3,null,'阿里巴巴');
數據表
| id | price | name |
|---|---|---|
| 12 | 12.3456 | 阿里巴巴 |
| 1 | 312.346 | 阿里巴巴 |
| NULL | 142.346 | 阿里巴巴 |
| 3 | NULL | 阿里巴巴 |
java使用jdbc連接
Statement st = conn.createStatement();
String sql = "select * from test where id = 3";
java.sql.ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String idStr = rs.getString("id");
Object idObj = rs.getObject("id");
int id = rs.getInt("id");
float price = rs.getFloat("price");
System.out.println("---------------------------");
System.out.println(idObj instanceof Integer);
System.out.println("id = " + id);
System.out.println("idStr = " + idStr);
System.out.println("idObj = " + idObj);
}
結果
---------------------------
true
id = 3
idStr = 3
idObj = 3
參考
- java.sql.ResultSet相應方法的注釋
- JSR 221 JDBC™ 4.2 Specification下載地址
