Creating User Account Information in Java - Updating and Deleting Records to Database

This is my final continuation of my other two tutorials: Creating User Account Information in Java - Adding Records to Database and Creating User Account Information in Java - Searching Records to Database. Now, we don't have to modify our code for that. Only add the following code below to your UserSettings.java. 1. This is for the initialization of variables to be used as buttons and labels. Just use JButton to have a button in your form. Here we just add Update, Delete, and Exit Button from the previous tutorial.
  1. public class UserSettings extends JFrame implements ActionListener{
  2.  
  3. JButton btnNew = new JButton("Add");
  4. JButton btnUpdate = new JButton("Update");
  5. JButton btnDelete = new JButton("Delete");
  6. JButton btnSearch = new JButton("Search");
  7. JButton btnExit = new JButton("Exit");
  8. }
2. Now in your constructor UserSettings() , add the 3 buttons for update, delete, and exit.
  1. public UserSettings() {
  2.  
  3. btnUpdate.setBounds(80,190,75,35);
  4. pane.add(btnUpdate);
  5. btnUpdate.addActionListener(this);
  6. btnDelete.setBounds(155,190,75,35);
  7. pane.add(btnDelete);
  8. btnDelete.addActionListener(this);
  9. btnExit.setBounds(130,260,75,35);
  10. pane.add(btnExit);
  11. }
3. Lastly, in the actionEvents when clicking this 3 buttons put this code below.
  1. if(source == btnUpdate){
  2. try{
  3.  
  4. String uname=txtUser.getText();
  5. String pass=txtPass.getText();
  6. String name1=txtName1.getText();
  7. String name2=txtName2.getText();
  8. if (!uname.equals("") && !pass.equals("")&& !name1.equals("") && !name2.equals("")) {
  9. st= cn.createStatement();
  10. PreparedStatement ps = cn.prepareStatement("UPDATE Login SET password = '" + txtPass.getText() + "',name1 = '" + txtName1.getText() + "',name2= '" + txtName2.getText()+ "'WHERE username = '" + txtUser.getText() + "'");
  11. ps.executeUpdate();
  12. JOptionPane.showMessageDialog(null,"Account has been successfully updated.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  13. txtUser.requestFocus(true);
  14. clear();
  15. st.close();
  16. }
  17. else{
  18. JOptionPane.showMessageDialog(null,"Please Fill Up The Empty Fields","Warning",JOptionPane.WARNING_MESSAGE);
  19. }
  20.  
  21. }catch (SQLException y){
  22. JOptionPane.showMessageDialog(null,"Unable to update!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);
  23. }
  24. }
  25. if(source==btnDelete){
  26. try{
  27. PreparedStatement ps = cn.prepareStatement("DELETE FROM Login WHERE username ='"+ txtUser.getText() + "'");
  28. ps.executeUpdate();
  29. JOptionPane.showMessageDialog(null,"Account has been successfully deleted.","Payroll System: User settings ",JOptionPane.INFORMATION_MESSAGE);
  30. txtUser.requestFocus(true);
  31. clear();
  32. st.close();
  33. }catch(SQLException s){
  34. JOptionPane.showMessageDialog(null,"Unable to delete!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);}
  35.  
  36. }if(source==btnExit){
  37. dispose();
  38. }
  39. }
The syntax for updating a record here is the: PreparedStatement ps = cn.prepareStatement("UPDATE Login SET password = '" + txtPass.getText() + "',name1 = '" + txtName1.getText() + "',name2= '" + txtName2.getText()+ "'WHERE username = '" + txtUser.getText() + "'"); The syntax for deleting a record here is the: PreparedStatement ps = cn.prepareStatement("DELETE FROM Login WHERE username ='"+ txtUser.getText() + "'"); And lastly, the dispose() function is used for exiting or closing the form. Output: outputoutputoutput Here is the full code of this tutorial:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5. import java.lang.*;
  6.  
  7. public class UserSettings extends JFrame implements ActionListener{
  8.  
  9. JLabel lblUser = new JLabel("Username ",JLabel.RIGHT);
  10. JLabel lblPass = new JLabel("Password ",JLabel.RIGHT);
  11. JLabel lblName1 = new JLabel("First Name",JLabel.RIGHT);
  12. JLabel lblName2 = new JLabel("Family Name",JLabel.RIGHT);
  13.  
  14. JTextField txtUser = new JTextField(20);
  15. JPasswordField txtPass= new JPasswordField(20);lll
  16. JTextField txtName1= new JTextField(20);
  17. JTextField txtName2= new JTextField(20);
  18.  
  19. JButton btnNew = new JButton("Add");
  20. JButton btnUpdate = new JButton("Edit");
  21. JButton btnDelete = new JButton("Delete");
  22. JButton btnSearch = new JButton("Search");
  23. JButton btnExit = new JButton("Exit");
  24.  
  25. public void clear(){
  26. txtUser.setText("");
  27. txtPass.setText("");
  28. txtName1.setText("");
  29. txtName2.setText("");
  30. }
  31. public UserSettings() {
  32. super("User Account Settings");
  33.  
  34. JPanel pane = new JPanel();
  35. pane.setLayout(null);
  36.  
  37. lblUser.setBounds(5,50,80,25);
  38. pane.add(lblUser);
  39. txtUser.setBounds(90,50,150,25);
  40. pane.add(txtUser);
  41. lblUser.setForeground(Color.white);
  42.  
  43. lblPass.setBounds(5,85,80,25);
  44. pane.add(lblPass);
  45. txtPass.setBounds(90,85,150,25);
  46. txtPass.setEchoChar('*');
  47. pane.add(txtPass);
  48. lblPass.setForeground(Color.white);
  49.  
  50. lblName1.setBounds(5,120,80,25);
  51. pane.add(lblName1);
  52. txtName1.setBounds(90,120,150,25);
  53. pane.add(txtName1);
  54. lblName1.setForeground(Color.white);
  55.  
  56.  
  57. lblName2.setBounds(5,155,80,25);
  58. pane.add(lblName2);
  59. txtName2.setBounds(90,155,150,25);
  60. pane.add(txtName2);
  61. lblName2.setForeground(Color.white);
  62.  
  63. btnNew.setBounds(5,190,75,35);
  64. pane.add(btnNew);
  65.  
  66. btnNew.addActionListener(this);
  67. btnUpdate.setBounds(80,190,75,35);
  68. pane.add(btnUpdate);
  69.  
  70. btnUpdate.addActionListener(this);
  71. btnDelete.setBounds(155,190,75,35);
  72. pane.add(btnDelete);
  73.  
  74. btnDelete.addActionListener(this);
  75. btnSearch.setBounds(230,190,75,35);
  76. pane.add(btnSearch);
  77.  
  78. btnSearch.addActionListener(this);
  79. btnExit.setBounds(130,260,75,35);
  80. pane.add(btnExit);
  81.  
  82. pane.setBackground(Color.black);
  83.  
  84. btnExit.addActionListener(this);
  85. setContentPane(pane);
  86. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  87. pane.setBorder(BorderFactory.createTitledBorder(
  88. BorderFactory.createEtchedBorder(), "Creating User Account"));
  89.  
  90. try{
  91. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  92. cn = DriverManager.getConnection("jdbc:odbc:User");
  93. System.err.println("Failed to load driver");
  94. e.printStackTrace();
  95. }
  96. catch(SQLException e){
  97. System.err.println("Unable to connect");
  98. e.printStackTrace();
  99. }
  100. }
  101.  
  102. public void actionPerformed(ActionEvent e){
  103. Object source = e.getSource();
  104. if(source == btnNew){
  105. try{
  106. String uname=txtUser.getText();
  107. String pass=txtPass.getText();
  108. String name1=txtName1.getText();
  109. String name2=txtName2.getText();
  110. if (!uname.equals("") && !pass.equals("")&& !name1.equals("") && !name2.equals("")) {
  111. st= cn.createStatement();
  112. ps=cn.prepareStatement("INSERT INTO Login" + " (username,password,name1,name2) " + " VALUES(?,?,?,?)");
  113. ps.setString(1,txtUser.getText());
  114. ps.setString(2,txtPass.getText());
  115. ps.setString(3,txtName1.getText());
  116. ps.setString(4,txtName2.getText());
  117. ps.executeUpdate();
  118. JOptionPane.showMessageDialog(null,"New account has been successfully added.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  119. txtUser.requestFocus(true);
  120. st.close();
  121. clear();
  122. }
  123. else{
  124. JOptionPane.showMessageDialog(null,"Please Fill Up The Empty Fields","Warning",JOptionPane.WARNING_MESSAGE);
  125. }
  126. }catch(SQLException sqlEx){
  127. sqlEx.printStackTrace();
  128. JOptionPane.showMessageDialog(null,"Unable to save!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);}
  129. }
  130. if(source == btnSearch){
  131. try{
  132.  
  133. String sUser ="";
  134. int tmp= 0;
  135. clear();
  136. sUser = JOptionPane.showInputDialog(null,"Enter Username to search.","Payroll System: User settings",JOptionPane.QUESTION_MESSAGE);
  137. st= cn.createStatement();
  138. ResultSet rs=st.executeQuery("SELECT * FROM Login WHERE username = '" + sUser + "'");
  139.  
  140. while(rs.next()){
  141. txtUser.setText(rs.getString(1));
  142. txtPass.setText(rs.getString(2));
  143. txtName1.setText(rs.getString(3));
  144. txtName2.setText(rs.getString(4));
  145. tmp=1;
  146. }
  147. st.close();
  148. if (tmp==0){
  149. JOptionPane.showMessageDialog(null,"No record found!!.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  150. }
  151. }catch(SQLException s){
  152. JOptionPane.showMessageDialog(null,"Unable to search!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);
  153. System.out.println("SQL Error" + s.toString() + " " + s.getErrorCode() + " " + s.getSQLState());
  154. }
  155. }
  156. if(source == btnUpdate){
  157. try{
  158.  
  159. String uname=txtUser.getText();
  160. String pass=txtPass.getText();
  161. String name1=txtName1.getText();
  162. String name2=txtName2.getText();
  163. if (!uname.equals("") && !pass.equals("")&& !name1.equals("") && !name2.equals("")) {
  164. st= cn.createStatement();
  165. PreparedStatement ps = cn.prepareStatement("UPDATE Login SET password = '" + txtPass.getText() + "',name1 = '" + txtName1.getText() + "',name2= '" + txtName2.getText()+ "'WHERE username = '" + txtUser.getText() + "'");
  166. ps.executeUpdate();
  167. JOptionPane.showMessageDialog(null,"Account has been successfully updated.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  168. txtUser.requestFocus(true);
  169. clear();
  170. st.close();
  171. }
  172. else{
  173. JOptionPane.showMessageDialog(null,"Please Fill Up The Empty Fields","Warning",JOptionPane.WARNING_MESSAGE);
  174. }
  175.  
  176. }catch (SQLException y){
  177. JOptionPane.showMessageDialog(null,"Unable to update!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);
  178. }
  179. }
  180. if(source==btnDelete){
  181. try{
  182. PreparedStatement ps = cn.prepareStatement("DELETE FROM Login WHERE username ='"+ txtUser.getText() + "'");
  183. ps.executeUpdate();
  184. JOptionPane.showMessageDialog(null,"Account has been successfully deleted.","Payroll System: User settings ",JOptionPane.INFORMATION_MESSAGE);
  185. txtUser.requestFocus(true);
  186. clear();
  187. st.close();
  188. }catch(SQLException s){
  189. JOptionPane.showMessageDialog(null,"Unable to delete!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);}
  190.  
  191. }if(source==btnExit){
  192. dispose();
  193. }
  194. }
  195. // public void frameUser(){
  196. public static void main(String[]args){
  197. UserSettings panel = new UserSettings();
  198. panel.setSize(370,350);
  199. panel.setVisible(true);
  200. panel.setLocation(350,200);
  201. panel.setResizable(false);
  202. }
  203. }
Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Visit and like my page on Facebook at: https://www.facebook.com/BermzISware Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Add new comment