ResultSet: setFetchSize(int rows) : ResultSet « java.sql « Java by API






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", "");
  }
}

   
  








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(int columnIndex)
28.ResultSet: getObject(String columnLabel)
29.ResultSet: getRef(int columnIndex)
30.ResultSet: getRow()
31.ResultSet: getShort(String columnLabel)
32.ResultSet: getString(int columnIndex)
33.ResultSet: getString(String columnLabel)
34.ResultSet: getTime(int columnIndex)
35.ResultSet: getTimestamp(int columnIndex)
36.ResultSet: getType()
37.ResultSet: getWarnings()
38.ResultSet: insertRow()
39.ResultSet: isAfterLast()
40.ResultSet: isBeforeFirst()
41.ResultSet: isFirst()
42.ResultSet: last()
43.ResultSet: moveToInsertRow()
44.ResultSet: next()
45.ResultSet: previous()
46.ResultSet: refreshRow()
47.ResultSet: relative(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()