首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Java JDBC ResultSet 正確讀取各類型資料庫欄位值

     編輯:程式設計綜合問答

核心 API 對照表

資料庫類型Java 類型ResultSet 方法
INT, BIGINT, SMALLINTint, long, shortgetInt(), getLong(), getShort()
FLOAT, DOUBLE, DECIMALfloat, double, BigDecimalgetFloat(), getDouble(), getBigDecimal()
CHAR, VARCHAR, TEXTStringgetString()
DATEjava.sql.DategetDate()
TIMESTAMP, DATETIMEjava.sql.TimestampgetTimestamp()
BLOBInputStream / byte[]getBinaryStream(), getBytes()
CLOBReader / StringgetCharacterStream(), getString()

完整讀取範例

try (Connection conn = DriverManager.getConnection(url, user, pass);
     PreparedStatement ps = conn.prepareStatement("SELECT id, name, birth, salary, photo FROM users");
     ResultSet rs = ps.executeQuery()) {

    while (rs.next()) {
        int id = rs.getInt("id");
        String name = rs.getString("name");
        Date birth = rs.getDate("birth");
        BigDecimal salary = rs.getBigDecimal("salary");
        Blob photo = rs.getBlob("photo");
        
        // 日期格式化
        String birthStr = (birth != null) ? new SimpleDateFormat("yyyy-MM-dd").format(birth) : "";
        // Blob 處理
        byte[] imgBytes = (photo != null) ? photo.getBytes(1, (int) photo.length()) : null;
        
        System.out.printf("%d | %s | %s | %s | %d bytes%n", id, name, birthStr, salary, imgBytes?.length ?? 0);
    }
}

關鍵注意事項

  1. 列名 vs 索引:推薦使用列名(rs.getInt("id")),避免 SELECT 順序變更導致錯誤。
  2. NULL 值處理:getInt() 遇 NULL 返回 0,無法區分。需先用 rs.wasNull() 或改用包裝類型邏輯判斷。
  3. 流式資源:getBinaryStream()/getCharacterStream() 返回的流需在 finally 或 try-with-resources 中關閉。
  4. 游標方向:預設 TYPE_FORWARD_ONLY,若需隨機訪問建立 Statement 時指定 ResultSet.TYPE_SCROLL_INSENSITIVE。
  5. 大數據量:MySQL 需設定 stmt.setFetchSize(Integer.MIN_VALUE) 避免一次性載入全部結果導致 OOM。

進階技巧

  • 使用 ResultSetMetaData 動態獲取列類型名稱,構建通用映射邏輯。
  • 搭配 RowMapper (Spring JdbcTemplate) 或 ORM 框架減少樣板代碼。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved