向oracle写入大数据

来源:互联网 发布:深圳直销软件开发 编辑:程序博客网 时间:2024/04/29 23:11
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.*;
import java.sql.Blob;
import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;
public class JdbcBlod {


    public static void main(String[] args){
    
        Connection conn = null;
        String sql = "insert into BL values(?,empty_blob())";
        PreparedStatement ps;
        try {
              Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
              String url="jdbc:oracle:thin:@192.168.0.7:1521:KMSS";
              String user="kmadmin";
              String password="szlandray";
              Blob blob = null;
              conn= DriverManager.getConnection(url,user,password);
              conn.setAutoCommit(false);
              ps = conn.prepareStatement(sql);
              ps.setString(1,"234");
              ps.executeUpdate();
             
              ps.close();
              ps = conn.prepareStatement("select bl from BL where id= ? for update");
              ps.setString(1,"234");
              ResultSet rset = ps.executeQuery();
              if (rset.next()) {
                  blob = rset.getBlob(1);
                 
                  ps = conn.prepareStatement("update BL set bl=? where id=?");
                  OutputStream out = ((oracle.sql.BLOB)blob).getBinaryOutputStream();
                  File tmpFile=new File("D://11.jpg");
                  InputStream fin = new FileInputStream(tmpFile);
                  byte[] data = new byte[((oracle.sql.BLOB)blob).getBufferSize()];
                  int nread = 0;   // Number of bytes read
              while( (nread= fin.read(data)) != -1 ) // Read from file
                  out.write(data, 0, nread);
                  fin.close();
                  out.close();
              }
              ps.close();
              conn.commit();
              conn.close();
        } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
        }catch(FileNotFoundException FNFE) {
             
        }catch(IOException ioe){
             
        }catch(ClassNotFoundException cnfe){
             
        }catch(InstantiationException ine){
             
        }catch(IllegalAccessException ile){
             
        }
    }
}

原创粉丝点击