473,419 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. private void btnUpdateRecordActionPerformed(ActionEvent e)
  2.         {
  3.             String first = textFirstName.getText();
  4.             String last = textLastName.getText();
  5.             String job = textJobTitle.getText();
  6.             String ID = textID.getText();
  7.  
  8.             int newID = Integer.parseInt(ID);
  9.  
  10.             try
  11.             {
  12. stmt= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  13.                         ResultSet.CONCUR_UPDATABLE);
  14.                         String sql = "SELECT * FROM Workers";
  15.                 rs = stmt.executeQuery(sql);
  16.  
  17.                 rs.updateInt("ID", newID);
  18.                 rs.updateString("FirstName", first);
  19.                 rs.updateString("LastName", last);
  20.                 rs.updateString("JobTitle", job);
  21.                 rs.updateRow();
  22.                 JOptionPane.showMessageDialog(
  23.                     Workers.this, "Update");
  24.             }
  25.             catch(SQLException err)
  26.             {
  27.                 System.out.println(err.getMessage() );
  28.             }
  29.  
  30.         }
  31.  
  32.         private void btnDeleteRecordActionPerformed(ActionEvent e)
  33.         {
  34.             try
  35.             {
  36.                 rs.deleteRow();
  37.                 stmt.close();
  38.                 rs.close();
  39.                 stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  40.                 ResultSet.CONCUR_UPDATABLE);
  41.                 String sql = "SELECT * FROM Workres";
  42.                 rs = stmt.executeQuery(sql);
  43.  
  44.                 rs.next();
  45.                 int idCol = rs.getInt("ID");
  46.                 String id = Integer.toString(idCol);
  47.                 String first = rs.getString("FirstName");
  48.                 String last = rs.getString("LastName");
  49.                 String job = rs.getString("JobTitle");
  50.  
  51.                 textID.setText(id);
  52.                 textFirstName.setText(first);
  53.                 textLastName.setText(last);
  54.                 textJobTitle.setText(job);
  55.             }
  56.             catch(SQLException err)
  57.             {
  58.                 JOptionPane.showMessageDialog(
  59.                     Workers.this, err.getMessage() );
  60.             }
  61.         }
  62. private void btnCancelNewRecordActionPerformed(ActionEvent e)
  63.         {
  64.             try
  65.             {
  66.                 rs.absolute(curRow);
  67.                 textFirstName.setText(rs.getString("FirstName") );
  68.                 textLastName.setText(rs.getString("LastName") );
  69.                 textJobTitle.setText(rs.getString("JobTitle") );
  70.                 textID.setText(Integer.toString(rs.getInt("ID")) );
  71.  
  72.                 btnFirst.setEnabled(true);
  73.                 btnPrevious.setEnabled(true);
  74.                 btnNext.setEnabled(true);
  75.                 btnLast.setEnabled(true);
  76.                 btnUpdateRecord.setEnabled(true);
  77.                 btnDeleteRecord.setEnabled(true);
  78.                 btnNewRecord.setEnabled(true);
  79.  
  80.                 btnSaveNewRecord.setEnabled(false);
  81.                 btnCancelNewRecord.setEnabled(false);
  82.             }
  83.             catch(SQLException err)
  84.             {
  85.                 System.out.println(err.getMessage() );
  86.             }
  87.  
  88.         }
  89.  
  90.         private void btnSaveNewRecordActionPerformed(ActionEvent e)
  91.         {
  92.             String first = textFirstName.getText();
  93.             String last = textLastName.getText();
  94.             String job = textJobTitle.getText();
  95.             String ID  = textID.getText();
  96.             int newID = Integer.parseInt(ID);
  97.  
  98.             try
  99.             {
  100.                 rs.updateInt("ID", newID);
  101.                 rs.updateString("FirstName", first);
  102.                 rs.updateString("LastName", last);
  103.                 rs.updateString("JobTitle", job);
  104.  
  105.                 rs.insertRow();
  106.  
  107.                 stmt.close();
  108.                 rs.close();
  109.  
  110. stmt=   con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  111.                 ResultSet.CONCUR_UPDATABLE);
  112.                 String sql = "SELECT * FROM Workers";
  113.                 rs = stmt.executeQuery(sql);
  114.  
  115.                 rs.next();
  116.                 int idCol = rs.getInt("ID");
  117.                 String id = Integer.toString(idCol);
  118.                 String first2 = rs.getString("FirstName");
  119.                 String last2 = rs.getString("LastName");
  120.                 String job2 = rs.getString("JobTitle");
  121.  
  122.                 textID.setText(id);
  123.                 textFirstName.setText(first2);
  124.                 textLastName.setText(last2);
  125.                 textJobTitle.setText(job2);
  126.  
  127.                 btnFirst.setEnabled(true);
  128.                 btnPrevious.setEnabled(true);
  129.                 btnNext.setEnabled(true);
  130.                 btnLast.setEnabled(true);
  131.                 btnUpdateRecord.setEnabled(true);
  132.                 btnDeleteRecord.setEnabled(true);
  133.                 btnNewRecord.setEnabled(true);
  134.  
  135.                 btnSaveNewRecord.setEnabled(false);
  136.                 btnCancelNewRecord.setEnabled(false);
  137.             }
  138.             catch(SQLException err)
  139.             {
  140.                 System.out.println(err.getMessage() );
  141.             }
  142.         }
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
Oct 13 '10 #1
0 1467

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: eddie wang | last post by:
Excel open automatically without giving a dialog box option to Open/Save/Cancel using filesys.createTextFile. How to pop up the dialog box option to Open/Save/Cancel? Thanks.
2
by: news.la.sbcglobal.net | last post by:
hi all i am trying to update or delete a row/record in table cptcodes the error i am getting is key column information is insufficient or incorrect to many rows were affected by update.... ...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
by: dixie | last post by:
I know I've asked this before, but the answer elludes me and the message has long since disappeared from my newsgroup messages. How do you save a record using vba from a button or as part of an...
2
by: Mark | last post by:
I have a repetitive task where I need to edit a table in SQL Server through a web page. Nothing fancy, just populate a datagrid and use the commands Insert/Update/Delete/Cancel. Is there a...
4
by: Daniel | last post by:
Hi All, I have question regarding to the read, and update the row in the table. for example: user A, read the row of data from the table. user B, read the row of data from the table as well....
2
by: Matuag | last post by:
Hi All, I want to create following command buttons on a Form which users can edit. Save ( Save Changes made) Cancel ( Undo data changes) Exit ( Close form) I am using Macros for each of...
2
by: Ian | last post by:
I am trying to save the current record on a form before opening a report, doesn’t sound to hard does it? The code on a buttons on click event goes like this: DoCmd.DoMenuItem acFormBar,...
1
by: elainenguyen | last post by:
I have a form that allow users to view the record, make update to the record, but then I want to allow user to save the update as a NEW RECORD. this is my code, and I need help. doCmd.save...
6
BeemerBiker
by: BeemerBiker | last post by:
I can't get rid of a "deleted" record in a database. Even if I delete it the record shows back up the next time I do an update. The following code generates an error message when using...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.