Advertisement
Guest User

update

a guest
Jan 2nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. import java.sql.*;
  5.  
  6. public class updateMember extends JFrame implements ActionListener
  7. {
  8. private JLabel MemberNameLabel, genderLabel, ageLabel, userIdLabel, passwordLabel, addressLabel,contactLabel;
  9. private JTextField MemberNameTF, genderTF, ageTF, userIdTF, contactTF, addressTF;
  10. private JPasswordField passwordPF;
  11. private JButton buttonBack, buttonLogout, buttonInsert,buttonUpdate;
  12. private JPanel panel;
  13. private String userId,password,gender,MemberName,contact, address;
  14. private int age;
  15. public updateMember()
  16. {
  17.  
  18. super("Gym Management System - Create New Account Window");
  19.  
  20. this.setSize(600, 400);
  21. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.  
  23. panel = new JPanel();
  24. panel.setLayout(null);
  25.  
  26. MemberNameLabel = new JLabel("Enter Name : ");
  27. MemberNameLabel.setBounds(100,10,150,30);
  28. panel.add(MemberNameLabel);
  29.  
  30. MemberNameTF = new JTextField();
  31. MemberNameTF.setBounds(260,10,100,30);
  32. panel.add(MemberNameTF);
  33.  
  34. userIdLabel = new JLabel("Enter Member ID : ");
  35. userIdLabel.setBounds(100,50,150,30);
  36. panel.add(userIdLabel);
  37.  
  38. userIdTF = new JTextField();
  39. userIdTF.setBounds(260,50,100,30);
  40. panel.add(userIdTF);
  41.  
  42. ageLabel = new JLabel("Enter Member's Age : ");
  43. ageLabel.setBounds(100,90,150,30);
  44. panel.add(ageLabel);
  45.  
  46. ageTF = new JTextField();
  47. ageTF.setBounds(260,90,100,30);
  48. panel.add(ageTF);
  49.  
  50. genderLabel = new JLabel("Enter Member's Gender : ");
  51. genderLabel.setBounds(100,130,150,30);
  52. panel.add(genderLabel);
  53.  
  54. genderTF = new JTextField();
  55. genderTF.setBounds(260,130,100,30);
  56. panel.add(genderTF);
  57.  
  58. contactLabel = new JLabel("Contact Info : ");
  59. contactLabel.setBounds(100,170,150,30);
  60. panel.add(contactLabel);
  61.  
  62. contactTF = new JTextField();
  63. contactTF.setBounds(260,170,100,30);
  64. panel.add(contactTF);
  65.  
  66. addressLabel = new JLabel("Enter Member's Address : ");
  67. addressLabel.setBounds(100,210,150,30);
  68. panel.add(addressLabel);
  69.  
  70. addressTF = new JTextField();
  71. addressTF.setBounds(260,210,100,30);
  72. panel.add(addressTF);
  73.  
  74.  
  75.  
  76. passwordLabel = new JLabel("Enter Password : ");
  77. passwordLabel.setBounds(100, 250, 150, 30);
  78. panel.add(passwordLabel);
  79.  
  80. passwordPF =new JPasswordField();
  81. passwordPF.setBounds(260, 250, 100, 30);
  82. panel.add(passwordPF);
  83.  
  84. buttonBack = new JButton("Back");
  85. buttonBack.setBounds(100, 300, 80, 30);
  86. buttonBack.addActionListener(this);
  87. panel.add(buttonBack);
  88.  
  89. buttonInsert = new JButton("Insert");
  90. buttonInsert.setBounds(190, 300, 80, 30);
  91. buttonInsert.addActionListener(this);
  92. panel.add(buttonInsert);
  93.  
  94. buttonLogout = new JButton("Logout");
  95. buttonLogout.setBounds(280, 300, 80, 30);
  96. buttonLogout.addActionListener(this);
  97. panel.add(buttonLogout);
  98.  
  99. buttonUpdate = new JButton("Update");
  100. buttonUpdate.setBounds(350, 300, 80, 30);
  101. buttonUpdate.addActionListener(this);
  102. panel.add(buttonUpdate);
  103.  
  104. this.add(panel);
  105. }
  106. public void actionPerformed(ActionEvent ae)
  107. {
  108. String buttonClicked = ae.getActionCommand();
  109.  
  110. if(buttonClicked.equals(buttonBack.getText()))
  111. {
  112.  
  113. AdminHome adh = new AdminHome(userId,password,gender,MemberName,age,contact,address);
  114. this.setVisible(false);
  115. adh.setVisible(true);
  116.  
  117. }
  118. else if(buttonClicked.equals(buttonUpdate.getText()))
  119. {
  120. UpdateintoDB();
  121. }
  122. else if(buttonClicked.equals(buttonLogout.getText()))
  123. {
  124. Login l = new Login();
  125. l.setVisible(true);
  126. this.setVisible(false);
  127. }
  128. }
  129.  
  130. private boolean validateFields(){
  131.  
  132. if( userIdTF.getText().isEmpty() | passwordPF.getText().isEmpty() | genderTF.getText().isEmpty() | MemberNameTF.getText().isEmpty() | ageTF.getText().isEmpty() | contactTF.getText().isEmpty() | addressTF.getText().isEmpty() ){
  133.  
  134. JOptionPane.showMessageDialog(null, "Add Proper Information");
  135. return false;
  136.  
  137. }
  138. return true;
  139.  
  140. }
  141.  
  142. public void UpdateintoDB()
  143. { String update_query = "UPDATE gymdatabase SET userId = '"+userIdTF.getText()+"', password = '"+passwordPF.getText()+"',gender = '"+genderTF.getText()+"',MemberName = '"+MemberNameTF.getText()+"'age='"+ageTF.getText()+"',contact='"+contactTF.getText()+"',address='"+addressTF.getText()+"'WHERE userId = '"+userIdTF.getText()+"';";
  144. System.out.println(update_query);
  145. if (validateFields()){
  146.  
  147. try
  148. {
  149.  
  150. Class.forName("com.mysql.jdbc.Driver");
  151. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Gym", "root", "");
  152. Statement stm = con.createStatement();
  153. stm.execute(update_query);
  154. stm.close();
  155. con.close();
  156.  
  157. }
  158. catch(Exception ex)
  159. {
  160. System.out.println("Exception : " +ex.getMessage());
  161. }
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement