Kbase 20599: Sample JDBC Scrollable Result Set Program
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  10/16/2008 |
|
Status: Unverified
GOAL:
Sample JDBC Scrollable Result Set Program
FIX:
Follow these steps to generate a sample program that demonstrates how to use scrollable result sets when connecting to a Progress database through JDBC:
1) Download and install the desired JDK.
2) Add the JDK/bin directory to your path.
3) Make sure that the DLC/java/jdbc.zip and DLC/java/progress.zip
files are in your CLASSPATH.
The following program assumes that there is a database server started for a Sports2000 database that uses port 2500. The example uses a User "dba1" with a password of "password".
Sample program:
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:sports2000";
String username= "dba1";
String password= "password";
int rowCount;
try {
Class.forName(driver);
Connection con =
DriverManager.getConnection(URL, username, password);
PreparedStatement pst =
con.prepareStatement("Select name from pub.customer " +
"where balance > 50000 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 in the query");
/* 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());
ex = ex.getNextException();
}
}
catch (Throwable excp) {
excp.printStackTrace();
}
}
}