2011-12-09 10 views
0

私はTreemapをバイトに変換し、データベース(Oracle 11g)に格納するこのコードを持っています。今はストレージが正常に動作しているようです。現在マップを取得したいが、BLOBフィールドのバイト数である。マップを取得して再構築するにはどうすればよいですか?Java:OracleからBLOBを読み取る

マップを格納するためのコードは次のとおり

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); 
    } 
} 

P.S.私はこのコードを編集したが、それはそれはそれは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; 
} 

答えて

2

する必要があります0として取得マップのサイズを表示しますので、あなたがResultSet.getBinaryStream()メソッドを介してInputStreamオブジェクトを得ることができる作品とは思いません。

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(); 
     ... 
     } 

バイナリストリームの代わりにバイト配列を書き込むことができます。

 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(); 

とバイト配列を読み:大幅にも、これをサポートするためのAVDへ

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

私はoracle.jdbc.driveを試しました...しかしdidntの仕事。それは公開されていないので、パッケージからアクセスできないと言いますか? –

+0

私はあなたの記事を読む前に、私は上記の質問を修正しましたが、うまくいきませんでした。理由は何ですか? –

+0

メソッドに配置されている場合、バイト配列を読み取るurコードはjava.sql.SQLExceptionを返します。ResultSet.nextが呼び出されていません –

1

おかげで私のコード/質問で最も働きました。

 Blob blob = rs.getBlob("col_blob"); 

    // Get the number bytes in the BLOB 
    long blobLength = blob.length(); 

    // Get bytes from the BLOB in a byte array 
    int pos = 1; // position is 1-based 
    int len = 10; 
    byte[] bytes = blob.getBytes(pos, len); 

ホープ、これは参考になると誰もが明確化が必要な場合、私は幸せ以上になります。だから、答えは...ここにBLOB from Oracle

そして、AVDの答えと補数で働いていたスニペットで、あります:D

関連する問題