Java:从 Oracle 读取 Blob [英] Java: Reading Blob from Oracle

查看:20
本文介绍了Java:从 Oracle 读取 Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以将 Treemap 转换为字节并将它们存储在数据库 (Oracle 11g) 中.现在该存储似乎工作正常.我现在想检索地图,但它在 blob 字段中以字节为单位.如何检索和重新构建地图?

I have this code that converts a Treemap into bytes and store them in database (Oracle 11g). Now that storage seems to be working fine. I want to retrieve the map now, but it is in bytes in blob field. How can I retrieve and re-construct the map?

存储地图的代码是:

 public void StoreMapDB(TreeMap<DateTime, Integer> map) throws
        IOException, FileNotFoundException{

       try {
          Connection con = null;

          Class.forName("oracle.jdbc.driver.OracleDriver");
          con=DriverManager.getConnection(
            "jdbc:oracle:thin:@dsds",
            "dsdsd",
            "XXdsdsX");
          con.setAutoCommit(false);
          ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
          ObjectOutputStream out = new ObjectOutputStream(bos);
          out = new ObjectOutputStream(bos) ;
          out.writeObject(map);
          out.close();

          byte[] buf = bos.toByteArray();
          PreparedStatement prepareStatement = con.prepareStatement("INSERT INTO 
           SMD_DATESTREEMAP VALUES(?,?)");
          prepareStatement.setLong(1, 2);
          prepareStatement.setBinaryStream(2, new ByteArrayInputStream(buf),
           buf.length);
          prepareStatement.executeUpdate();


         // insertMap.executeUpdate();
          con.commit();
    } catch(Exception e){
        System.err.print(e);
    }
}

附言我编辑了这段代码,但认为它不起作用,因为它将检索到的地图的大小显示为 0,而它应该是 366.

P.S. I edited this code but don't think it works because it displays size of retrieved map as 0 where it should be 366.

  public TreeMap<DateTime, Integer> retrieveMapDB()throws IOException,
        SQLException{

    try {
        Connection con = null;

        Class.forName("oracle.jdbc.driver.OracleDriver");
        con=DriverManager.getConnection(
            "jdbc:oracle:thin:@oradbfdfdt05:f:fdfd",
            "cxcx",
            "hpdbcxcxsmb");
        con.setAutoCommit(false);
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        String query = "SELECT TREEMAP FROM SMD_DATESTREEMAP WHERE id = ?";
        try {
            pstmt = con.prepareStatement(query);
            int id = 1;
            pstmt.setInt(1, id);

            rs = pstmt.executeQuery();
            while(rs.next()){
                ByteArrayInputStream bos = new 
           ByteArrayInputStream(rs.getBytes("TREEMAP")) ;
                ObjectInputStream out = new ObjectInputStream(bos);
                retrievedmap=(TreeMap<DateTime, Integer>)out.readObject();
            }


        }catch(IOException ioe){
            System.err.print(ioe);
        }
    }catch(ClassNotFoundException cnfe){
        System.err.print(cnfe);
    }
  return retrievedmap;
}

推荐答案

您可以通过 ResultSet.getBinaryStream() 方法获取 InputStream 对象.

You may obtain InputStream object via ResultSet.getBinaryStream() method.

PreparedStatement prepareStatement = con.prepareStatement("select * from SMD_DATESTREEMAP");
ResultSet rs=prepareStatement.executeQuery();
      
while(rs.next())
{
   oracle.jdbc.driver.OracleBlobInputStream bos = (oracle.jdbc.driver.OracleBlobInputStream) rs.getBinaryStream(2) ;
       
   ObjectInputStream out = new ObjectInputStream(bos);
      
   map = (TreeMap<DateTime, Integer>) out.readObject();
   ...
}

您可以编写字节数组而不是二进制流.

You may write byte array instead of binary stream.

ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(map);
out.flush();
out.close();
byte[] buf = bos.toByteArray();
      
PreparedStatement prepareStatement = con.prepareStatement("INSERT INTO SMD_DATESTREEMAP VALUES(?,?)");
prepareStatement.setInt(1, 1);
prepareStatement.setBytes(2, buf);
prepareStatement.executeUpdate();
prepareStatement.close();

并读取字节数组:

while(rs.next())
{ 
    byte []buf=rs.getBytes(2);
    ByteArrayInputStream bos=new ByteArrayInputStream(buf);
    ObjectInputStream out = new ObjectInputStream(bos);
    map=(TreeMap<DateTime, Integer>)out.readObject();
    ..
}

这篇关于Java:从 Oracle 读取 Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆