Caching Data for Use When Disconnected - Java JDBC

Java examples for JDBC:CachedRowSet

Introduction

Use a CachedRowSet object to store the data to work with while offline.

Demo Code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.spi.SyncProviderException;

public class Main {
  static Connection conn = null;
  static CachedRowSet crs = null;
  public static void main(String[] args) {
    try {// ww  w.  j av  a 2 s.  c  om
      conn = getConnection();
      queryWithRowSet();
      updateData();
      syncWithDatabase();
    } catch (java.sql.SQLException ex) {
      System.out.println(ex);
    } finally {
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
    }
  }

  public static void syncWithDatabase() {
    try {
      crs.acceptChanges(conn);
    } catch (SyncProviderException ex) {
      ex.printStackTrace();
    } finally {
      if (crs != null) {
        try {
          crs.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
    }
  }

  public static void queryWithRowSet() {
    RowSetFactory factory;
    try {
      factory = RowSetProvider.newFactory();
      // Create a CachedRowSet object using the factory
      crs = factory.createCachedRowSet();
      // populate the CachedRowSet connection settings
      // crs.setUsername(createConn.getUsername());
      // crs.setPassword(createConn.getPassword());
      // crs.setUrl(createConn.getJdbcUrl());

      crs.setCommand("select id, id, recipe_name, description from recipes");

      int[] keys = { 1 };
      crs.setKeyColumns(keys);
      crs.execute(conn);

      while (crs.next()) {
        System.out.println(crs.getString(2) + ": " + crs.getString(3) + " - "
            + crs.getString(4));
      }

    } catch (SQLException ex) {
      ex.printStackTrace();
    }
  }

  public static boolean updateData() {
    boolean returnValue = false;
    try {
      // Move to the position before the first row in the result set
      crs.beforeFirst();
      while (crs.next()) {
        if (crs.getString("id").equals("1")) {
          crs.updateString("description", "test");
          crs.updateRow();
        }
      }
      returnValue = true;
      // Move to the position before the first row in the result set
      crs.beforeFirst();
      while (crs.next()) {
        System.out.println(crs.getString(2) + ": " + crs.getString(3) + " - "
            + crs.getString(4));
      }
    } catch (SQLException ex) {
      returnValue = false;
      ex.printStackTrace();
    }
    return returnValue;
  }

  public static Connection getConnection() throws SQLException {
    Connection conn = null;
    String hostname = null;
    String port = null;
    String database = null;
    String username = null;
    String password = null;
    String driver = null;
    String jdbcUrl;
    if (driver.equals("derby")) {
      jdbcUrl = "jdbc:derby://" + hostname + ":" + port + "/" + database;
    } else {
      jdbcUrl = "jdbc:oracle:thin:@" + hostname + ":" + port + ":" + database;
    }
    conn = DriverManager.getConnection(jdbcUrl, username, password);
    System.out.println("Successfully connected");
    return conn;
  }
}

Related Tutorials