Kbase 20751: Scrolling Result Set Sample in Java
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  4/8/2002 |
|
Status: Unverified
GOAL:
Scrolling Result Set Sample in Java
FACT(s) (Environment):
Progress 9.1B
FIX:
Below is a sample Java program to show how to scroll thru data records when accessing the SQL-92 engine via JDBC.
Scrolling thru data is not a function that is included in the early releases of JDBC. The following code works with Progress version 9.1B and Java JDK 1.2 or later and illustrates the scrolling record sets functionality available in JDBC 2:
import java.sql.*;
public class scrollExample
{
public static void main(String args[])
{
String driver = "com.progress.sql.jdbc.JdbcProgressDriver";
String URL = "jdbc:jdbcprogress:T:localhost:2500:sports";
String username= "sysprogress";
String password= "x";
int rowCount;
try {
Class.forName(driver);
Connection con =
DriverManager.getConnection(URL, username, password);
PreparedStatement pst =
con.prepareStatement("Select name from pub.customer " +
"where balance > 30000 order by name",
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
System.out.println("Connected to " + URL);
ResultSet rs = pst.executeQuery();
/* Go to the last row and get the row number.
This will tell us how many rows
were returned by the query. */
rs.last();
rowCount = rs.getRow();
System.out.println("There are " + rowCount + " rows");
/* Go to the first row and get the value
of the "name" column. */
rs.first();
String firstName = rs.getString("name");
System.out.println("The first customer is: " + firstName);
/* Go to the last row and get the value of the "name" column. */
rs.last();
String lastName = rs.getString("name");
System.out.println("The last customer is: " + lastName);
/* Go back to before the first row
(so the rs.next method will work) and
display the rows from start to finish. */
System.out.println("\nCustomer names in ascending order");
System.out.println("=================================");
rs.beforeFirst();
while (rs.next()) {
String s = rs.getString("name");
System.out.println(s);
}
/* Go forward to after the last row
(so the rs.previous method will work)
and display the rows from finish to start. */
System.out.println("\nCustomer names in descending order");
System.out.println("==================================");
rs.afterLast();
while (rs.previous()) {
String s = rs.getString(1);
System.out.println(s);
}
rs.close();
}
catch (SQLException ex) {
System.out.println("\n*** SQL Exception caught*** \n");
while (ex != null) {
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message: " + ex.getMessage());
System.out.println("Vendor: " + ex.getErrorCode() + "\n");
ex = ex.getNextException();
}
}
catch (Throwable excp) {
excp.printStackTrace();
}
}
}