Java Oracle:将TreeMap存储为BLOB?

sys*_*bug 1 java oracle oracle11g

我有以下代码:

  public void StoreMapInDB(TreeMap<DateTime, Integer> map) throws
        IOException, FileNotFoundException{
    try {
  PreparedStatement insertMap = null;
  String insertString = "INSERT INTO TESTMAP(ID, NAME) VALUES (1, ?)";
  Connection con=null;
  con.setAutoCommit(false);
  Class.forName("oracle.jdbc.driver.OracleDriver");
  con=DriverManager.getConnection(
    "jdbc:oracle:thin:@XXXXX",
    "XXX",
    "XXX");
    //This line is incorrect for sure 
    //insertMap.setBlob(1, map.);
    } catch(Exception e){e.printStackTrace();}
}
Run Code Online (Sandbox Code Playgroud)

连接工作,所有与数据库.这次我试图插入地图,即我创建的树形图到表格中BLOB类型的列.我怎样才能做到这一点?还有其他更好的数据类型我应该研究一下吗?

谢谢,

WeM*_*are 5

如果要将对象放入BLOB数据类型,可以执行以下操作:

// Serialize to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(object);
out.close();

// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
PreparedStatement prepareStatement = connection.prepareStatement("insert into tableName values(?,?)");
prepareStatement.setLong(1, id);
prepareStatement.setBinaryStream(2, new ByteArrayInputStream(buf), buf.length);
Run Code Online (Sandbox Code Playgroud)