Java by API/java.sql/ResultSet — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:47, 31 мая 2010

Содержание

ResultSet: absolute(int row)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";
      ResultSet rs = stmt.executeQuery(sqlQuery);
      int rowSize = 0;
      while (rs.next()) {
        rowSize++;
      }
      System.out.println("Number of Rows in ResultSet is: " + rowSize);
      if (rowSize == 0) {
        System.out.println("Since there are no rows, exiting...");
        System.exit(0);
      }
      int cursorPosition = Math.round(rowSize / 2);
      System.out.println("Moving to position: " + cursorPosition);
      rs.absolute(cursorPosition);
      System.out.println("Name: " + rs.getString(2));
      rs.relative(-1);
      cursorPosition = rs.getRow();
      System.out.println("Moving to position: " + cursorPosition);
      System.out.println("Name: " + rs.getString(2));
      System.out.println("Moving to the first row");
      while (!rs.isFirst()) {
        rs.previous();
      }
      System.out.println("Name: " + rs.getString(2));
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: beforeFirst()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static Connection getConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/octopus";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = getConnection();
      String query = "select id, name from employees";
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      rs = stmt.executeQuery(query);
      while (rs.next()) {
        String id = rs.getString(1);
        String name = rs.getString(2);
        System.out.println("id=" + id + "  name=" + name);
      }
      rs.first();
      rs.deleteRow();
      rs.beforeFirst();
      while (rs.next()) {
        String id = rs.getString(1);
        String name = rs.getString(2);
        System.out.println("id=" + id + "  name=" + name);
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    } finally {
      try {
        rs.close();
        stmt.close();
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}





ResultSet: cancelRowUpdates()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);
    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    // Move cursor to the row to update
    resultSet.first();
    // Update the value of column col_string on that row
    resultSet.updateString("col_string", "new data");
    // Discard the update to the row
    resultSet.cancelRowUpdates();
  }
}





ResultSet: close()

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    String sqlQuery = "SELECT uid, name, duration from EVENTS";
    ResultSet rs = stmt.executeQuery(sqlQuery);
    while (rs.next()) {
      rs.updateString("Name", "new Name");
      rs.updateRow();
    }
    rs.first();
    while (rs.next()) {
      String name = rs.getString(2);
      Timestamp hireDate = rs.getTimestamp(5);
      System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }
    rs.close();
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





ResultSet.CLOSE_CURSORS_AT_COMMIT

 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    DatabaseMetaData dbMeta = conn.getMetaData();
    if (dbMeta.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
      System.out.println("this database hold cursors over commit");
    } else if (dbMeta.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
      System.out.println("this database close cursors at commit");
    }
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





ResultSet.CONCUR_READ_ONLY

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    int rsConcurrency = rs.getConcurrency();
    if (rsConcurrency == java.sql.ResultSet.CONCUR_READ_ONLY) {
      System.out.println("java.sql.ResultSet.CONCUR_READ_ONLY");
    } else if (rsConcurrency == java.sql.ResultSet.CONCUR_UPDATABLE) {
      System.out.println("java.sql.ResultSet.CONCUR_UPDATABLE");
    } else {
      // it is an error
    }
    rs.close();
    st.close();
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





ResultSet.CONCUR_UPDATABLE

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    String sqlQuery = "SELECT uid, name, duration from EVENTS";
    ResultSet rs = stmt.executeQuery(sqlQuery);
    while (rs.next()) {
      rs.updateString("Name", "new Name");
      rs.updateRow();
    }
    rs.first();
    while (rs.next()) {
      String name = rs.getString(2);
      Timestamp hireDate = rs.getTimestamp(5);
      System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }
    rs.close();
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





ResultSet: deleteRow()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      ResultSet rs = stmt.executeQuery("SELECT * from DUMMY");
      System.out.println(rs.getType());
      System.out.println(rs.getConcurrency());
      rs.deleteRow();
      rs.close();
      rs.close();
      stmt.close();
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: first()

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static Connection getConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/octopus";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = getConnection();
      String query = "select id, name from employees";
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      rs = stmt.executeQuery(query);
      while (rs.next()) {
        String id = rs.getString(1);
        String name = rs.getString(2);
        System.out.println("id=" + id + "  name=" + name);
      }
      rs.first();
      rs.deleteRow();
      rs.beforeFirst();
      while (rs.next()) {
        String id = rs.getString(1);
        String name = rs.getString(2);
        System.out.println("id=" + id + "  name=" + name);
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    } finally {
      try {
        rs.close();
        stmt.close();
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}





ResultSet.getAsciiStream(int columnIndex)

 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      FileInputStream fis = new FileInputStream("sometextfile.txt");
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement createTable = connection.createStatement();
      createTable.executeUpdate("CREATE TABLE source_code (name char(20), source LONGTEXT)");
      String ins = "INSERT INTO source_code VALUES(?,?)";
      PreparedStatement statement = connection.prepareStatement(ins);
      statement.setString(1, "TryInputStream2");
      statement.setAsciiStream(2, fis, fis.available());
      int rowsUpdated = statement.executeUpdate();
      System.out.println("Rows affected: " + rowsUpdated);
      Statement getCode = connection.createStatement();
      ResultSet theCode = getCode.executeQuery("SELECT name,source FROM source_code");
      BufferedReader reader = null;
      String input = null;
      while (theCode.next()) {
        reader = new BufferedReader(new InputStreamReader(theCode.getAsciiStream(2)));
        while ((input = reader.readLine()) != null) {
          System.out.println(input);
        }
      }
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: getBigDecimal(int columnIndex)

 
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM Employees");
    printColumnInfo(rs);
    printColumnNames(rs);
    processRs(rs);
    rs = stmt.executeQuery("SELECT * FROM Location");
    printColumnInfo(rs);
    printColumnNames(rs);
    processRs(rs);
    conn.close();
  }
  public static void processRs(ResultSet rs) throws SQLException {
    ResultSetMetaData rmd = rs.getMetaData();
    while (rs.next()) {
      for (int col = 1; col <= rmd.getColumnCount(); col++)
        getData(rs, rmd.getColumnType(col), col);
    }
  }
  public static void printColumnNames(ResultSet rs) throws SQLException {
    ResultSetMetaData rmd = rs.getMetaData();
    for (int col = 1; col <= rmd.getColumnCount(); col++)
      System.out.println(rmd.getColumnName(col) + " ");
  }
  public static void getData(ResultSet rs, int type, int colIdx) throws SQLException {
    switch (type) {
    case java.sql.Types.CHAR:
    case java.sql.Types.VARCHAR:
      System.out.println(rs.getString(colIdx));
      break;
    case java.sql.Types.INTEGER:
      int i = rs.getInt(colIdx);
      System.out.println(i);
      break;
    case java.sql.Types.NUMERIC:
      BigDecimal bd = rs.getBigDecimal(colIdx);
      System.out.println(bd.toString());
      break;
    case java.sql.Types.TIMESTAMP:
    case java.sql.Types.DATE:
      java.sql.Date d = rs.getDate(colIdx);
      System.out.println(d.toString());
      break;
    }
  }
  public static void printColumnInfo(ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int cols = rsmd.getColumnCount();
    for (int colIdx = 1; colIdx <= cols; colIdx++) {
      String name = rsmd.getColumnName(colIdx);
      int type = rsmd.getColumnType(colIdx);
      String typeName = rsmd.getColumnTypeName(colIdx);
      System.out.println(name + ", " + type + ", " + typeName);
    }
  }
}





ResultSet: getBinaryStream(int columnIndex)

 
import java.awt.Image;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.ResultSet;
public class Main {
  public static void main(String[] argv) throws Exception {
    ResultSet rset = null;
    InputStream stream = rset.getBinaryStream(1);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int a1 = stream.read();
    while (a1 >= 0) {
      output.write((char) a1);
      a1 = stream.read();
    }
    Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
    output.close();
  }
}





ResultSet: getBlob(int columnIndex)

 
import java.io.ObjectInputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.ImageIcon;
public class Main {
  public static void main(String[] args) throws Exception {
    ImageIcon image;
    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/c:\\employee");
    Statement s = con.createStatement();
    ResultSet rs = s.executeQuery("select photo from employee where name = "Duke"");
    if (rs.next()) {
      Blob photo = rs.getBlob(1);
      ObjectInputStream ois = null;
      ois = new ObjectInputStream(photo.getBinaryStream());
      image = (ImageIcon) ois.readObject();
    } 
    s.close();
  }
}





ResultSet: getBlob(String colName)

 
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JPanel;
public class BlobSelect extends JPanel {
  public static void main(String args[]) throws Exception {
    Connection conn = null;
    byte[] data = getBLOB(01, conn);
  }
  public static byte[] getBLOB(int id, Connection conn) throws Exception {
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {
      pstmt = conn.prepareStatement(query);
      pstmt.setInt(1, id);
      rs = pstmt.executeQuery();
      rs.next();
      Blob blob = rs.getBlob("photo");
      // materialize BLOB onto client
      return blob.getBytes(1, (int) blob.length());
    } finally {
      rs.close();
      pstmt.close();
      conn.close();
    }
  }
}





ResultSet: getBytes(int columnIndex)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("create table survey (id int, name BINARY );");
    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();
    pstmt.setBytes(1, binaryData);
    pstmt.executeUpdate();
    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
      System.out.print(rs.getBytes(2).length + "   ");
    }
    rs.close();
    stmt.close();
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





ResultSet: getClob(int columnIndex)

 
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Main{
  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 String getCLOB(int id) throws Exception {
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT clobData FROM tableName WHERE id = ?";
    try {
      conn = getConnection();
      pstmt = conn.prepareStatement(query);
      pstmt.setInt(1, id);
      rs = pstmt.executeQuery();
      rs.next();
      Clob clob = rs.getClob(1);
      // materialize CLOB onto client
      String wholeClob = clob.getSubString(1, (int) clob.length());
      return wholeClob;
    } finally {
      rs.close();
      pstmt.close();
      conn.close();
    }
  }
  public static void main(String args[]) throws Exception {
    System.out.println(getCLOB(01));
  }
}





ResultSet: getConcurrency()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      ResultSet rs = stmt.executeQuery("SELECT * from DUMMY");
      System.out.println(rs.getType());
      System.out.println(rs.getConcurrency());
      rs.deleteRow();
      rs.close();
      rs.close();
      stmt.close();
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: getDate(int columnIndex)

 
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM Employees");
    printColumnInfo(rs);
    printColumnNames(rs);
    processRs(rs);
    rs = stmt.executeQuery("SELECT * FROM Location");
    printColumnInfo(rs);
    printColumnNames(rs);
    processRs(rs);
    conn.close();
  }
  public static void processRs(ResultSet rs) throws SQLException {
    ResultSetMetaData rmd = rs.getMetaData();
    while (rs.next()) {
      for (int col = 1; col <= rmd.getColumnCount(); col++)
        getData(rs, rmd.getColumnType(col), col);
    }
  }
  public static void printColumnNames(ResultSet rs) throws SQLException {
    ResultSetMetaData rmd = rs.getMetaData();
    for (int col = 1; col <= rmd.getColumnCount(); col++)
      System.out.println(rmd.getColumnName(col) + " ");
  }
  public static void getData(ResultSet rs, int type, int colIdx) throws SQLException {
    switch (type) {
    case java.sql.Types.CHAR:
    case java.sql.Types.VARCHAR:
      System.out.println(rs.getString(colIdx));
      break;
    case java.sql.Types.INTEGER:
      int i = rs.getInt(colIdx);
      System.out.println(i);
      break;
    case java.sql.Types.NUMERIC:
      BigDecimal bd = rs.getBigDecimal(colIdx);
      System.out.println(bd.toString());
      break;
    case java.sql.Types.TIMESTAMP:
    case java.sql.Types.DATE:
      java.sql.Date d = rs.getDate(colIdx);
      System.out.println(d.toString());
      break;
    }
  }
  public static void printColumnInfo(ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int cols = rsmd.getColumnCount();
    for (int colIdx = 1; colIdx <= cols; colIdx++) {
      String name = rsmd.getColumnName(colIdx);
      int type = rsmd.getColumnType(colIdx);
      String typeName = rsmd.getColumnTypeName(colIdx);
      System.out.println(name + ", " + type + ", " + typeName);
    }
  }
}





ResultSet: getDouble(String columnLabel)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);
    rs.beforeFirst();
    while (rs.next()) {
      double newSalary = rs.getDouble("salary") * 1.053;
      rs.updateDouble("salary", newSalary);
      rs.updateRow();
    }
    printRs(rs);
    conn.close();
  }
  public static void printRs(ResultSet rs) throws SQLException {
    rs.beforeFirst();
    while (rs.next()) {
      int ssn = rs.getInt("ssn");
      String name = rs.getString("name");
      double salary = rs.getDouble("salary");
      System.out.print("Row Number=" + rs.getRow());
      System.out.print(", SSN: " + ssn);
      System.out.print(", Name: " + name);
      System.out.println(", Salary: $" + salary);
    }
    System.out.println();
  }
}





ResultSet: getInt(int columnIndex)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,"Tom")");
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
      int id = rs.getInt(1); // index 1 is the "id" column
      String name = rs.getString(2); // index 2 is the "name" column
      System.out.println(id);
      System.out.println(name);
    }
    rs.close();
    st.close();
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





ResultSet: getInt(String columnName)

 
import javax.sql.rowset.CachedRowSet;
public class Main {
  
  public static void main(String[] args) throws Exception {
    CachedRowSet rs;
    String ROWSET_IMPL_CLASS = "com.sun.rowset.CachedRowSetImpl";
    
    
    Class c = Class.forName(ROWSET_IMPL_CLASS);
    rs = (CachedRowSet) c.newInstance();
    rs.setUrl("jdbc:postgresql:dbname");
    rs.setUsername("username");
    rs.setPassword("password");
    rs.setCommand("select * from members where name like ?");
    rs.setString(1, "I%");
    rs.execute();
    while (rs.next()) {
      if (rs.getInt("id") == 42) {
        rs.setString(1, "newString");
        rs.updateRow(); // Normal JDBC
        rs.acceptChanges();
      }
    }
    rs.close();
  }
}





ResultSet: getMetaData()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
   
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);
    rs.close();
    st.close();
    conn.close();
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





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.rumit();
      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();
    }
  }
}





ResultSet: getObject(String columnLabel)

 
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;
import java.sql.Statement;
import java.text.NumberFormat;
import java.util.Map;
class Employee implements SQLData {
  public BigDecimal SSN;
  public String FirstName;
  public String LastName;
  public BigDecimal Salary;
  private String sqlUdt;
  public void writeSQL(SQLOutput stream) throws SQLException {
    stream.writeBigDecimal(SSN);
    stream.writeString(FirstName);
    stream.writeString(LastName);
    stream.writeBigDecimal(Salary);
  }
  public String getSQLTypeName() throws SQLException {
    return sqlUdt;
  }
  public void readSQL(SQLInput stream, String typeName) throws SQLException {
    sqlUdt = typeName;
    SSN = stream.readBigDecimal();
    FirstName = stream.readString();
    LastName = stream.readString();
    Salary = stream.readBigDecimal();
  }
  public String calcMonthlySalary() {
    double monthlySalary = Salary.doubleValue() / 12;
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    String str = nf.format(monthlySalary);
    return str;
  }
}
public class Main {
  public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myserver:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    Map map = conn.getTypeMap();
    map.put("EMP_DATA", Class.forName("Employee"));
    conn.setTypeMap(map);
    ResultSet rs = stmt.executeQuery("SELECT * from Emp");
    Employee employee;
    while (rs.next()) {
      int empId = rs.getInt("EmpId");
      employee = (Employee) rs.getObject("Emp_Info");
      System.out.print("Employee Id: " + empId + ", SSN: " + employee.SSN);
      System.out.print(", Name: " + employee.FirstName + " " + employee.LastName);
      System.out.println(", Yearly Salary: $" + employee.Salary + " Monthly Salary: "
          + employee.calcMonthlySalary());
    }
    conn.close();
  }
}





ResultSet: getRef(int columnIndex)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Main {
  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 void main(String[] args) throws Exception {
    String deptName = "oldName";
    String newDeptName = "newName";
    ResultSet rs = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    PreparedStatement pstmt2 = null;
    try {
      conn = getConnection();
      // prepare query for getting a REF object and PrepareStatement object
      String refQuery = "select manager from dept_table where dept_name=?";
      pstmt = conn.prepareStatement(refQuery);
      pstmt.setString(1, deptName);
      rs = pstmt.executeQuery();
      java.sql.Ref ref = null;
      if (rs.next()) {
        ref = rs.getRef(1);
      }
      if (ref == null) {
        System.out.println("error: could not get a reference for manager.");
        System.exit(1);
      }
      String query = "INSERT INTO dept_table(dept_name, manager)values(?, ?)";
      pstmt2 = conn.prepareStatement(query);
      pstmt2.setString(1, newDeptName);
      pstmt2.setRef(2, ref);
      // execute query, and return number of rows created
      int rowCount = pstmt2.executeUpdate();
      System.out.println("rowCount=" + rowCount);
    } finally {
      pstmt.close();
      pstmt2.close();
      conn.close();
    }
  }
}





ResultSet: getRow()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    Connection con;
    Statement stmt;
    ResultSet uprs;
    try {
      Class.forName(driver);
      con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student","student");
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      uprs = stmt.executeQuery("SELECT * FROM Records");
      // Check the column count
      ResultSetMetaData md = uprs.getMetaData();
      System.out.println("Resultset has " + md.getColumnCount() + " cols.");
      int rowNum = uprs.getRow();
      System.out.println("row1 " + rowNum);
      uprs.absolute(1);
      rowNum = uprs.getRow();
      System.out.println("row2 " + rowNum);
      uprs.next();
      uprs.moveToInsertRow();
      uprs.updateInt(1, 150);
      uprs.updateString(2, "Madonna");
      uprs.updateString(3, "Dummy");
      uprs.updateString(4, "Jazz");
      uprs.updateString(5, "Image");
      uprs.updateInt(6, 5);
      uprs.updateDouble(7, 5);
      uprs.updateInt(8, 15);
      uprs.insertRow();
      uprs.close();
      stmt.close();
      con.close();
    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
  }
}





ResultSet: getShort(String columnLabel)

 

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    ResultSet rs = null;
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTypeInfo();
    while (rs.next()) {
      // Get the database-specific type name
      String typeName = rs.getString("TYPE_NAME");
      // Get the java.sql.Types type to which this
      // database-specific type is mapped
      short dataType = rs.getShort("DATA_TYPE");
      // Get the name of the java.sql.Types value.
      System.out.println("type name="+typeName);
      System.out.println("dataType="+dataType);
      System.out.println("jdbcType="+dataType);
    }   
    st.close();
    conn.close();
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}





ResultSet: getString(int columnIndex)

 
/*
Driver Loaded.
Got Connection.
SYSTEM_ALIASES
SYSTEM_ALLTYPEINFO
SYSTEM_AUTHORIZATIONS
SYSTEM_BESTROWIDENTIFIER
SYSTEM_CACHEINFO
SYSTEM_CATALOGS
SYSTEM_CHECK_COLUMN_USAGE
SYSTEM_CHECK_CONSTRAINTS
SYSTEM_CHECK_ROUTINE_USAGE
SYSTEM_CHECK_TABLE_USAGE
SYSTEM_CLASSPRIVILEGES
SYSTEM_COLLATIONS
SYSTEM_COLUMNPRIVILEGES
SYSTEM_COLUMNS
SYSTEM_CROSSREFERENCE
SYSTEM_INDEXINFO
SYSTEM_PRIMARYKEYS
SYSTEM_PROCEDURECOLUMNS
SYSTEM_PROCEDURES
SYSTEM_PROPERTIES
SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS
SYSTEM_SCHEMAS
SYSTEM_SCHEMATA
SYSTEM_SEQUENCES
SYSTEM_SESSIONINFO
SYSTEM_SESSIONS
SYSTEM_SUPERTABLES
SYSTEM_SUPERTYPES
SYSTEM_TABLEPRIVILEGES
SYSTEM_TABLES
SYSTEM_TABLETYPES
SYSTEM_TABLE_CONSTRAINTS
SYSTEM_TEXTTABLES
SYSTEM_TRIGGERCOLUMNS
SYSTEM_TRIGGERS
SYSTEM_TYPEINFO
SYSTEM_UDTATTRIBUTES
SYSTEM_UDTS
SYSTEM_USAGE_PRIVILEGES
SYSTEM_USERS
SYSTEM_VERSIONCOLUMNS
SYSTEM_VIEWS
SYSTEM_VIEW_COLUMN_USAGE
SYSTEM_VIEW_ROUTINE_USAGE
SYSTEM_VIEW_TABLE_USAGE 
 * */
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    DatabaseMetaData md = conn.getMetaData();
    ResultSet rs = md.getTables(null, null, "%", null);
    while (rs.next()) {
      System.out.println(rs.getString(3));
    }  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





ResultSet: getString(String columnLabel)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
      String name = rs.getString("name");
      System.out.println(name);
    }
    rs.close();
    st.close();
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





ResultSet: getTime(int columnIndex)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main {
  public static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    Class.forName(driver);
    return DriverManager.getConnection(url, "name", "password");
  }
  public static void main(String args[]) {
    String GET_RECORD = "select date_column, time_column, "
        + "timestamp_column from TestDates where id = ?";
    ResultSet rs = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
      conn = getConnection();
      pstmt = conn.prepareStatement(GET_RECORD);
      pstmt.setString(1, "0001");
      rs = pstmt.executeQuery();
      while (rs.next()) {
        java.sql.Date dbSqlDate = rs.getDate(1);
        java.sql.Time dbSqlTime = rs.getTime(2);
        java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
        System.out.println("dbSqlDate=" + dbSqlDate);
        System.out.println("dbSqlTime=" + dbSqlTime);
        System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        rs.close();
        pstmt.close();
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}





ResultSet: getTimestamp(int columnIndex)

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    String sqlQuery = "SELECT uid, name, duration from EVENTS";
    ResultSet rs = stmt.executeQuery(sqlQuery);
    while (rs.next()) {
      rs.updateString("Name", "new Name");
      rs.updateRow();
    }
    rs.first();
    while (rs.next()) {
      String name = rs.getString(2);
      Timestamp hireDate = rs.getTimestamp(5);
      System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }
    rs.close();
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





ResultSet: getType()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      ResultSet rs = stmt.executeQuery("SELECT * from DUMMY");
      System.out.println(rs.getType());
      System.out.println(rs.getConcurrency());
      rs.deleteRow();
      rs.close();
      rs.close();
      stmt.close();
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: getWarnings()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);
    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
    try {
      SQLWarning warning = connection.getWarnings();
      while (warning != null) {
        String message = warning.getMessage();
        String sqlState = warning.getSQLState();
        int errorCode = warning.getErrorCode();
        warning = warning.getNextWarning();
      }
      Statement stmt = connection.createStatement();
      warning = stmt.getWarnings();
      if (warning != null) {
      }
      ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
      while (resultSet.next()) {
        warning = resultSet.getWarnings();
        if (warning != null) {
        }
      }
    } catch (SQLException e) {
    }
  }
}





ResultSet.HOLD_CURSORS_OVER_COMMIT

 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    DatabaseMetaData dbMeta = conn.getMetaData();
    if (dbMeta.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
      System.out.println("this database hold cursors over commit");
    } else if (dbMeta.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
      System.out.println("this database close cursors at commit");
    }
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





ResultSet: insertRow()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    Connection con;
    Statement stmt;
    ResultSet uprs;
    try {
      Class.forName(driver);
      con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student","student");
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      uprs = stmt.executeQuery("SELECT * FROM Records");
      // Check the column count
      ResultSetMetaData md = uprs.getMetaData();
      System.out.println("Resultset has " + md.getColumnCount() + " cols.");
      int rowNum = uprs.getRow();
      System.out.println("row1 " + rowNum);
      uprs.absolute(1);
      rowNum = uprs.getRow();
      System.out.println("row2 " + rowNum);
      uprs.next();
      uprs.moveToInsertRow();
      uprs.updateInt(1, 150);
      uprs.updateString(2, "Madonna");
      uprs.updateString(3, "Dummy");
      uprs.updateString(4, "Jazz");
      uprs.updateString(5, "Image");
      uprs.updateInt(6, 5);
      uprs.updateDouble(7, 5);
      uprs.updateInt(8, 15);
      uprs.insertRow();
      uprs.close();
      stmt.close();
      con.close();
    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
  }
}





ResultSet: isAfterLast()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn
        .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,"Tom")");
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    // Get cursor position
    int pos = rs.getRow();          // 0
    System.out.println(pos);
    boolean b = rs.isBeforeFirst(); // true
    System.out.println(b);
    // Move cursor to the first row
    rs.next();
    // Get cursor position
    pos = rs.getRow();              // 1
    b = rs.isFirst();               // true
    System.out.println(pos);
    System.out.println(b);
    
    
    // Move cursor to the last row
    rs.last();
    // Get cursor position
    pos = rs.getRow();   
    System.out.println(pos);
    b = rs.isLast();     // true
    // Move cursor past last row
    rs.afterLast();
    // Get cursor position
    pos = rs.getRow();       
    b = rs.isAfterLast();    // true
    rs.close();
    st.close();
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





ResultSet: isBeforeFirst()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager
        .getConnection("jdbc:mysql://localhost/testdb", "root", "");
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");
    if (resultSet.isBeforeFirst()) {
      System.out.println("beginning");
    }
    connection.close();
  }
}





ResultSet: isFirst()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";
      ResultSet rs = stmt.executeQuery(sqlQuery);
      int rowSize = 0;
      while (rs.next()) {
        rowSize++;
      }
      System.out.println("Number of Rows in ResultSet is: " + rowSize);
      if (rowSize == 0) {
        System.out.println("Since there are no rows, exiting...");
        System.exit(0);
      }
      int cursorPosition = Math.round(rowSize / 2);
      System.out.println("Moving to position: " + cursorPosition);
      rs.absolute(cursorPosition);
      System.out.println("Name: " + rs.getString(2));
      rs.relative(-1);
      cursorPosition = rs.getRow();
      System.out.println("Moving to position: " + cursorPosition);
      System.out.println("Name: " + rs.getString(2));
      System.out.println("Moving to the first row");
      while (!rs.isFirst()) {
        rs.previous();
      }
      System.out.println("Name: " + rs.getString(2));
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: last()

 
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";
      ResultSet rs = stmt.executeQuery(sqlQuery);
      rs.last();
      // Move the cursor backwards through the ResultSet
      while (rs.previous()) {
        String nbr = rs.getString(1);
        String name = rs.getString(2);
        String job = rs.getString(3);
        String mgr = rs.getString(4);
        Timestamp hireDate = rs.getTimestamp(5);
        System.out.println(name);
      }
      rs.close();
      stmt.close();
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: moveToInsertRow()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    Connection con;
    Statement stmt;
    ResultSet uprs;
    try {
      Class.forName(driver);
      con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student","student");
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      uprs = stmt.executeQuery("SELECT * FROM Records");
      // Check the column count
      ResultSetMetaData md = uprs.getMetaData();
      System.out.println("Resultset has " + md.getColumnCount() + " cols.");
      int rowNum = uprs.getRow();
      System.out.println("row1 " + rowNum);
      uprs.absolute(1);
      rowNum = uprs.getRow();
      System.out.println("row2 " + rowNum);
      uprs.next();
      uprs.moveToInsertRow();
      uprs.updateInt(1, 150);
      uprs.updateString(2, "Madonna");
      uprs.updateString(3, "Dummy");
      uprs.updateString(4, "Jazz");
      uprs.updateString(5, "Image");
      uprs.updateInt(6, 5);
      uprs.updateDouble(7, 5);
      uprs.updateInt(8, 15);
      uprs.insertRow();
      uprs.close();
      stmt.close();
      con.close();
    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
  }
}





ResultSet: next()

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    String sqlQuery = "SELECT uid, name, duration from EVENTS";
    ResultSet rs = stmt.executeQuery(sqlQuery);
    while (rs.next()) {
      rs.updateString("Name", "new Name");
      rs.updateRow();
    }
    rs.first();
    while (rs.next()) {
      String name = rs.getString(2);
      Timestamp hireDate = rs.getTimestamp(5);
      System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }
    rs.close();
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





ResultSet: previous()

 
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";
      ResultSet rs = stmt.executeQuery(sqlQuery);
      rs.last();
      // Move the cursor backwards through the ResultSet
      while (rs.previous()) {
        String nbr = rs.getString(1);
        String name = rs.getString(2);
        String job = rs.getString(3);
        String mgr = rs.getString(4);
        Timestamp hireDate = rs.getTimestamp(5);
        System.out.println(name);
      }
      rs.close();
      stmt.close();
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: refreshRow()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);
    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");
    // Retrieve the current values of the row from the database
    resultSet.refreshRow();
  }
}





ResultSet: relative(int rows)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
      String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";
      ResultSet rs = stmt.executeQuery(sqlQuery);
      int rowSize = 0;
      while (rs.next()) {
        rowSize++;
      }
      System.out.println("Number of Rows in ResultSet is: " + rowSize);
      if (rowSize == 0) {
        System.out.println("Since there are no rows, exiting...");
        System.exit(0);
      }
      int cursorPosition = Math.round(rowSize / 2);
      System.out.println("Moving to position: " + cursorPosition);
      rs.absolute(cursorPosition);
      System.out.println("Name: " + rs.getString(2));
      rs.relative(-1);
      cursorPosition = rs.getRow();
      System.out.println("Moving to position: " + cursorPosition);
      System.out.println("Name: " + rs.getString(2));
      System.out.println("Moving to the first row");
      while (!rs.isFirst()) {
        rs.previous();
      }
      System.out.println("Name: " + rs.getString(2));
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





ResultSet: setFetchSize(int rows)

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLWarning;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    
    st.setFetchSize(1);
    
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    st.executeUpdate("insert into survey (id,name ) values (2,"nameValue")");
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    
    rs.setFetchSize(1);
    
    outputResultSet(rs);
    checkForWarning(rs.getWarnings());
    rs.close();
    st.close();
    conn.close();
  }
  static boolean checkForWarning(SQLWarning w) {
    if (w == null) {
      return false;
    }
    do {
      System.err.println("Warning:\nMessage: " + w.getMessage());
      System.err.println("SQL state: " + w.getSQLState());
      System.err.println("Vendor code: " + w.getErrorCode() + "\n----------------");
    } while ((w = w.getNextWarning()) != null);
    return true;
  }
  private static void outputResultSet(ResultSet rs) throws Exception {
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    for (int i = 1; i < numberOfColumns + 1; i++) {
      String columnName = rsMetaData.getColumnName(i);
      System.out.print(columnName + "   ");
    }
    System.out.println();
    System.out.println("----------------------");
    while (rs.next()) {
      for (int i = 1; i < numberOfColumns + 1; i++) {
        System.out.print(rs.getString(i) + "   ");
      }
      System.out.println();
    }
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





ResultSet: TYPE_FORWARD_ONLY

 

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
  private static final String DRIVER = "com.mysql.jdbc.Driver";
  private static final String URL = "jdbc:mysql://localhost/yourDatabase";
  private static final String USERNAME = "root";
  private static final String PASSWORD = "";
  public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    DatabaseMetaData metadata = connection.getMetaData();
    boolean updatable = metadata.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY,
        ResultSet.CONCUR_UPDATABLE);
    System.out.println("Updatable ResultSet supported = " + updatable);
    connection.close();
  }
}





ResultSet.TYPE_SCROLL_INSENSITIVE

 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    DatabaseMetaData meta = conn.getMetaData();
    if (meta.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)) {
      System.out.println("type name=TYPE_FORWARD_ONLY");
    }
    if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
      System.out.println("type name=TYPE_SCROLL_INSENSITIVE");
    }
    if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
      System.out.println("type name=TYPE_SCROLL_SENSITIVE");
    }

    st.close();
    conn.close();
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}





ResultSet.TYPE_SCROLL_SENSITIVE

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    String sqlQuery = "SELECT uid, name, duration from EVENTS";
    ResultSet rs = stmt.executeQuery(sqlQuery);
    while (rs.next()) {
      rs.updateString("Name", "new Name");
      rs.updateRow();
    }
    rs.first();
    while (rs.next()) {
      String name = rs.getString(2);
      Timestamp hireDate = rs.getTimestamp(5);
      System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }
    rs.close();
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





ResultSet: updateDouble(int columnIndex, double x)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    Connection con;
    Statement stmt;
    ResultSet uprs;
    try {
      Class.forName(driver);
      con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student","student");
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      uprs = stmt.executeQuery("SELECT * FROM Records");
      // Check the column count
      ResultSetMetaData md = uprs.getMetaData();
      System.out.println("Resultset has " + md.getColumnCount() + " cols.");
      int rowNum = uprs.getRow();
      System.out.println("row1 " + rowNum);
      uprs.absolute(1);
      rowNum = uprs.getRow();
      System.out.println("row2 " + rowNum);
      uprs.next();
      uprs.moveToInsertRow();
      uprs.updateInt(1, 150);
      uprs.updateString(2, "Madonna");
      uprs.updateString(3, "Dummy");
      uprs.updateString(4, "Jazz");
      uprs.updateString(5, "Image");
      uprs.updateInt(6, 5);
      uprs.updateDouble(7, 5);
      uprs.updateInt(8, 15);
      uprs.insertRow();
      uprs.close();
      stmt.close();
      con.close();
    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
  }
}





ResultSet: updateInt(String columnLabel, int x)

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Contacts");
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("select * from employee");
    rs.moveToInsertRow();
    rs.updateInt("Contact_ID", 150);
    rs.updateString("First_Name", "Nigel");
    rs.updateString("Last_Name", "Thornebury");
    rs.insertRow();
  }
}





ResultSet: updateRow()

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    String sqlQuery = "SELECT uid, name, duration from EVENTS";
    ResultSet rs = stmt.executeQuery(sqlQuery);
    while (rs.next()) {
      rs.updateString("Name", "new Name");
      rs.updateRow();
    }
    rs.first();
    while (rs.next()) {
      String name = rs.getString(2);
      Timestamp hireDate = rs.getTimestamp(5);
      System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }
    rs.close();
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





ResultSet: updateString(String columnName, String x)

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class Main {
  public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
    String sqlQuery = "SELECT uid, name, duration from EVENTS";
    ResultSet rs = stmt.executeQuery(sqlQuery);
    while (rs.next()) {
      rs.updateString("Name", "new Name");
      rs.updateRow();
    }
    rs.first();
    while (rs.next()) {
      String name = rs.getString(2);
      Timestamp hireDate = rs.getTimestamp(5);
      System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }
    rs.close();
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





ResultSet: wasNull()

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:technical_library";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String theStatement =
     "SELECT authid, lastname, firstname, email FROM authors ORDER BY authid";
    try {
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, "guest", "guest");
      Statement queryAuthors = connection.createStatement();
      ResultSet results = queryAuthors.executeQuery(theStatement);
      String lastname, firstname, email;
      int id;
      while(results.next()) {
        id = results.getInt(1);
        lastname = results.getString(2);
        firstname = results.getString(3);
        email = results.getString(4);
        if(results.wasNull()) {
          email = "no email";
        }
        System.out.println(Integer.toString(id) + ", " +
                           lastname.trim() + ", " +
                           firstname.trim() +", " +
                           email.trim());
      }
      queryAuthors.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}