怎样将 oracle 的 blob 字段内容,读到 byte[] 数组中?? 我用的 weblogic 数据库连接, jdk 1.3 !!

解决方案 »

  1.   

    很简单,看看便知(以下取自本人的一些代码)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();
      

  2.   

    参考
        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;
        }
      

  3.   

    招聘
    java工程师
    java jsp servlet spring hibernate js css
    上面会一部分既可qq 283765999 
    地点:北京
      

  4.   

    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,汗。
      

  5.   

    招聘
    java工程师
    java jsp servlet spring hibernate js css
    上面会一部分既可qq 283765999 
    地点:北京