高分请求: 怎样将 oracle 的 blob 字段内容,读到 byte[] 数组中?? 怎样将 oracle 的 blob 字段内容,读到 byte[] 数组中?? 我用的 weblogic 数据库连接, jdk 1.3 !! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 很简单,看看便知(以下取自本人的一些代码)String sql = "SELECT " + colName + " FROM " + tabName; Connection con = DataBaseConnection.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); int i = 1; while(rs.next()) { InputStream in = rs.getBinaryStream(1); File file = new File(outPath.getAbsolutePath() + "/0" + i + "." + fmt); FileOutputStream fout = new FileOutputStream(file); int b = in.read(); while(b != -1) { fout.write(b); b = in.read(); } in.close(); fout.close(); i ++; } rs.close(); stmt.close(); con.close(); 参考 public byte[] getBlob(int memberID){ String query = "select image from pictures where memberID= ?"; Blob blob = null; byte[] bytes = null; //String description = ""; try{ Context initCtx = new InitialContext(); DataSource db = (DataSource) initCtx .lookup("java:comp/env/jdbc/mysql"); Connection conn = db.getConnection(); PreparedStatement pstmt = conn.prepareStatement(query); pstmt.setInt(1,memberID); ResultSet rs = pstmt.executeQuery(); //ResultSetMetaData md = rs.getMetaData(); while(rs.next()){ blob = rs.getBlob(1); } bytes = blob.getBytes(1,(int)(blob.length())); conn.close(); }catch(Exception e){ e.printStackTrace(); } return bytes; } 招聘java工程师java jsp servlet spring hibernate js css上面会一部分既可qq 283765999 地点:北京 bytes = blob.getBytes(1,(int)(blob.length()));如果这个能成,那确实比我的理想。我的东西是酱紫地:package com.storm.app;import java.io.*;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"); }}好处是可以直接运行,只是不知道 JDK 1.3 上成不,从未用过 1.4 以下的 JDK,汗。 招聘java工程师java jsp servlet spring hibernate js css上面会一部分既可qq 283765999 地点:北京 求一个QQ下拉列表框 quickchm问题(不知道发哪。。。) 遍历问题!请教各位高手! rss订阅用jsp如何实现 java编译出错,大家快来看一下啊 又有一个错,帮我看看吧 visual cafe ,哪位大哥知道哪里有这方面的资料下载或有共享给我的。 请问各位高手,JTable里面怎么加一行数据! 求SQL Server 2000 JDBC的用法!(高分) [原创]java聊天机器人程序 关于现在的java数据结构与算法 Java怎么可以在最小化时把一个程序缩小在任务栏图标
Connection con = DataBaseConnection.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int i = 1;
while(rs.next())
{
InputStream in = rs.getBinaryStream(1);
File file = new File(outPath.getAbsolutePath() + "/0" + i +
"." + fmt);
FileOutputStream fout = new FileOutputStream(file);
int b = in.read();
while(b != -1)
{
fout.write(b);
b = in.read();
}
in.close();
fout.close();
i ++;
}
rs.close();
stmt.close();
con.close();
public byte[] getBlob(int memberID){
String query = "select image from pictures where memberID= ?";
Blob blob = null;
byte[] bytes = null;
//String description = "";
try{
Context initCtx = new InitialContext();
DataSource db = (DataSource) initCtx
.lookup("java:comp/env/jdbc/mysql");
Connection conn = db.getConnection();
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1,memberID);
ResultSet rs = pstmt.executeQuery();
//ResultSetMetaData md = rs.getMetaData();
while(rs.next()){
blob = rs.getBlob(1);
}
bytes = blob.getBytes(1,(int)(blob.length()));
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return bytes;
}
java工程师
java jsp servlet spring hibernate js css
上面会一部分既可qq 283765999
地点:北京
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");
}
}好处是可以直接运行,只是不知道 JDK 1.3 上成不,从未用过 1.4 以下的 JDK,汗。
java工程师
java jsp servlet spring hibernate js css
上面会一部分既可qq 283765999
地点:北京