Results 1 to 2 of 2
  1. #1
    kodjeff1 is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Error retrieving serialized data from database

    Hello all,

    I am trying to write a Vector of String's to my database, my write code seems to be working correctly (no exceptions being thrown):

    <code>
    Vector<String> vector = new Vector<String>();
    vector.add("one");
    vector.add("two");
    try {
    String insertionString = "INSERT INTO test SET testField = ?";
    PreparedStatement ps = dbConn.prepareStatement(insertionString);
    ps.setObject(1, vector);
    ps.executeUpdate();
    ps.close();
    System.out.println("Insert successful");
    } catch (SQLException e) {
    e.printStackTrace();
    }

    </code>

    However, when I try to read the data back out, I am getting many errors:

    java.sql.SQLException: Before start of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:927)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSet Impl.java:841)
    at com.mysql.jdbc.ResultSetImpl.getObject(ResultSetIm pl.java:4853)
    at Main.init(Main.java:57)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    My read code is as follows:
    <code>
    Statement st;
    try {
    st = dbConn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM test");
    Object obj = rs.getObject(1);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    </code>

    As I understand it, setObject and getObject should take care of all serialization (allowing me to avoid all the streaming mess).

    My database only has one column, and one row. The column type is BLOB.

    Does anyone see the problem?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    rs.next().

    A ResultSet is initially pointing before the first record (as the error says), so you have to use next() to get it to point to the first record.

    As to your general idea...no. setObject() has nothing to do with serialisation. You will need to use the object stream and setBinaryStream() to serialise it.

Similar Threads

  1. Retrieving data from database every 10 secs.
    By vnsuresh.abi in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 03-31-2011, 11:14 AM
  2. error in reading data from table in pidgree database
    By chethan_kc in forum Java Servlet
    Replies: 17
    Last Post: 02-18-2011, 02:11 PM
  3. Replies: 1
    Last Post: 03-19-2009, 07:38 AM
  4. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 04:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •