zoukankan      html  css  js  c++  java
  • Storing Java objects in MySQL blobs

    Storing Java objects in MySQL blobs
    It's easy to store any serializable object in a blob in a MySQL database, if you use a PreparedStatement:

    String query="replace into mysession (personid,data) values (?,?)";
    PreparedStatement statement=conn.prepareStatement(query);
    statement.setInt(1, ses.getPersonid());
    statement.setObject(2, (Object)ses);
    statement.execute();
    When you try to retrieve the object, it gets work. It's supposed to work like this, using getObject():

    query="select data from mysession where personid=? and time_to_sec(timediff(now(), datum)) < 600";
    statement=conn.prepareStatement(query);
    statement.setInt(1, personid);
    resultSet = statement.executeQuery();
    if (resultSet.next()) {
    Object x=resultSet.getObject(1);
    ses = (MySession)x;
    } else {
    However, the responsibility for deserializing the blob so that getObject() succeeds, lies with the database driver, and MySQL doesn't do this automatically.

    You do get an Object back, and everything looks fine, but the class name of the object (read with o.getClass().toString()) is just garbage, containing stuff like "[B", and when you try to cast it to your own class you get a classCastException.

    To make the MySQL driver deserialize blobs, you are supposed to add a parameter, "autoDeserialize=true", to your JDBC URL.

    Unfortunately, the MySQL driver appears to exist in its own context, oblivious to any user-defined classes. Making your own classes globally available is usually not a good idea, so instead you can use an ObjectInputStream:

    query="select data from mysession where personid=? and time_to_sec(timediff(now(), datum)) < 600";
    statement=conn.prepareStatement(query);
    statement.setInt(1, personid);
    resultSet = statement.executeQuery();
    if (resultSet.next()) {
    InputStream is = resultSet.getBlob("data").getBinaryStream();
    ObjectInputStream ois = new ObjectInputStream(is);
    Object x = ois.readObject();
    ses = (MySession)x;
    } else {

  • 相关阅读:
    POJ 1182 食物链
    HDU 1385 Minimum Transport Cost
    HDU_2544 最短路
    HDU 2066 一个人的旅行
    poj3090
    poj3157
    poj3169
    poj3125
    poj3187
    VMware Workstation 8的简明使用教程
  • 原文地址:https://www.cnblogs.com/fwjybfq/p/7003442.html
Copyright © 2011-2022 走看看