用Blob类型,用STREAM的方式进行读写。

解决方案 »

  1.   


    文件写入数据库import java.util.*;
    import java.sql.*;
    import java.io.*;
    import java.text.*;
    import oracle.sql.*;
    import oracle.jdbc.driver.*;public class write {  public static void main(String[] args) throws SQLException,IOException
      {
        PreparedStatement stmt = null;
        ResultSet rs =  null;
        InputStream fin = null;
        OutputStream fout = null;
        Connection conn=null;
        try{
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        conn =DriverManager.getConnection ("jdbc:oracle:thin:@gyf:1521:orcl",
       "test", "test");
        conn.setAutoCommit(false);    stmt=conn.prepareStatement("insert into  tmanuscript (glidenumber,filename,word) values (?,?,EMPTY_BLOB() ) ");
        stmt.setString(1,args[0]);
        System.out.println(args[1]);
        stmt.setString(2,args[1]);
        stmt.execute() ;
        stmt.clearParameters() ;
        String mysql="select word from tmanuscript where glidenumber='"+args[0]+"' for update ";
        stmt=conn.prepareStatement(mysql );
        //stmt.setString(1,args[0]);
        rs=stmt.executeQuery() ;
        if(rs.next()) {
    BLOB blob = ((OracleResultSet)rs).getBLOB("word");
    fout = blob.getBinaryOutputStream();
    File f = new File(args[1]);
    fin = new FileInputStream(f);
    byte[] buffer = new byte[blob.getBufferSize()];
    int bytesRead = 0;
    while((bytesRead = fin.read(buffer)) != -1) 
    {
    fout.write(buffer, 0, bytesRead);
    System.out.println(bytesRead);
    } blob = null;
    f = null;
    buffer = null;
    fin.close();
    fout.close();
    }
      }
      catch(SQLException ex)
        {
    ex.printStackTrace();  }
    catch(FileNotFoundException ef) {
    ef.printStackTrace(); }
    catch(Exception e) {
    e.printStackTrace(); } finally {
    try {

    conn.commit();
    stmt.close();
    conn.close();
    fin = null;
    fout = null;
    rs = null;
    conn = null;
    stmt = null;
    }
                            catch(SQLException e) {
    e.printStackTrace();
    }
                            }
    }}从数据库读出import java.util.*;
    import java.sql.*;
    import java.io.*;
    import java.text.*;
    import oracle.sql.*;
    import oracle.jdbc.driver.*;public class read {   public static void main(String[] args) throws SQLException,IOException
      {
          Connection conn = null;
    PreparedStatement stmt = null;
    InputStream in = null;
    OutputStream out = null;
    BLOB blob = null;
    ResultSet rs = null;
    try {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                conn =DriverManager.getConnection ("jdbc:oracle:thin:@gyf:1521:orcl",
                                               "test", "test");
                conn.setAutoCommit(true);
                 String mysql="Select word FROM tmanuscript WHERE glidenumber='"+args[0]+"' for update";
    System.out.println(mysql);

    stmt = conn.prepareStatement(mysql);
    //stmt.setString(1,"67");
    rs = stmt.executeQuery();
    if(rs.next()) {
    blob = ((OracleResultSet)rs).getBLOB("word");

    in = blob.getBinaryStream();
    out = new FileOutputStream(args[0]+"o.zip");
    int bufferSize = blob.getBufferSize();
    byte[] buffer = new byte[bufferSize];
    int bytesRead = 0;
    while ((bytesRead = in.read(buffer)) != -1) {
    out.write(buffer, 0, bytesRead);
    System.out.println(bytesRead);
    }
                            stmt.clearParameters();
    buffer = null;
    in.close();
    out.close();
    }
    }
    catch(SQLException ex) {
    ex.printStackTrace(); }
    catch(Exception e) {
    e.printStackTrace(); } finally {
    try {

    conn.commit();
    stmt.close();
    conn.close();
    in = null;
    blob = null;
    rs = null;
    out = null;
    conn = null;
    stmt = null;
    }
    catch(SQLException e) { }
    }  }
    }
      

  2.   

    看不清楚的话我mail给你
    [email protected]
      

  3.   

    public static void FileToDB(String filename,String table,String field,Connection con) throws Exception
      {
        if(filename==null||table==null||field==null) throw new Exception("错误:参数值不能为空。");    File file=new File(filename);
        if(!file.exists()||!file.isFile()) throw new Exception("文件不存在!");    if(con==null) throw new Exception("错误:数据库连接无效。");    PreparedStatement ps=null;
        try
        {
          ps=con.prepareStatement("insert into "+table+"("+field+") values(?)");
          System.out.println("start write:");      ps.setBinaryStream(1,new FileInputStream(file),(int)file.length());
          ps.execute();
          System.out.println("end write:");
        }
        catch (Exception e)
        { e.printStackTrace();
          throw e;
        }
        finally
        {
          if(ps!=null) try {ps.close();} catch(Exception e) {}
        }
      }
      

  4.   


    Ryan76(秋南)      真的好感谢你啊,但是我是在是太菜了,学java才一周,并且我用的是SQLSERVER2000数据库,不是Oracle  ,所以我照抄的时候,抄到:
    fout = blob.getBinaryOutputStream(); ------》这里没有getBinaryOutputSt
    ream();我换成了setBinaryStream( 0)行么?
    File f = new File(args[1]);
    fin = new FileInputStream(f);
    byte[] buffer = new byte
    [blob.getBufferSize()];------》这里没有getBufferSize方法,用length()也不行 我该在那么办啊?javahui(阶级斗争要年年讲,月月讲,天天讲。)大大你的方法我正在试,好像是什么
    java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]第 1 行: '@P1' 附近有语法错误。
    我正在调试