怎样将 oracle 的 blob 字段内容,读到 byte[] 数组中?? 我用的 是 weblogic 数据库连接, jdk 1.3
解决方案 »
- web系统双向认证问题(https,ssl,java)
- jfreechart如何设置3维度数?
- 急求帮忙~
- 为什么数据库连接断了之后重新启动才行?
- javaBean问题,着急啊。。不然以下的学习工作都无法开展拉。。帮下忙 谢谢大家!!!
- 能提供一份关于开发web应用系统的文档的例子么?谢谢。
- 关于打印的问题,请教各位大虾!
- 文件下载
- 将数据库中的字符串转化为整型数。。。。。。。。。
- 如何显示Excel文档在JSP页面中? thanks
- jsp中新开页面(如按shift)时的session失效问题
- 请问这是什么错误啊Cannot refer to a non-final variable conn inside an inner class defined in a different method
import java.sql.*;public class BlobTest {
private static final String driver = "oracle.jdbc.driver.OracleDriver";
private static final String url = "jdbc:oracle:thin:@localhost:1521:oradb";
private static final String insert =
"insert into bt values (?, empty_blob())";
private static final String update =
"select bf from bt where pkid=? for update";
private static final String query =
"select bf from bt where pkid=?";
private Connection conn;
private byte[] buf = new byte[128]; public static void main(String[] args) {
BlobTest bt = new BlobTest();
bt.write();
bt.read();
} private void write() {
try {
conn = this.getConnection();
conn.setAutoCommit(false);
PreparedStatement stmt = conn.prepareStatement(insert);
stmt.setInt(1, 1);
stmt.executeUpdate();
stmt = conn.prepareStatement(update);
stmt.setInt(1, 1);
ResultSet rs = stmt.executeQuery(); if (rs.next()) {
Blob blob = rs.getBlob("bf");
OutputStream os = ((oracle.sql.BLOB) blob).
getBinaryOutputStream(); byte[] b = new byte[128];
for (int i=0; i < b.length; i++)
b[i] = (byte)i;
ByteArrayInputStream is = new ByteArrayInputStream(b); int len = 0;
while ((len = is.read(buf)) != -1)
os.write(buf, 0, len);
os.flush();
os.close();
}
rs.close();
stmt.close();
conn.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException sex) {
sex.printStackTrace();
}
}
} private void read() {
try {
conn = this.getConnection();
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setInt(1, 1);
ResultSet rs = stmt.executeQuery(); if (rs.next()) {
Blob blob = rs.getBlob("bf");
InputStream is = blob.getBinaryStream();
ByteArrayOutputStream os = new ByteArrayOutputStream(); int len = 0;
while ((len = is.read(buf)) != -1)
os.write(buf, 0, len);
byte[] b = os.toByteArray();
os.flush();
os.close(); for (int i = 0; i < b.length; i++)
System.out.println(b[i]);
}
rs.close();
stmt.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException sex) {
sex.printStackTrace();
}
}
} private Connection getConnection() throws SQLException { try {
Class.forName(driver);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
System.exit( -1);
} return DriverManager.getConnection(url, "wd", "password");
}
}