用Java实现磁盘文件以大对象二进制文件形式存储到Oracle数据库

BLOB blob = null; 
PreparedStatement pstmt = conn.prepareStatement("insert into javatest(name,context) values(?,empty_blob())"); 
            pstmt.setString(1,""+list.get(i).toString()+""); 
            pstmt.executeUpdate(); 
            pstmt.close(); 

            pstmt = conn.prepareStatement("select context from javatest where name= ? for update"); 
            pstmt.setString(1,""+list.get(i).toString()+""); 
            ResultSet rset = pstmt.executeQuery(); 
            if (rset.next()) blob = (BLOB) rset.getBlob(1); 

            pstmt = conn.prepareStatement("update javatest set context=? where name=?"); 

            OutputStream out = blob.getBinaryOutputStream(); 

            int count = -1, total = 0; 
            byte[] data = new byte[(int)fin.available()]; 
            fin.read(data); 
            out.write(data); 
            /* 
            byte[] data = new byte[blob.getBufferSize()];  另一种实现方法,节省内存 
            while ((count = fin.read(data)) != -1) { 
              total += count; 
              out.write(data, 0, count); 
            } 
            */ 

            
            fin.close(); 
            out.close(); 

            pstmt.setBlob(1,blob); 
            pstmt.setString(2,""+list.get(i).toString()+""); 
            pstmt.executeUpdate(); 
            
            pstmt.close(); 

 

你可能感兴趣的:(java,oracle)