Skip to main content

APIzation: Replication Package

[Q#19533991][A#19534456] How to get number of rows returned by Resultset while using HSQLDB?

https://stackoverflow.com/q/19533991

I am getting exception feature not supported while getting the total no of rows using ResultSet.last() function. I tried using the hsqldb specific connection and ResultSet class but no success. Can some one guide any way to get the no of rows from result set except looping through all the rows. the code snippet used for getting no of rows is given below:

Answer

https://stackoverflow.com/a/19534456

After having look at HSQLDB api ,it can be done same as for ORACLE which is shown below

APIzation

String URL = "jdbc:oracle:thin:@ip:port:sid";
String USER = "test";
String PASS = "test";
String query = "Select * from mytable";
try {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection con = DriverManager.getConnection(URL, USER, PASS);
    Statement stmt = con.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = stmt.executeQuery(query);
    if (rs.next()) {
        rs.last();
        System.out.println("total rows is : " + rs.getRow());
    } else {
        System.out.println("No Data");
    }
} catch (Exception e) {
    e.printStackTrace();
}
package com.stackoverflow.api;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Human19534456 {

  public static int numberOfReturnedHSQLDBBResultSet(
    String URL,
    String USER,
    String PASS,
    String query
  ) {
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
      Connection con = DriverManager.getConnection(URL, USER, PASS);
      Statement stmt = con.createStatement(
        ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_READ_ONLY
      );
      ResultSet rs = stmt.executeQuery(query);
      if (rs.next()) {
        rs.last();
        return rs.getRow();
      } else {
        return 0;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return -1;
  }
}

package com.stackoverflow.api;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * How to get number of rows returned by Resultset while using HSQLDB?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/19534456">https://stackoverflow.com/a/19534456</a>
 */
public class APIzator19534456 {

  public static void getNumber(
    String URL,
    String USER,
    String PASS,
    String query
  ) throws Exception {
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
      Connection con = DriverManager.getConnection(URL, USER, PASS);
      Statement stmt = con.createStatement(
        ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_READ_ONLY
      );
      ResultSet rs = stmt.executeQuery(query);
      if (rs.next()) {
        rs.last();
        System.out.println("total rows is : " + rs.getRow());
      } else {
        System.out.println("No Data");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}