package example;
import java.sql.*;
public class RowGenerator {
public static void rowGenerator( int numRows, ResultSet rset[] ) {
// Create the meta data needed for the result set
ResultSetMetaDataImpl rsmd = new ResultSetMetaDataImpl(2);
//The first column is the SQL type INTEGER.
rsmd.setColumnType(1, Types.INTEGER);
rsmd.setColumnName(1,"c1");
rsmd.setColumnLabel(1,"c1");
rsmd.setTableName(1,"MyTable");
// The second column is the SQL type VARCHAR length 255
rsmd.setColumnType(2, Types.VARCHAR);
rsmd.setColumnName(2,"c2");
rsmd.setColumnLabel(2,"c2");
rsmd.setColumnDisplaySize(2, 255);
rsmd.setTableName(2,"MyTable");
// Create result set using the ResultSetMetaData
ResultSetImpl rs = null;
try {
rs = new ResultSetImpl( (ResultSetMetaData)rsmd );
rs.beforeFirst(); // Make sure we are at the beginning.
} catch( Exception e ) {
System.out.println( "Error: couldn't create result set." );
System.out.println( e.toString() );
}
// Add the rows to the result set and populate them
for( int i = 0; i < numRows; i++ ) {
try {
rs.insertRow(); // insert a new row.
rs.updateInt( 1, i ); // put the integer value in the first column
rs.updateString( 2, ("Str" + i) ); // put the VARCHAR/String value in the second column
} catch( Exception e ) {
System.out.println( "Error: couldn't insert row/data on row " + i );
System.out.println( e.toString() );
}
}
try {
rs.beforeFirst(); // rewind the result set so that the server gets it from the beginning.
} catch( Exception e ) {
System.out.println( e.toString() );
}
rset[0] = rs; // assign the result set to the 1st of the passed in array.
}
}