如何将附件插入到oracle中的blob字段中?

解决方案 »

  1.   

    你命好,刚上传的资源
    http://download.csdn.net/user/JadoNet
      

  2.   

    无非是使用字节流将附件的内容读出来,在使用字节流的toString("UTF-8"),获取生成的字符串,将字符串保存到blob字段中。反之需要解析成附件的话,就将字段饭解析回来就可以了
      

  3.   

    前几天刚好些了个例子public class BlobTest {

    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@192.168.38.243:1521:orcl";
    String username = "fm";
    String password = "manager";
    Connection con = null;
    Statement stmt = null;

    public BlobTest() {
    con = getConnection();
    try {
    stmt = con.createStatement();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {
    BlobTest bt = new BlobTest();
    bt.insertBolb();
    bt.getBlob();
    bt.close();
    }

    private void insertBolb() {
    try {
    //insert an empty blob
    String sql1 = "insert into blobtest values ('001',empty_blob())";
    stmt.executeUpdate(sql1);

    //query the empty blob for update
    String sql2 = "select filedata from blobtest where fileid = '001' for update";
    ResultSet rs = stmt.executeQuery(sql2);

    //read the empty bolb
    while(rs.next()) {
    oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("filedata");
    OutputStream os = blob.getBinaryOutputStream();

    //read outter file
    InputStream is = new FileInputStream(new File("test.txt"));

    //write inputstream to outputstream
    byte[] b = new byte[blob.getBufferSize()];
    int len = 0;
    while((len = is.read(b)) != -1) {
    os.write(b, 0, len);
    }
    is.close();
    os.flush();
    os.close();
    con.commit();
    }

    } catch (SQLException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private void getBlob() {
    try {
    //get blob from db
    String sql = "select filedata from blobtest where fileid = '001'";
    ResultSet rs = stmt.executeQuery(sql);

    while(rs.next()) {
    //get inputstream from resultset
    oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("filedata");
    InputStream is = blob.getBinaryStream();

    //create fileoutputstream
    OutputStream os = new FileOutputStream(new File("out.txt"));

    //write to file
    byte[] b = new byte[1024];
    int len;
    while((len = is.read(b)) != -1) {
    os.write(b, 0, len);
    }
    os.flush();
    os.close();
    is.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private Connection getConnection() {
    try {
    Class.forName(driver);
    con = DriverManager.getConnection(url,username,password);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return con;
    }

    private void close() {
    if(stmt != null) {
    try {
    stmt.close();
    } catch(SQLException e) {
    e.printStackTrace();
    } finally {
    if(con != null) {
    try {
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }

    }
    }
    }}