http://www.csdn.net/expert/topic/83/83096.shtm

解决方案 »

  1.   

    不行啊!
    我用的数据库类型是blob.
    如果是文本文件可以。但是时图片的话就报错:
    java.sql.SQLException:ora-01461:仅可以为插入long列的long值赋值。
    就是说不能把一个long值插入到不是long值得列。但是插入一个html文件倒是可以!
      

  2.   

    /* this is code . The image is stored as blob datatype in oracle database */ 
    try { 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); 
    Connection con=DriverManager.getConnection("jdbc:odbc:Multimedia","samp","samp"); 
    Statement stmt=con.createStatement(); 
    ResultSet rs=stmt.executeQuery("select frame from MULTIMEDIA_TAB where clip_id=1"); 
    if(rs.next()){ 
    try { 
    InputStream dis=rs.getBinaryStream(1); 
    byte[] x = new byte [10*1024]; //creare byte array to hold the blob data 
    int lengthRead = 0; // Number of bytes read 
    String fileToWriteBlob = "./default-app/MyProgs/ImageRetrieval/ganpati.jpg"; 
    FileOutputStream outputStream = new FileOutputStream(fileToWriteBlob); 
    while( (lengthRead =dis.read(x)) != -1){ 
    outputStream.write(x); 

    dis.close(); 
    outputStream.close(); 
    }
    catch(Exception e) { 
    System.out.println ("Error is : " + e); 


    }
    catch(Exception ex) { 
    System.out.println("Error " + ex); 

      

  3.   

    The following is one way of saving a byte[] array into a BLOB field in an Oracle database: If you are inserting a row: 1.Insert the row with all values, but insert an empty blob into the blob column: 
    "INSERT INTO yourtable (id,blob_value) VALUES (?,empty_blob())" 2. Select the empty blob you created: 
    "SELECT blob_value FROM yourtable WHERE id=?" 
    oracle.sql.BLOB blob = rs.getBLOB("blob_value"); 
    (some code is of course missing, but this should give you the idea) 
    3. Write the bytes into the blob: try { 
    OutputStream outstream = blob.getBinaryOutputStream(); 
    outstream.write(blobValue); 
    outstream.flush(); 
    outstream.close(); 
    } catch (IOException e) { 
    // handle exception 

    Where blobValue is the byte array containing the image data. 4. Update the row: 
    "UPDATE yourtable SET blob_value=? WHERE id=?" 
    pstmt.setBLOB(1, blob); 
    pstmt.setInt(2, id); 
    (NOTE: the setBLOB is an Oracle specific method, you can probably also use the setBlob method). 
    Again, some essential code concerning the prepared statements is missing, but this should give you an idea... That's that. If you just need to update the row, you just go through steps 2-4 above. 
      

  4.   

    Here is an example: // ---->%---- 
    // images blob test import java.sql.*; 
    import java.io.*; public class imgtest { private PreparedStatement ps = null; 
    private Connection conn = null; public void initialise() { 
    String URL = "jdbc:oracle:thin:@localhost:1521:SID"; 
    String userid = "user"; 
    String passwd = "pass"; 
    // register the JDBC driver 
    try { 
    DriverManager.registerDriver(new 
    oracle.jdbc.driver.OracleDriver()); 
    // get a connection 
    Connection conn = DriverManager.getConnection(URL, userid, passwd); 
    this.conn = conn; 
    // create a prepared statement 
    ps = conn.prepareStatement("INSERT INTO images (NAME,IMAGE) VALUES (?,?)"); 

    catch (Exception e) { 
    System.out.println("Error: " + e); 
    System.exit(1); 

    } public imgtest() { 
    initialise(); 
    File fImage = null; 
    FileInputStream isImage = null; try { 
    fImage = new File( "fileo.gif" ); 
    isImage = new FileInputStream( fImage ); 

    catch (FileNotFoundException fnf) { 
    System.out.println("File not found: " + fnf); 
    System.exit(1); 

    try { 
    ps.setString(1,"Open File"); 
    ps.setBinaryStream(2,isImage,(int)(fImage.length())); 
    ps.executeUpdate(); 
    ps.close(); 
    conn.close(); 

    catch (Exception e) { 
    System.out.println("Error: " + e); 
    System.exit(1); 

    } public static void main(String[] args) { 
    new imgtest(); 


    // ---->%---- My table looks like this: 
    // ---->%---- 
    CREATE TABLE images 
    (NAME VARCHAR2(16) primary key not null, 
    IMAGE BLOB(64k)); 
    commit; 
    quit; 
    // ---->%----
      

  5.   

    还是不行啊.你上面的这个例子和再上一个的思路不是一样的.我原来就是这么做的,但是就会说
    不能往不是long型的列中插入long类型的数据.我查考你的再上一个例子的思路做了一个.运行时
    说ORA-22920:未锁定含有lob值的行.我看了oracle的文档.这个ORA-22920的错误时指对lob进行
    操作时必须将它锁定.但是我不知道如何去锁定它,我觉得这应该是有数据库系统去锁定的,不应该
    由程序来去锁定,而且jdbc好像也没有提供这个方法.oracle的jdbc中也没有.
    下面是我的程序.请指点!非常感谢!import java.io.*;
    import com.sql.*;
    import oracle.sql.*;
    import oracle.jdbc.driver.*;
    public class WriteImage{ public static void main(String args[]){
    try{
    DBConnectionManager dbcm = DBConnectionManager.getInstance();
    OracleConnection conn = (OracleConnection)dbcm.getConnection("bbs");
    if(conn == null){
    System.out.println("DataBses is busing,Please try wait a minute!");
    System.exit(1);
    }
    //ResultSet rs = null;
    String filename = "logo.gif";
    OracleStatement  stmt = (OracleStatement)conn.createStatement();
    OraclePreparedStatement pstmt = null;
    File f = new File(filename);
    int len = (int)f.length();
    FileInputStream fis = new FileInputStream(f);
    BLOB blob = null;
    OracleResultSet rs;
    int i;
        rs = (OracleResultSet)stmt.executeQuery("select content from image");
        if(rs.next()){
    blob = rs.getBLOB(1);
    System.out.println("1");
    }
            rs.close();
        OutputStream os = blob.getBinaryOutputStream();
        byte[] b = new byte[1024];
        while ((i = fis.read(b)) != -1){
    os.write(b);
    }
        os.flush();
        os.close();
        System.out.println("2");
        pstmt = (OraclePreparedStatement)conn.prepareStatement("update image set content=?");
        pstmt.setBLOB(1,blob);
        System.out.println("3");
        if(pstmt.executeUpdate() > 0){
        System.out.println("Succeed!");
    }
        fis.close();
        stmt.close();
        pstmt.close();
        dbcm.freeConnection("bbs",conn);
        dbcm.release();
        }
    catch(Exception e){
    System.out.print("Error:"+e.toString()); }
    } }