byte[] b;
PreparedStatement ps=conn.prepareStatement("insert into a(aaa) values(?)");
ps.setBytes(b);
ps.executeUpdate();
ps.close();

解决方案 »

  1.   

    import java.sql.*;
    import java.io.*;
    import oracle.jdbc.driver.*;
    import oracle.sql.*;public class OracleBlob

    public  void InsertBlob()
    {
    Connection con=null;
    PreparedStatement stmt=null;
    ResultSet rs=null;
    PictrueFrame window=null;

    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:oci8:@ESALES_DATASERVER","sa","sa");
    con.setAutoCommit(false);
    stmt=con.prepareStatement(
    "insert into product "+
    "(PDID,PDNAME,PDTYPEID,PDPRICE,PDCOST,STOCKS,ENID,CURID,PDIMAGE) "+
                     "values(321,'tupian',45,246,246,2.645,1,1,EMPTY_BLOB())");
    stmt.execute();
    System.out.println ("Inserted:"+stmt.getUpdateCount());

    stmt=con.prepareStatement("select pdimage from product where pdid=321 for update");
    rs=stmt.executeQuery();
    if(rs.next())
    {
    BLOB blob=(BLOB)((OracleResultSet)rs).getBlob(1);
    File f=new File("d://xxx.jpg");
    OutputStream out=blob.getBinaryOutputStream();
    FileInputStream in=new FileInputStream(f);
    byte[] buf=new byte[blob.getChunkSize()];
    int len;
    while((len=in.read(buf))!=-1)
    {
    out.write(buf,0,len);
    }
    out.flush();
    System.out.println ("OK 了。");
    }
    con.commit();

        }
        catch (Exception ex) {
         ex.printStackTrace();
        } }

    public void showBlob(int i)
    {
    Connection con=null;
    PreparedStatement stmt=null;
    ResultSet rs=null;
    PictrueFrame window=null;
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:oci8:@ESALES_DATASERVER","sa","sa");
    stmt=con.prepareStatement("select pdimage from product where pdid=?");
    stmt.setInt(1,i);
    rs=stmt.executeQuery();
    if(rs.next())
    {
    System.out.println ("Blob is showed.");
    oracle.sql.BLOB blob=((OracleResultSet)rs).getBLOB(1);
    if(blob!=null)
    {
    byte [] buf=blob.getBytes(1L,(int)blob.length());
    window=new PictrueFrame(buf);
    window.setVisible(true);
        }
    }
        }
        catch (Exception ex) {
         ex.printStackTrace();
        }
    }

    }我的一个小小的例子。你看看吧。
      

  2.   

    import oracle.jdbc.driver.*;
    import oracle.sql.*;这两句话为什么不能引用?
      

  3.   

    用下面这种也可以把Blob数据插到数据库中去// two local files of pictures
    String file1 = "PEiffelTower.jpg";
    String file2 = "EiffelTower.jpg"; try {
              // to connect to the database
      Connection conn = 
        getConnected(drivername,dbstring, username,password);   //  create a preparedStatement with 
      //   ?  represents the lobs to be inserted
      PreparedStatement stmt = conn.prepareStatement(
                  "insert into pictures values (20,'Eiffel Tower','Paris',?,? )" );   // Set the first parameter 
      File file = new File( file1 );
              stmt.setBinaryStream(1,new FileInputStream(file),(int)file.length());   // set the second parameter
      file = new File(file2);
      stmt.setBinaryStream(2,new FileInputStream(file),(int)file.length());   // execute the insert statement
      stmt.executeUpdate();
      System.out.println( "the execution succeeds");   conn.close();
    } catch( Exception ex ) { 
        System.out.println( ex.getMessage());
    }
      

  4.   

    二进制的数据并不是真正的存储在数据库中,在数据库中存储的只是一个路径,所以在读取这些数据的时候要,利用输入,输出流的方式来写入和读取它。
    下面的例子是写入一个二进制文件到oracle中并读取出它!
    BolbBean.java
    -----------------------------------------
    import java.sql.*;
    import javax.sql.*;
    import java.io.*;
    import oracle.jdbc.driver.OracleResultSet;
    import oracle.sql.BLOB;
    public class BlobBean
    {
       private Connection conn;
       /**
        *构造方法,创建Connection对象,并且在数据库中添加一个表。
        */
       public BlobBean()throws Exception
       {
           Class.forName("oracle.jdbc.driver.OracleDriver");
           conn =DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:oracle","scott",
     "tiger");//依据各人的情况来更改
           conn.createStatement().execute("create table blobtable(blobvalue blob)");
        }
        
        /**
         *写入Blob数据到数据库
         */
       public void addBlob(String fileName)throws Exception
       {
    conn.setAutoCommit(false);
           Statement stmt = conn.createStatement();
            stmt.execute("insert into blobtable values (empty_blob())");//注意写入二进制数据的方法
            ResultSet rset = stmt.executeQuery("SELECT blobvalue FROM blobtable FOR UPDATE");
            BLOB blob = null;
            while (rset.next()) {
                blob = ((OracleResultSet) rset).getBLOB(1);
                System.out.println(blob.length());
            }
            File binaryFile = new File(fileName);
            System.out.println(fileName+"'s length = " + binaryFile.length());
            FileInputStream instream = new FileInputStream(binaryFile);
            OutputStream outstream = blob.getBinaryOutputStream();
            int chunk = blob.getChunkSize();
            System.out.println("chunk size = " + chunk);
            byte[] buffer = new byte[chunk];
            int length = -1;
            while ((length = instream.read(buffer)) != -1)
               outstream.write(buffer, 0, length);
            instream.close();
            outstream.close();
            conn.commit();
        } 
        /**
         *从数据库读取blob数据,并且保存在文件系统。
         */
         public void readBlob(String fileName)throws Exception
         {
     conn.setAutoCommit(false);
             Statement stmt = conn.createStatement();
             ResultSet rset = stmt.executeQuery("SELECT blobvalue FROM blobtable");
             BLOB blob = null;
             while (rset.next()) {
                blob = ((OracleResultSet) rset).getBLOB(1);
                System.out.println(blob.length());
             }         FileOutputStream file_out = new FileOutputStream(new File(fileName));
             InputStream blob_in = blob.getBinaryStream();
             int temp;
             while((temp=blob_in.read())!=-1)
             file_out.write(temp);//读取数据、写入文件系统
               
             file_out.close();
             blob_in.close();
             conn.commit();
         }        
             
    }
    testblob.jsp
    ---------------------------------------
     <%@ page contentType="text/html; charset=gb2312" %>
    <jsp:useBean id="blobtest" class="BlobBean" scope="page"/>
    <%
    try{
        blobtest.addBlob("c:\\case.mdl");//写一个存在的文件路径
        blobtest.readBlob("c:\\case2.mdl");
        out.println("sucess!");
    }
    catch(Exception e){
       out.println(e);
       e.printStackTrace();
    }
    %>
      

  5.   

    我同意 fuzuyuan(happyboy) 他说的!