Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JOptionPane;
  6. import javax.swing.JButton;
  7. import javax.swing.JTextArea;
  8. import javax.swing.JPanel;
  9. import java.awt.event.ActionListener;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.awt.event.ActionEvent;
  15. import javax.swing.JTextField;
  16.  
  17. public class Crud {
  18.  
  19. private JFrame frame;
  20. Connection conn = null;
  21. private JTextField textFname;
  22. private JTextField textLname;
  23. private JTextField textAge;
  24. private JTextField textSalary;
  25.  
  26. /**
  27. * Launch the application.
  28. */
  29. public static void main(String[] args) {
  30. EventQueue.invokeLater(new Runnable() {
  31. public void run() {
  32. try {
  33. Crud window = new Crud();
  34. window.frame.setVisible(true);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. });
  40. }
  41.  
  42. /**
  43. * Create the application.
  44. */
  45. public Crud() {
  46. initialize();
  47. conn = SqlConnect.dbConnect();
  48. }
  49.  
  50. /**
  51. * Initialize the contents of the frame.
  52. */
  53. private void initialize() {
  54. frame = new JFrame();
  55. frame.setBounds(100, 100, 795, 594);
  56. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57. frame.getContentPane().setLayout(null);
  58.  
  59. JLabel lblDatabaseCrudOperation = new JLabel("Database CRUD Operation Demo");
  60. lblDatabaseCrudOperation.setBounds(195, 16, 420, 26);
  61. frame.getContentPane().add(lblDatabaseCrudOperation);
  62.  
  63. JTextArea textArea = new JTextArea();
  64. textArea.setBounds(0, 0, 304, 359);
  65.  
  66. JTextArea textArea_1 = new JTextArea();
  67. textArea_1.setBounds(508, 119, 240, 366);
  68. frame.getContentPane().add(textArea_1);
  69.  
  70. JButton btnLoad = new JButton("Load");
  71. btnLoad.addActionListener(new ActionListener() {
  72. public void actionPerformed(ActionEvent arg0) {
  73.  
  74. try {
  75.  
  76. String query = "Select * from Employee";
  77. PreparedStatement pst = conn.prepareStatement(query);
  78.  
  79. ResultSet rs = pst.executeQuery();
  80. String display = "";
  81.  
  82. while(rs.next()) {
  83. display += rs.getString("Fname")+ " ";
  84. display += rs.getString("Lname")+ " ";
  85.  
  86. Integer age = rs.getInt("Age");
  87. display += rs.getInt("Age")+ " ";
  88.  
  89. Double sal = rs.getDouble("Salary");
  90. display += sal.toString()+ " ";
  91. display += "\n";
  92. //JOptionPane.showMessageDialog(null, display); //to display every new entry
  93. }//end of while
  94.  
  95.  
  96. textArea_1.setText(display);
  97.  
  98. } catch (Exception e) {
  99. JOptionPane.showMessageDialog(null, "Error reading Person Table");
  100. }
  101.  
  102. }
  103. });
  104. btnLoad.setBounds(607, 63, 141, 35);
  105. frame.getContentPane().add(btnLoad);
  106.  
  107. JLabel lblNewLabel = new JLabel("FirstName:");
  108. lblNewLabel.setBounds(21, 88, 122, 26);
  109. frame.getContentPane().add(lblNewLabel);
  110.  
  111. JLabel lblLNewLabel_1 = new JLabel("LastName:");
  112. lblLNewLabel_1.setBounds(21, 151, 122, 26);
  113. frame.getContentPane().add(lblLNewLabel_1);
  114.  
  115. JLabel lblNewLabel_2 = new JLabel("Age:");
  116. lblNewLabel_2.setBounds(21, 221, 122, 26);
  117. frame.getContentPane().add(lblNewLabel_2);
  118.  
  119. JLabel lblNewLabel_3 = new JLabel("Salary:");
  120. lblNewLabel_3.setBounds(21, 295, 122, 26);
  121. frame.getContentPane().add(lblNewLabel_3);
  122.  
  123. textFname = new JTextField();
  124. textFname.setBounds(146, 85, 186, 32);
  125. frame.getContentPane().add(textFname);
  126. textFname.setColumns(10);
  127.  
  128. textLname = new JTextField();
  129. textLname.setBounds(146, 148, 186, 32);
  130. frame.getContentPane().add(textLname);
  131. textLname.setColumns(10);
  132.  
  133. textAge = new JTextField();
  134. textAge.setBounds(146, 218, 186, 32);
  135. frame.getContentPane().add(textAge);
  136. textAge.setColumns(10);
  137.  
  138. textSalary = new JTextField();
  139. textSalary.setBounds(146, 292, 186, 32);
  140. frame.getContentPane().add(textSalary);
  141. textSalary.setColumns(10);
  142.  
  143. JButton btnClear = new JButton("Clear");
  144. btnClear.addActionListener(new ActionListener() {
  145. public void actionPerformed(ActionEvent arg0) {
  146.  
  147. textArea_1.setText("");
  148. textFname.setText("");
  149. textLname.setText("");
  150. textAge.setText("");
  151. textSalary.setText("");
  152.  
  153. }
  154. });
  155. btnClear.setBounds(21, 394, 141, 35);
  156. frame.getContentPane().add(btnClear);
  157.  
  158. JButton btnFind = new JButton("Find");
  159. btnFind.addActionListener(new ActionListener() {
  160. public void actionPerformed(ActionEvent e) {
  161.  
  162. String query = "select * from Employee where Fname = ? and Lname = ?";
  163. try {
  164. PreparedStatement pst = conn.prepareStatement(query);
  165.  
  166. pst.setString(1, textFname.getText()); //first name
  167. pst.setString(2, textLname.getText()); //last name
  168.  
  169. ResultSet rs = pst.executeQuery();
  170. int count = 0;
  171.  
  172. rs.next();
  173. count ++;
  174.  
  175. if(count == 1) {
  176.  
  177. Integer iage = rs.getInt("Age");
  178. Double dsal = rs.getDouble("Salary");
  179. textAge.setText(iage.toString());
  180. textSalary.setText(dsal.toString());
  181. }
  182. else JOptionPane.showMessageDialog(null, "Invalid First/Last name");
  183.  
  184. } catch (SQLException e1) {
  185.  
  186. e1.printStackTrace();
  187. }
  188.  
  189. }
  190. });
  191. btnFind.setBounds(191, 394, 141, 35);
  192. frame.getContentPane().add(btnFind);
  193.  
  194. JButton btnInsert = new JButton("Insert");
  195. btnInsert.addActionListener(new ActionListener() {
  196. public void actionPerformed(ActionEvent e) {
  197.  
  198. String query = "Insert into Employee (Fname, Lname, Age, Salary) Values (?,?,?,?)";
  199.  
  200. try {
  201. PreparedStatement pst = conn.prepareStatement(query);
  202. pst.setString(1, textFname.getText());
  203. pst.setString(2, textLname.getText());
  204.  
  205. String sage = textAge.getText();
  206. String ssal = textSalary.getText();
  207.  
  208. int iage = Integer.parseInt(sage);
  209. double dsal = Double.parseDouble(ssal);
  210.  
  211. pst.setInt(3, iage);
  212. pst.setDouble(4, dsal);
  213.  
  214. pst.execute();
  215.  
  216. JOptionPane.showMessageDialog(null, "Record added to Employee table");
  217.  
  218. } catch (Exception e2) {
  219. JOptionPane.showMessageDialog(null, "Error Inserting record to Employee table");
  220. //JOptionPane.showMessageDialog(null, e2.getMessage());
  221.  
  222. }
  223.  
  224.  
  225. }
  226. });
  227. btnInsert.setBounds(353, 394, 141, 35);
  228. frame.getContentPane().add(btnInsert);
  229.  
  230. JButton btnDelete = new JButton("Delete");
  231. btnDelete.setBounds(21, 450, 141, 35);
  232. frame.getContentPane().add(btnDelete);
  233.  
  234. JButton btnUpdate = new JButton("Update");
  235. btnUpdate.setBounds(191, 450, 141, 35);
  236. frame.getContentPane().add(btnUpdate);
  237.  
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement