Blob:
写入:
FileInputStream fis = new FileInputStream(file);
pstmt.setBinaryStream(1, fis, (int) file.length());
... pstmt.close();
fis.close();
读取:
Blob blob = rs.getBlob("photo");
ImageIcon icon = new ImageIcon(blob.getBytes(1, (int)blob.length()));
JLabel lPhoto = new JLabel(icon);
setLayout(new GridLayout(1, 1));
add(lPhoto);
Clob:
写入:
FileInputStream fis = new FileInputStream(file);
pstmt.setAsciiStream(1, fis, (int)file.length());
读取:
Clob clob = rs.getClob("memo");
InputStreamReader in = new InputStreamReader(clob.getAsciiStream());
JTextArea text = new JTextArea(readString(in));
setLayout(new GridLayout(1, 1));
add(text);
public static String readString(InputStreamReader in) throws IOException {
StringBuffer buf = new StringBuffer();
int n;
char[] b = new char[1024];
while((n = in.read(b)) > 0)
buf.append(b, 0, n);
return buf.toString();
}
写入:
FileInputStream fis = new FileInputStream(file);
pstmt.setBinaryStream(1, fis, (int) file.length());
... pstmt.close();
fis.close();
读取:
Blob blob = rs.getBlob("photo");
ImageIcon icon = new ImageIcon(blob.getBytes(1, (int)blob.length()));
JLabel lPhoto = new JLabel(icon);
setLayout(new GridLayout(1, 1));
add(lPhoto);
Clob:
写入:
FileInputStream fis = new FileInputStream(file);
pstmt.setAsciiStream(1, fis, (int)file.length());
读取:
Clob clob = rs.getClob("memo");
InputStreamReader in = new InputStreamReader(clob.getAsciiStream());
JTextArea text = new JTextArea(readString(in));
setLayout(new GridLayout(1, 1));
add(text);
public static String readString(InputStreamReader in) throws IOException {
StringBuffer buf = new StringBuffer();
int n;
char[] b = new char[1024];
while((n = in.read(b)) > 0)
buf.append(b, 0, n);
return buf.toString();
}
Java中Blob和Clob的读写操作
博客介绍了Java中Blob和Clob的读写操作。对于Blob,给出了写入时使用FileInputStream和pstmt.setBinaryStream,读取时使用rs.getBlob等代码示例;对于Clob,展示了写入时用pstmt.setAsciiStream,读取时用rs.getClob及相关处理的代码。

被折叠的 条评论
为什么被折叠?



