如题。
代码如下:
package demo;
import java.sql.*;
import java.io.*;
import oracle.sql.BLOB;
import oracle.jdbc.*;
import com.database.DbDAO;
public class Orablob extends DbDAO{ private static Orablob inform;
public static synchronized Orablob getInstance() 
{
if (inform == null)
inform = new Orablob();
return inform;
}

  public void insertblob()
  {
  try
  {
//首先是将文件输入到数据库。
    Class.forName("oracle.jdbc.driver.OracleDriver");//注册数据库引擎。
    Connection conn= DriverManager.getConnection("dburl=jdbc:oracle:thin:@10.1.92.100:1521:oradb", "DEW", "MARBLE");//建立连接串
    conn.setAutoCommit(false);//关闭自动提交,以提高性能。
    Statement stmt=conn.createStatement();//建立会话
   
    File file=new File("D:\\laod-balancing.doc");
    InputStream is=new FileInputStream(file);//创建输入流,将外部文件输入到InputStream 中。
    System.out.print(file.length());
    
    byte[] blobByte=new byte[is.available()];    is.read(blobByte);
    is.close();
    
    PreparedStatement pstmt=conn.prepareStatement("INSERT INTO DEW.T_FILE  VALUES (?,?)");
    pstmt.setString(1,"1230110");
    pstmt.setBinaryStream(2,(new ByteArrayInputStream(blobByte)), blobByte.length);
   
    pstmt.executeUpdate();//将文件流插入到数据库中。
    System.out.print("测试"); //执行到这里就不行了;
    pstmt.close();    System.out.print("以下是从库中读取文件。");
    ResultSet rset=stmt.executeQuery("SELECT * from DEW.T_FILE");
    while (rset.next())
      {// Get LOB locators into Java wrapper classes.
        BLOB blob=((OracleResultSet)rset).getBLOB(2);//获取文件字段。
        // int chunk=blob.getChunkSize();
       int chunk=blob.getChunkSize();
      byte [] buffer=new byte[chunk];
        System.out.println(chunk);        File binaryFile=new File("D:\\test.doc");
        FileOutputStream fileoutstream=new FileOutputStream(binaryFile);//创建文件输出流。
        InputStream instream=blob.getBinaryStream();//建立输入流,并将字段comment的值以流的形式,放入instream变量。
        instream.read(buffer,0,chunk);//将文件流存入变量buffer,以buffer为中转
        fileoutstream.write(buffer,0,chunk);//将buffer写入文件输出流。
        System.out.println("write ok!");
        instream.close();//关闭流
        fileoutstream.close();
      }
    rset.close();
    stmt.close();
    conn.close();
    System.out.print("ok");
  }
  catch(Exception ee)
  {
      System.out.println(ee.getMessage());
  }
  }
      public static void main(String args[])
    {Orablob test=getInstance() ;
     test.insertblob();
}}
输出结果很简单:6489 Io 异常: Connection reset
6489是文件长度。

解决方案 »

  1.   

    lz的连接url好象不对。修改如下试试:
    Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@10.1.92.100:1521:oradb", "DEW", "MARBLE");//建立连接串
      

  2.   

    按照如javaboy2006所说修改,还是一样的错误。
      

  3.   

    这个代码我也是从网上拷贝的,照理应该对的。当执行到要插入的时候,就出现 Io 异常: Connection reset
      

  4.   

    Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@10.1.92.100:1521:oradb", "DEW", "MARBLE");//建立连接串lz不是连的本机上的Oracle吗?
      

  5.   

    lz先试着执行一个select查询语句看能不能返回ResultSet。
      

  6.   

    数据库开始的时候是没问题的,就是在要insert的时候就断开了
      

  7.   

    PreparedStatement pstmt=conn.prepareStatement("INSERT INTO DEW.T_FILE VALUES (?,?)");
    pstmt.setString(1,"1230110");
    pstmt.setBinaryStream(2,(new ByteArrayInputStream(blobByte)), blobByte.length);pstmt.executeUpdate();//将文件流插入到数据库中。
    System.out.print("测试"); //执行到这里就不行了;你这是sqlserver的写法,oracle跟这个写法有十万八千里的差别,自己google一下吧
      

  8.   

    import   java.io.*;   
    import   java.sql.*;   
        
        
      public   class   BlobOperation   
      {   
      public   static   void   addLob(long   id,   String   binFile)   throws   SQLException   
      {   
      Connection   con   =   null;   
      PreparedStatement   ps   =   null;   
      ResultSet   rs   =   null;   
        
      try   
      {   
      con   =   ConnectionFactory.getConnection();   //换成你自己取连接的方法   
      con.setAutoCommit(false);   
        
      String   sql   =   "INSERT   INTO   Blob_Tbl(id,   binfile,   bincontent)";   
      sql   +=   "   VALUES(?,   ?,   ?)";   
      ps   =   con.prepareStatement(sql);   
        
      ps.setLong(1,   id);   
      ps.setString(2,   binFile);   
      ps.setBlob(3,   oracle.sql.BLOB.empty_lob());   
        
      ps.executeUpdate();   
      //DatabaseUtils.closeObject(ps);   
        
      ps   =   con.prepareStatement("SELECT   bincontent   FROM   Blob_Tbl   WHERE   id   =   "   +   id   +   "   for   update   ");   
      rs   =   ps.executeQuery();   
        
      if   (rs.next())   
      {   
      oracle.sql.BLOB   binContent   =   (oracle.sql.BLOB)   rs.getBlob(1);   
        
      /*   write   blob   content   */   
      OutputStream   binOut   =   binContent.getBinaryOutputStream();   
      BufferedOutputStream   out   =   new   BufferedOutputStream(binOut);   
      BufferedInputStream   in   =   new   BufferedInputStream(new   FileInputStream(binFile));   
      int   c;   
      while   ((c   =   in.read())   !=   -1)   
      {   
      out.write(c);   
      }   
      in.close();   
      out.close();   
      }   
      con.commit();   
      }   catch   (Exception   e)   
      {   
      e.printStackTrace();   
      try   
      {   
      con.rollback();   
      }   catch   (SQLException   se)   
      {   
      }   
      throw   new   SQLException(e.getMessage());   
      }   finally   
      {   
      DatabaseUtils.closeObject(rs,   ps,   con);   
      }   
      }   
        
      public   static   void   fetchLob(long   id,   String   filename)   throws   SQLException   
      {   
      Connection   con   =   null;   
      Statement   st   =   null;   
      ResultSet   rs   =   null;   
        
      try   
      {   
      con   =   ConnectionFactory.getConnection();   
        
      String   sql   =   "SELECT   *     From   Blob_Tbl   Where   id   =   "   +   id;   
      st   =   con.createStatement();   
        
      rs   =   st.executeQuery(sql);   
      while   (rs.next())   
      {   
        
      String   binFile   =   rs.getString("binfile");   
      oracle.sql.BLOB   binContent   =   (oracle.sql.BLOB)   rs.getBlob("bincontent");   
        
      /*   read   blob   content   */   
      BufferedOutputStream   out   =   new   BufferedOutputStream(new   FileOutputStream(filename));   
      BufferedInputStream   in   =   new   BufferedInputStream(binContent.getBinaryStream());   
        
      int   c;   
      while   ((c   =   in.read())   !=   -1)   
      {   
      out.write(c);   
      }   
      in.close();   
      out.close();   
      }   
        
      }   catch   (Exception   e)   
      {   
      throw   new   SQLException(e.getMessage());   
      }   finally   
      {   
      DatabaseUtils.closeObject(rs,   st,   con);   
      }   
      }   
        
      public   static   void   main(String[]   args)   throws   Exception   
      {   
      if   (args.length   ==   0)   
      {   
      addLob(1,   "a.jpg");   
      }   else   
      {   
      fetchLob(1,   args[0]);   
      }   
      }   
      }   
        
      一个例子,有的地方你需要自己改下