JDBC 2.0 引入 SQL3 標準大對象與結構化類型支持,讓 Java 直接操作數據庫豐富類型:
String sql = "INSERT INTO tbl_User (username, photo) VALUES (?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
File file = new File("C:/images/photo.jpg");
FileInputStream fis = new FileInputStream(file);
pstmt.setString(1, "John");
pstmt.setBinaryStream(2, fis, (int) file.length()); // 關鍵:流 + 長度
pstmt.executeUpdate();
pstmt.close();
fis.close();
String sql = "SELECT photo FROM tbl_User WHERE username = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, "John");
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("photo");
byte[] imgBytes = blob.getBytes(1, (int) blob.length()); // 1-based 索引
ImageIcon icon = new ImageIcon(imgBytes);
JLabel photoLabel = new JLabel(icon);
// 添加到 Swing 界面...
}
rs.close();
pstmt.close();
String sql = "INSERT INTO tbl_Articles (title, content) VALUES (?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
File file = new File("C:/data/news.txt");
FileInputStream fis = new FileInputStream(file);
// 或使用 Reader: FileReader reader = new FileReader(file);
pstmt.setString(1, "Iraq War");
pstmt.setAsciiStream(2, fis, (int) file.length()); // ASCII 文本
// pstmt.setCharacterStream(2, reader, (int) file.length()); // 字符流 (含編碼)
pstmt.executeUpdate();
pstmt.close();
fis.close();
String sql = "SELECT content FROM tbl_Articles WHERE title = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, "Iraq War");
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
Clob clob = rs.getClob("content");
// 方式 1: ASCII 流
InputStreamReader in = new InputStreamReader(clob.getAsciiStream());
// 方式 2: 字符流 (推薦,自動處理編碼)
// Reader reader = clob.getCharacterStream();
JTextArea textArea = new JTextArea(readString(in)); // 輔助方法讀取流
}
rs.close();
pstmt.close();
setBinaryStream/setAsciiStream/setCharacterStream 避免將大對象完全載入 JVM 堆內存 (防 OOM)。setXxxStream(stream, length) 的長度必須準確,部分驅動依賴它優化傳輸。ResultSet/Statement,再關流,最後關 Connection (若非連接池)。BLOB/CLOB 需先 empty_blob() 初始化;MySQL LONGBLOB/LONGTEXT 直接支持流式寫入;PostgreSQL 使用 BYTEA/TEXT + Large Object API。具體驅動行為請查閱廠商文檔。Array:ResultSet.getArray() 返回 java.sql.Array,getArray() 獲取 Java 數組,getResultSet() 獲取結果集逐行處理。Ref:代表對象標識符 (OID),getObject() 可解析為具體結構對象。Struct / UDT:getObject() 返回 java.sql.Struct,getAttributes() 取字段值數組。需配合 SQLData 接口實現自定義映射。「這些新類型結合在一起,使得數據庫設計人員可以創建更豐富的模式,並簡化了對復雜數據的處理和持久化。」