Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.Vector;
  9. import javax.swing.JButton;
  10. import javax.swing.JComboBox;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTable;
  15. import com.mysql.jdbc.Connection;
  16. import com.mysql.jdbc.ResultSetMetaData;
  17. import com.mysql.jdbc.Statement;
  18.  
  19. public class Frame_Application {
  20. //Declaram variabilele globala
  21. public JFrame frame;
  22. public JPanel panel, panelBottom, panelTop;
  23. public JTable table;
  24. public JScrollPane containerScroll;
  25. public Vector<Vector<Object>> data;
  26. public Vector<String> columnNames;
  27. public JComboBox<String> comboBoxSoferi;
  28. public Connection connection = null;
  29. public Statement statement = null;
  30.  
  31. public void linkDatabase() {
  32. String DB_URL = "jdbc:mysql://localhost/agentie_livrari?zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8";
  33. String USER = "root";
  34. String PASS = "";
  35. try {
  36. Class.forName("com.mysql.jdbc.Driver");
  37. connection = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);
  38. statement = (Statement) connection.createStatement();
  39. } catch (ClassNotFoundException | SQLException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44. public void createDriversList() {
  45. String sqlSoferi = "Select nume from soferi";
  46. ResultSet rs = null;
  47. ArrayList listaSoferi = new ArrayList();
  48. try {
  49. rs = statement.executeQuery(sqlSoferi);
  50. while(rs.next()) {
  51. listaSoferi.add(rs.getString(1));
  52. }
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. comboBoxSoferi = new JComboBox(listaSoferi.toArray());
  57. panelTop.add(comboBoxSoferi, BorderLayout.CENTER);
  58. }
  59.  
  60. public void populateTable() {
  61. String sql = "SELECT * from livrari";
  62. try {
  63. ResultSet rs2 = statement.executeQuery(sql);
  64. ResultSetMetaData metaData = (ResultSetMetaData) rs2.getMetaData();
  65.  
  66. columnNames = new Vector<String>();
  67. int columnCount = metaData.getColumnCount();
  68. for (int column = 1; column <= columnCount; column++) {
  69. columnNames.add(metaData.getColumnName(column));
  70. }
  71.  
  72.  
  73. data = new Vector<Vector<Object>>();
  74. while (rs2.next()) {
  75. Vector<Object> vector = new Vector<Object>();
  76. for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
  77. vector.add(rs2.getObject(columnIndex));
  78. }
  79. data.add(vector);
  80. }
  81. //initializam tabel
  82. table = new JTable(data, columnNames);
  83. //creem containerul cu scroll automat
  84. containerScroll = new JScrollPane(table);
  85. //setam o proprietate a tabelului care ii permite sa isi modifice inaltimea
  86. table.setFillsViewportHeight(true);
  87. //adaugam tabelul pe panou in centru
  88. panel.add(containerScroll, BorderLayout.CENTER);
  89. //setam frame-ul sa fie vizibil
  90. frame.setVisible(true);
  91.  
  92. } catch (ClassNotFoundException | SQLException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96.  
  97. //Creaza frame-ul
  98. public void createFrame() {
  99. //initializam frame-ul
  100. frame = new JFrame();
  101. //setam dimensiunea aplicatiei
  102. frame.setSize(800, 600);
  103. //setam butonul de x sa inchida aplicatia
  104. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  105. //setam titlul aplicatie
  106. frame.setTitle("Proiect 3");
  107. }
  108.  
  109.  
  110.  
  111. public void createButton(){
  112. JButton buton3= new JButton("buton3");
  113. panelBottom.add(buton3, BorderLayout.EAST);
  114. //button1.setPreferredSize(new Dimension(50,100));
  115.  
  116. JButton buton2= new JButton("buton2");
  117. panelBottom.add(buton2, BorderLayout.CENTER);
  118. //button1.setPreferredSize(new Dimension(50,100));
  119.  
  120. JButton buton1= new JButton("buton1");
  121. panelBottom.add(buton1, BorderLayout.WEST);
  122. //button1.setPreferredSize(new Dimension(50,100));
  123.  
  124.  
  125. JButton butonFiltreaza= new JButton("Filtreaza");
  126. panelTop.add(butonFiltreaza, BorderLayout.EAST);
  127. ActionListener actiuneFiltreaza = new listenToFiltreaza();
  128. butonFiltreaza.addActionListener(actiuneFiltreaza);
  129. //button1.setPreferredSize(new Dimension(50,100));
  130.  
  131. }
  132.  
  133.  
  134.  
  135.  
  136. public void createPanel() {
  137. //initializam panou cu un layout manager de Border Layout
  138. panel = new JPanel(new BorderLayout());
  139. panelBottom = new JPanel(new BorderLayout());
  140. panelTop = new JPanel(new BorderLayout());
  141. //adaugam panoul pe frame.
  142. frame.add(panel);
  143. panel.add(panelBottom, BorderLayout.SOUTH);
  144. panel.add(panelTop, BorderLayout.NORTH);
  145. }
  146.  
  147. @SuppressWarnings({ "rawtypes", "unchecked" })
  148. public void filtreazaDate(String numeSofer) {
  149. //setam numele driver-ului pentru baza de date mysql
  150. //prestabilit ptr a sti ca e baza de date mysql
  151. try {
  152. columnNames.clear();
  153. data.clear();
  154. Class.forName("com.mysql.jdbc.Driver");
  155. //setam link-ul conexiunii la baza de date de pe localhost(cea creata de noi)
  156. //prestabilit ptr a sti ca ne conectam la o baza de date locala
  157. String DB_URL = "jdbc:mysql://localhost/agentie_livrari?zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8";
  158. //setam numele de utilizator pentru conectarea la baza de date
  159. String USER = "root";
  160. //setam parola pentru conectarea la baza de date
  161. String PASS = "";
  162. //aici creem conexiunea propriu-zisa folosind datele de mai sus.
  163. Connection connection = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);
  164. //aici creem o afirmatie
  165. Statement statement = (Statement) connection.createStatement();
  166. //creem comanda sql pentru a lua numele soferilor din baza de date
  167. String sqlSoferi = "Select nume from soferi";
  168. //se creaza o variabila de tip resultset unde se memoreaza rezultatul comenzii sql
  169. ResultSet rs = statement.executeQuery(sqlSoferi);
  170. //se creaza un arraylist(o lista de obiecte dinamica) goala
  171.  
  172.  
  173.  
  174. //se creaza o comanda sql ptr popularea tabelului
  175. String sql = "SELECT * from livrari WHERE sofer='"+numeSofer+"'";
  176. //se executa comanda sql
  177. ResultSet rs2 = statement.executeQuery(sql);
  178.  
  179.  
  180. ResultSetMetaData metaData = (ResultSetMetaData) rs2.getMetaData();
  181.  
  182. // names of columns
  183. columnNames = new Vector<String>();
  184. int columnCount = metaData.getColumnCount();
  185. for (int column = 1; column <= columnCount; column++) {
  186. columnNames.add(metaData.getColumnName(column));
  187. }
  188.  
  189.  
  190. data = new Vector<Vector<Object>>();
  191. while (rs2.next()) {
  192. Vector<Object> vector = new Vector<Object>();
  193. for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
  194. vector.add(rs2.getObject(columnIndex));
  195. }
  196. data.add(vector);
  197. }
  198. //initializam tabel
  199. table = new JTable(data, columnNames);
  200. //creem containerul cu scroll automat
  201. containerScroll = new JScrollPane(table);
  202. //setam o proprietate a tabelului care ii permite sa isi modifice inaltimea
  203. table.setFillsViewportHeight(true);
  204. //adaugam tabelul pe panou in centru
  205. panel.add(containerScroll, BorderLayout.CENTER);
  206. //setam frame-ul sa fie vizibil
  207. frame.setVisible(true);
  208.  
  209. } catch (ClassNotFoundException | SQLException e) {
  210. e.printStackTrace();
  211. }
  212. }
  213.  
  214.  
  215.  
  216. public class listenToFiltreaza implements ActionListener {
  217. @Override
  218. public void actionPerformed(ActionEvent arg0) {
  219. //Ce scriem aici se intampla cand apesi pe butonul click
  220. String soferAles = (String) comboBoxSoferi.getSelectedItem();
  221. filtreazaDate(soferAles);
  222. System.out.println(soferAles);
  223.  
  224. }
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement