Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 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.awt.event.ActionEvent;
  14.  
  15. public class Crud {
  16.  
  17. private JFrame frame;
  18. Connection conn = null;
  19.  
  20. /**
  21. * Launch the application.
  22. */
  23. public static void main(String[] args) {
  24. EventQueue.invokeLater(new Runnable() {
  25. public void run() {
  26. try {
  27. Crud window = new Crud();
  28. window.frame.setVisible(true);
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. });
  34. }
  35.  
  36. /**
  37. * Create the application.
  38. */
  39. public Crud() {
  40. initialize();
  41. conn = SqlConnect.dbConnect();
  42. }
  43.  
  44. /**
  45. * Initialize the contents of the frame.
  46. */
  47. private void initialize() {
  48. frame = new JFrame();
  49. frame.setBounds(100, 100, 795, 594);
  50. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. frame.getContentPane().setLayout(null);
  52.  
  53. JLabel lblDatabaseCrudOperation = new JLabel("Database CRUD Operation Demo");
  54. lblDatabaseCrudOperation.setBounds(194, 0, 420, 26);
  55. frame.getContentPane().add(lblDatabaseCrudOperation);
  56.  
  57. JTextArea textArea = new JTextArea();
  58. textArea.setBounds(0, 0, 304, 359);
  59.  
  60. JPanel panel = new JPanel();
  61. panel.setBounds(444, 70, 304, 359);
  62. frame.getContentPane().add(panel);
  63. panel.setLayout(null);
  64.  
  65. JTextArea textArea_1 = new JTextArea();
  66. textArea_1.setBounds(39, 70, 351, 366);
  67. frame.getContentPane().add(textArea_1);
  68.  
  69. JButton btnLoad = new JButton("Load");
  70. btnLoad.addActionListener(new ActionListener() {
  71. public void actionPerformed(ActionEvent arg0) {
  72.  
  73. try {
  74.  
  75. String query = "Select * from Employee";
  76. PreparedStatement pst = conn.prepareStatement(query);
  77.  
  78. ResultSet rs = pst.executeQuery();
  79. String display = "";
  80.  
  81. while(rs.next()) {
  82. display += rs.getString("Fname")+ " ";
  83. display += rs.getString("Lname")+ " ";
  84.  
  85. Integer age = rs.getInt("Age");
  86. display += rs.getInt("Age")+ " ";
  87.  
  88. Double sal = rs.getDouble("Salary");
  89. //display += rs.getDouble("Salary")+ " ";
  90. display += sal.toString()+ " ";
  91. display += "\n";
  92. JOptionPane.showMessageDialog(null, display);
  93. }//end of while
  94.  
  95. //textArea.setText(display);
  96. textArea_1.setText(display);
  97.  
  98. } catch (Exception e) {
  99. // TODO: handle exception
  100. }
  101.  
  102. }
  103. });
  104. btnLoad.setBounds(509, 21, 141, 35);
  105. frame.getContentPane().add(btnLoad);
  106. //
  107. // JTextArea textArea_1 = new JTextArea();
  108. // textArea_1.setBounds(39, 70, 351, 366);
  109. // frame.getContentPane().add(textArea_1);
  110.  
  111. // JPanel panel = new JPanel();
  112. // panel.setBounds(444, 70, 304, 359);
  113. // frame.getContentPane().add(panel);
  114. // panel.setLayout(null);
  115. //
  116. // JTextArea textArea = new JTextArea();
  117. // textArea.setBounds(0, 0, 304, 359);
  118. // panel.add(textArea);
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement