ResultSet: getObject(int columnIndex) : ResultSet « java.sql « Java by API






ResultSet: getObject(int columnIndex)

 
/*
 * mysql> CREATE TABLE java_objects ( 
 * id INT AUTO_INCREMENT, 
 * name varchar(128), 
 * object_value BLOB, 
 * primary key (id));
 **/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main {
  static final String WRITE_OBJECT_SQL = "INSERT INTO java_objects(name, object_value) VALUES (?, ?)";

  static final String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE id = ?";

  public static Connection getConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/databaseName";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static long writeJavaObject(Connection conn, Object object) throws Exception {
    String className = object.getClass().getName();
    PreparedStatement pstmt = conn.prepareStatement(WRITE_OBJECT_SQL);
    pstmt.setString(1, className);
    pstmt.setObject(2, object);
    pstmt.executeUpdate();
    ResultSet rs = pstmt.getGeneratedKeys();
    int id = -1;
    if (rs.next()) {
      id = rs.getInt(1);
    }
    rs.close();
    pstmt.close();
    return id;
  }

  public static Object readJavaObject(Connection conn, long id) throws Exception {
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    Object object = rs.getObject(1);
    String className = object.getClass().getName();
    rs.close();
    pstmt.close();
    return object;
  }
  public static void main(String args[])throws Exception {
    Connection conn = null;
    try {
      conn = getConnection();
      System.out.println("conn=" + conn);
      conn.setAutoCommit(false);
      List<Object> list = new ArrayList<Object>();
      list.add("This is a short string.");
      list.add(new Integer(1234));
      list.add(new Date());

      long objectID = writeJavaObject(conn, list);
      conn.commit();
      System.out.println("Serialized objectID => " + objectID);
      List listFromDatabase = (List) readJavaObject(conn, objectID);
      System.out.println("[After De-Serialization] list=" + listFromDatabase);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      conn.close();
    }
  }
}

   
  








Related examples in the same category

1.ResultSet.CLOSE_CURSORS_AT_COMMIT
2.ResultSet.CONCUR_READ_ONLY
3.ResultSet.CONCUR_UPDATABLE
4.ResultSet.HOLD_CURSORS_OVER_COMMIT
5.ResultSet.TYPE_SCROLL_INSENSITIVE
6.ResultSet.TYPE_SCROLL_SENSITIVE
7.ResultSet: TYPE_FORWARD_ONLY
8.ResultSet: absolute(int row)
9.ResultSet: beforeFirst()
10.ResultSet: cancelRowUpdates()
11.ResultSet: close()
12.ResultSet: deleteRow()
13.ResultSet: first()
14.ResultSet.getAsciiStream(int columnIndex)
15.ResultSet: getBinaryStream(int columnIndex)
16.ResultSet: getBigDecimal(int columnIndex)
17.ResultSet: getBlob(int columnIndex)
18.ResultSet: getBlob(String colName)
19.ResultSet: getBytes(int columnIndex)
20.ResultSet: getClob(int columnIndex)
21.ResultSet: getConcurrency()
22.ResultSet: getDate(int columnIndex)
23.ResultSet: getDouble(String columnLabel)
24.ResultSet: getInt(int columnIndex)
25.ResultSet: getInt(String columnName)
26.ResultSet: getMetaData()
27.ResultSet: getObject(String columnLabel)
28.ResultSet: getRef(int columnIndex)
29.ResultSet: getRow()
30.ResultSet: getShort(String columnLabel)
31.ResultSet: getString(int columnIndex)
32.ResultSet: getString(String columnLabel)
33.ResultSet: getTime(int columnIndex)
34.ResultSet: getTimestamp(int columnIndex)
35.ResultSet: getType()
36.ResultSet: getWarnings()
37.ResultSet: insertRow()
38.ResultSet: isAfterLast()
39.ResultSet: isBeforeFirst()
40.ResultSet: isFirst()
41.ResultSet: last()
42.ResultSet: moveToInsertRow()
43.ResultSet: next()
44.ResultSet: previous()
45.ResultSet: refreshRow()
46.ResultSet: relative(int rows)
47.ResultSet: setFetchSize(int rows)
48.ResultSet: updateInt(String columnLabel, int x)
49.ResultSet: updateRow()
50.ResultSet: updateDouble(int columnIndex, double x)
51.ResultSet: updateString(String columnName, String x)
52.ResultSet: wasNull()