hi,

my next button displays next record from the database. But after i save a record, next button don't work.

Im not sure but is the next button not working because the resultSet needs to be refreshed after saving a new record?

I have created the statement and resultSet in the constructor.

Save button does work.

This is my first post here so Please help. Thanks in advance.

///////////////////////////////// constructor calls displayFirstrecord() method to create statement and resultSet/////////////////////////////////////


/////////////////////////////////////////////////////// displayFirstrecord() starts //////////////////////////////////////////////
public void displayFirstrecord()
{
try{

statement = parent.connection.createStatement(ResultSet.
TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

resultSet = statement.executeQuery("Select * FROM tblPerson order by USERID");

resultSet = statement.getResultSet();

if(resultSet.next())
{
txtUser.setText(resultSet.getString("userID"));
txtPass.setText(resultSet.getString("password"));
txtGiven.setText(resultSet.getString("given"));
txtSurname.setText(resultSet.getString("surname"));
}

}
catch (SQLException f)
{
JOptionPane.showMessageDialog( null, f.getMessage(), "Database Error", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
}
////////////////////////////////////////////////// displayFirstrecord() ends ////////////////////////

////// ///////////////////////////////////// nextbutton starts /////
void btnNext_actionPerformed(ActionEvent e) {
try {

if (resultSet.next()) {
//resultSet.refreshRow();
hidden.setText(resultSet.getString("USERID")); //hidden

txtUser.setText(resultSet.getString("USERID"));
txtPass.setText(resultSet.getString("PASSWORD"));
txtGiven.setText(resultSet.getString("GIVEN"));
txtSurname.setText(resultSet.getString("SURNAME"));
}
}
catch (SQLException d) {
JOptionPane.showMessageDialog(null, d.getMessage());
d.printStackTrace();
}

}
////////////////////////////////////////////////// next button ends ////////////////////////////


////////////////////////////// save button starts ////////////////////////////////////////////
void btnSave_actionPerformed(ActionEvent e) {

if(txtUser.getText().equals("")||txtPass.getText().equals("") ||
txtGiven.getText().equals("") || txtSurname.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Enter values in all text boxes");
}

try
{

if(addStatus == true)
{
String sql = "INSERT INTO tblPerson values("
+ "'"
+ txtUser.getText()
+"'"
+ ","
+ "'"
+ txtPass.getText()
+ "'"
+ ","
+ "'"
+ txtGiven.getText()
+ "'"
+ ","
+ "'"
+ txtSurname.getText()
+ "'"
+ ")";

int x =statement.executeUpdate(sql);
if(x ==1)
{
addStatus = false;

JOptionPane.showMessageDialog(null,"Data saved to database");

}
else
{
JOptionPane.showMessageDialog(null,"Data not saved to database");
}


}
/////////////////////////////////////////////////// save button ends ///////////////////////////////////////////////////////////////

Likely your DB session is set to autoCommit after each SQL command issued through JDBC. Any open resultSets on a given DB connection will reset after a commit (auto or manual). If you turn off auto-commit then you will be able to use your cursor after an update. But, now you will need to find a good place to commit your changes. One way to do it is to have a "Save" and a "Cancel" button on your GUI that can be attached to the DB "commit" and "rollback" respectively. You would need to re-fetch your result set after performing a commit or a rollback.

yeh.. thanks man... my program working now.
Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.