Guest User

Untitled

a guest
Feb 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. package pl.kti.pk2.dbdemo.gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.sql.ResultSet;
  10. import java.sql.Statement;
  11.  
  12. import javax.swing.BorderFactory;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPanel;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTable;
  19. import javax.swing.JTextArea;
  20.  
  21. import pl.kti.pk2.dbdemo.data.CachingResultSetTableModel;
  22. import pl.kti.pk2.dbdemo.data.DBManager;
  23.  
  24. public class DBDemoFrame extends JFrame {
  25.  
  26. private static final long serialVersionUID = -6846315193410091203L;
  27.  
  28. public static void main(String[] args) {
  29. JFrame app_frame = new DBDemoFrame();
  30. app_frame.setVisible(true);
  31. }
  32.  
  33. private JTable _table;
  34. private CachingResultSetTableModel _tableModel;
  35. private JButton _showAll;
  36. private JButton _showLatest;
  37. private JButton _Departments;
  38. private JButton _EmptfromtheDept;
  39. private JButton _Addnewdept;
  40. private JButton _newemployee;
  41.  
  42. public DBDemoFrame() {
  43. super("database connectivity demo");
  44. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45. setSize(550, 650);
  46. setLocationRelativeTo(null);
  47.  
  48. initGUI();
  49. }
  50.  
  51. private void initGUI() {
  52. getContentPane().setLayout(new BorderLayout());
  53.  
  54. // query area
  55. getContentPane().setLayout(new BorderLayout(2,1));
  56.  
  57. JPanel actionPanel = new JPanel();
  58. actionPanel.setLayout(new GridLayout(5, 1));
  59. getContentPane().add(actionPanel,BorderLayout.NORTH);
  60.  
  61. _Addnewdept = new JButton("Add new dept");
  62. actionPanel.add(_Addnewdept);
  63.  
  64. _newemployee = new JButton("Add new employee");
  65. actionPanel.add(_newemployee);
  66.  
  67. _Departments= new JButton("Departments");
  68. actionPanel.add(_Departments);
  69. _Departments.addActionListener( new ActionListener(){
  70. public void actionPerformed(ActionEvent arg0){
  71.  
  72. Departments();
  73. }
  74.  
  75. });
  76. _EmptfromtheDept= new JButton(" Empt. from Dept.");
  77. actionPanel.add(_EmptfromtheDept);
  78. _EmptfromtheDept.addActionListener( new ActionListener(){
  79. public void actionPerformed(ActionEvent arg0){
  80. EmptfromtheDept();
  81. }
  82. });
  83.  
  84. _showAll = new JButton("ShowAll");
  85. actionPanel.add(_showAll);
  86.  
  87. _showAll.addActionListener(new ActionListener() {
  88. public void actionPerformed(ActionEvent arg0) {
  89. showAll();
  90. }
  91. });
  92.  
  93. _showLatest = new JButton("ShowLatest");
  94. actionPanel.add(_showLatest);
  95.  
  96. _showLatest.addActionListener(new ActionListener() {
  97. public void actionPerformed(ActionEvent arg0) {
  98. showLatest();
  99. }
  100. });
  101.  
  102. // data table
  103. _tableModel = new CachingResultSetTableModel(null);
  104. _table = new JTable(_tableModel);
  105. JScrollPane tableScroll = new JScrollPane(_table);
  106. add(tableScroll, BorderLayout.CENTER);
  107.  
  108.  
  109.  
  110.  
  111.  
  112. }
  113. protected void EmptfromtheDept(){
  114. try{
  115. int x;
  116. x = _table.getSelectedRow();
  117. x = (Integer) _tableModel.getValueAt(x, 0);
  118. String query= "select * from dzialy, pracownicy where pracownicy.idd_fk="+x +" and dzialy.idd ="+x+" ;";
  119. Statement stmt = DBManager.getConnection().createStatement();
  120. ResultSet queryResult = stmt.executeQuery(query);
  121. _tableModel.setResultSet(queryResult);
  122. queryResult.close();
  123. stmt.close();
  124.  
  125. }
  126. catch(Exception e) {
  127.  
  128. }
  129.  
  130. }
  131.  
  132. protected void Departments(){
  133. try{
  134. String query = "select * from dzialy;";
  135. Statement stmt = DBManager.getConnection().createStatement();
  136. ResultSet queryResult = stmt.executeQuery(query);
  137. // pass the resultSet to the table model (use setResultSet method from the model)
  138. _tableModel.setResultSet(queryResult);
  139. // close the resultSet and statement
  140. queryResult.close();
  141. stmt.close();
  142.  
  143. }
  144. catch(Exception e) {
  145. }
  146.  
  147.  
  148. }
  149. protected void showAll()
  150. {
  151. try
  152. {
  153. String query = "select * from pracownicy;";
  154. Statement stmt = DBManager.getConnection().createStatement();
  155. ResultSet queryResult = stmt.executeQuery(query);
  156. // pass the resultSet to the table model (use setResultSet method from the model)
  157. _tableModel.setResultSet(queryResult);
  158. // close the resultSet and statement
  159. queryResult.close();
  160. stmt.close();
  161.  
  162. }
  163. catch(Exception e) {
  164. // .setText(e.getMessage());
  165. }
  166. }
  167.  
  168. protected void showLatest()
  169. {
  170. try
  171. {
  172. String query = "select * from pracownicy " +
  173. "ORDER BY Id desc LIMIT 1;";
  174. Statement stmt = DBManager.getConnection().createStatement();
  175. ResultSet queryResult = stmt.executeQuery(query);
  176. // pass the resultSet to the table model (use setResultSet method from the model)
  177. _tableModel.setResultSet(queryResult);
  178. // close the resultSet and statement
  179. queryResult.close();
  180. stmt.close();
  181.  
  182. }
  183. catch(Exception e) {
  184.  
  185. }
  186. }
  187.  
  188.  
  189. }
Add Comment
Please, Sign In to add comment