Navigationa with ResultSet
-
I'm not able to move forward with the ResultSet with this code on "next Button". Here is the code
public void nextrecord()
{Statement stmt = null; String query = "select \* from Employer"; try { stmt = con.createStatement(ResultSet.TYPE\_SCROLL\_INSENSITIVE,ResultSet.CONCUR\_READ\_ONLY); rs = stmt.executeQuery(query); if(rs.isBeforeFirst()); { while(rs.next()) { firstNameTextField.setText(rs.getString(2)); lastNameTextField.setText(rs.getString(3)); addressTextField.setText(rs.getString(4)); } } } catch (SQLException e ) { e.printStackTrace(); } }
The problem is when I click on the next button ,it takes me to the last record and skips all the records ,I wanted to go through all the records one by one ,clicking each record one by one. There is one record present in the text fields before pressing next button.
-
I'm not able to move forward with the ResultSet with this code on "next Button". Here is the code
public void nextrecord()
{Statement stmt = null; String query = "select \* from Employer"; try { stmt = con.createStatement(ResultSet.TYPE\_SCROLL\_INSENSITIVE,ResultSet.CONCUR\_READ\_ONLY); rs = stmt.executeQuery(query); if(rs.isBeforeFirst()); { while(rs.next()) { firstNameTextField.setText(rs.getString(2)); lastNameTextField.setText(rs.getString(3)); addressTextField.setText(rs.getString(4)); } } } catch (SQLException e ) { e.printStackTrace(); } }
The problem is when I click on the next button ,it takes me to the last record and skips all the records ,I wanted to go through all the records one by one ,clicking each record one by one. There is one record present in the text fields before pressing next button.
Separate your actions... 0. Method to query the data, then store the record set. 1. Method to display a record. Something like [untested code]
class Hatstand
extends Stuff
{
private RecordSet recordSet;
// need to initialise & stuff./**
* Query the data
*/
private void submitQuery()
{
Statement stmt = null;
String query = "select * from Employer";
try
{
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
this.recordSet = stmt.executeQuery(query);
}
catch (SQLException e )
{
// message box'd be better
e.printStackTrace();
this.recordSet = null;
}
finally
{
setRecord(true);
}
}/**
* Set the displayed record
* called after query and by next/previous buttons.
*/
private void setRecord(boolean forward)
{
// set the record:
boolean isNull = true;
if (this.RecordSet != null)
{
if (forward && !this.RecordSet.isLast())
{
this.RecordSet.next();
isNull = false;
}
else if (!forward && !this.RecordSet.isFirst())
{
this.RecordSet.previous();
isNull = false;
}
}if (isNull) { firstNameTextField.setText(null); lastNameTextField.setText(null); addressTextField.setText(null); } } else { firstNameTextField.setText(rs.getString(2)); lastNameTextField.setText(rs.getString(3)); addressTextField.setText(rs.getString(4)); } }
}
}speramus in juniperus
-
Separate your actions... 0. Method to query the data, then store the record set. 1. Method to display a record. Something like [untested code]
class Hatstand
extends Stuff
{
private RecordSet recordSet;
// need to initialise & stuff./**
* Query the data
*/
private void submitQuery()
{
Statement stmt = null;
String query = "select * from Employer";
try
{
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
this.recordSet = stmt.executeQuery(query);
}
catch (SQLException e )
{
// message box'd be better
e.printStackTrace();
this.recordSet = null;
}
finally
{
setRecord(true);
}
}/**
* Set the displayed record
* called after query and by next/previous buttons.
*/
private void setRecord(boolean forward)
{
// set the record:
boolean isNull = true;
if (this.RecordSet != null)
{
if (forward && !this.RecordSet.isLast())
{
this.RecordSet.next();
isNull = false;
}
else if (!forward && !this.RecordSet.isFirst())
{
this.RecordSet.previous();
isNull = false;
}
}if (isNull) { firstNameTextField.setText(null); lastNameTextField.setText(null); addressTextField.setText(null); } } else { firstNameTextField.setText(rs.getString(2)); lastNameTextField.setText(rs.getString(3)); addressTextField.setText(rs.getString(4)); } }
}
}speramus in juniperus