sql >> Database teknologi >  >> RDS >> Oracle

Java:Læser Blob fra Oracle

Du kan hente InputStream-objekt via ResultSet.getBinaryStream() metode.

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

Du kan skrive byte-array i stedet for binær 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();

og læs byte-array:

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


  1. Trimning af transaktionsloggen fedt

  2. T-SQL for at finde Remote Server Name på den linkede server

  3. Tuning af SQL Server Reporting Services

  4. Et dybdegående kig på databaseindeksering