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 {

  • 相关阅读:
    异常
    带参数的方法
    变量,基本类型,数据类型和运算符
    数据类型转换(针对数字类型)
    this关键字
    面向对象DAO模式
    常见类 Object
    方法和包
    final关键字
    abstract关键字
  • 原文地址:https://www.cnblogs.com/fwjybfq/p/7003442.html
Copyright © 2011-2022 走看看