If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > JAVA > How to Add, Update, Delete Save and Cancel New a Record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-13-10, 03:33
Akinyemi Akinyemi is offline
Registered User
 
Join Date: Jul 2006
Posts: 24
How to Add, Update, Delete Save and Cancel New a Record

Hi,
I have been learning how to Add, Save, Delete, and Update a database table. I created my Database named Employees and a Table named Workers. The Table has the following fields or columns: ID, FirstName, LastName and JobTitle. I made the ID field the primary key. My goal is to be able to add, Delete, cancel new record and to be able to edit existing records. Below is the code for each:

private void btnUpdateRecordActionPerformed(ActionEvent e)
{
String first = textFirstName.getText();
String last = textLastName.getText();
String job = textJobTitle.getText();
String ID = textID.getText();

int newID = Integer.parseInt(ID);

try
{
stmt= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV E,
ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM Workers";
rs = stmt.executeQuery(sql);

rs.updateInt("ID", newID);
rs.updateString("FirstName", first);
rs.updateString("LastName", last);
rs.updateString("JobTitle", job);
rs.updateRow();
JOptionPane.showMessageDialog(
Workers.this, "Update");
}
catch(SQLException err)
{
System.out.println(err.getMessage() );
}

}

private void btnDeleteRecordActionPerformed(ActionEvent e)
{
try
{
rs.deleteRow();
stmt.close();
rs.close();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV E,
ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM Workres";
rs = stmt.executeQuery(sql);

rs.next();
int idCol = rs.getInt("ID");
String id = Integer.toString(idCol);
String first = rs.getString("FirstName");
String last = rs.getString("LastName");
String job = rs.getString("JobTitle");

textID.setText(id);
textFirstName.setText(first);
textLastName.setText(last);
textJobTitle.setText(job);
}
catch(SQLException err)
{
JOptionPane.showMessageDialog(
Workers.this, err.getMessage() );
}
}
private void btnCancelNewRecordActionPerformed(ActionEvent e)
{
try
{
rs.absolute(curRow);
textFirstName.setText(rs.getString("FirstName") );
textLastName.setText(rs.getString("LastName") );
textJobTitle.setText(rs.getString("JobTitle") );
textID.setText(Integer.toString(rs.getInt("ID")) );

btnFirst.setEnabled(true);
btnPrevious.setEnabled(true);
btnNext.setEnabled(true);
btnLast.setEnabled(true);
btnUpdateRecord.setEnabled(true);
btnDeleteRecord.setEnabled(true);
btnNewRecord.setEnabled(true);

btnSaveNewRecord.setEnabled(false);
btnCancelNewRecord.setEnabled(false);
}
catch(SQLException err)
{
System.out.println(err.getMessage() );
}

}

private void btnSaveNewRecordActionPerformed(ActionEvent e)
{
String first = textFirstName.getText();
String last = textLastName.getText();
String job = textJobTitle.getText();
String ID = textID.getText();
int newID = Integer.parseInt(ID);

try
{
rs.updateInt("ID", newID);
rs.updateString("FirstName", first);
rs.updateString("LastName", last);
rs.updateString("JobTitle", job);

rs.insertRow();

stmt.close();
rs.close();

stmt= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV E,
ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM Workers";
rs = stmt.executeQuery(sql);

rs.next();
int idCol = rs.getInt("ID");
String id = Integer.toString(idCol);
String first2 = rs.getString("FirstName");
String last2 = rs.getString("LastName");
String job2 = rs.getString("JobTitle");

textID.setText(id);
textFirstName.setText(first2);
textLastName.setText(last2);
textJobTitle.setText(job2);

btnFirst.setEnabled(true);
btnPrevious.setEnabled(true);
btnNext.setEnabled(true);
btnLast.setEnabled(true);
btnUpdateRecord.setEnabled(true);
btnDeleteRecord.setEnabled(true);
btnNewRecord.setEnabled(true);

btnSaveNewRecord.setEnabled(false);
btnCancelNewRecord.setEnabled(false);
}
catch(SQLException err)
{
System.out.println(err.getMessage() );
}
}
Each time I run the program and click any of the buttons, I get the following error message: Result Set not Updatable. This result set must come from a statement that was created with a result set type of a ResultSet.CUNCUR_UPDATABLE. The query must select only on table and must select all primary keys from that table. My code and my application meet the above requirements.

Please, help me solve the problem or direct me to where I can find a solution. Alternatively, I need a SQL statement that will do the same job.
Than you.
Akinyemi
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On