Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. package pzlab4;
  2.  
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.JTable;
  8. import javax.swing.table.AbstractTableModel;
  9. import java.awt.Dimension;
  10. import java.awt.GridLayout;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.sql.Connection;
  16. import java.sql.DriverManager;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.ArrayList;
  22. import java.util.Scanner;
  23.  
  24. public class App extends JPanel implements ActionListener {
  25. JTable table = new JTable(new MyTableModel());
  26. public JButton b1 = new JButton("Refresh");
  27. private static ArrayList<DaneTabeli> datax = new ArrayList<DaneTabeli>();
  28. public static JFrame frame = new JFrame("TABELA");
  29. public static App test = new App();
  30. public static String adres, port, user, baza, password, url;
  31. // polacz z baza
  32. public Connection connect(String url, String user, String password) {
  33. Connection conn = null;
  34. try {
  35. conn = DriverManager.getConnection(url, user, password);
  36. System.out.println("Connected to the PostgreSQL server successfully.");
  37. } catch (SQLException e) {
  38. System.out.println(e.getMessage());
  39. }
  40.  
  41. return conn;
  42. }
  43.  
  44. public App() {
  45. super(new GridLayout(2, 0));
  46. b1.addActionListener(this);
  47.  
  48. table.setPreferredScrollableViewportSize(new Dimension(500, 70));
  49. table.setFillsViewportHeight(true);
  50.  
  51. // Create the scroll pane and add the table to it.
  52. JScrollPane scrollPane = new JScrollPane(table);
  53. // Add the scroll pane to this panel.
  54. add(scrollPane);
  55. add(b1);
  56.  
  57. }
  58.  
  59. // model tablicy
  60. class MyTableModel extends AbstractTableModel {
  61.  
  62. public String[] columnNames = { "kraj", "przychod" };
  63. public ArrayList<DaneTabeli> data = datax;
  64.  
  65. public int getColumnCount() {
  66. return columnNames.length;
  67. }
  68.  
  69. public int getRowCount() {
  70. return data.size();
  71. }
  72.  
  73. public String getColumnName(int col) {
  74. return columnNames[col];
  75. }
  76.  
  77. public void SetDatax(int i, String x, int y) {
  78. DaneTabeli element = new DaneTabeli(x, y);
  79. data.set(i, element);
  80. }
  81.  
  82. public Object getValueAt(int row, int col) {
  83. if (col == 0) {
  84. return data.get(row).kraj;
  85. } else {
  86. return data.get(row).przych;
  87. }
  88. }
  89.  
  90. }
  91.  
  92. // stworz gui
  93. private static void createAndShowGUI() {
  94. // Create and set up the window.
  95.  
  96. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  97. frame.setSize(800, 800);
  98.  
  99. // Create and set up the content pane.
  100. App newContentPane = new App();
  101. newContentPane.setOpaque(true); // content panes must be opaque
  102. frame.setContentPane(newContentPane);
  103.  
  104. // Display the window.
  105. frame.pack();
  106. frame.setVisible(true);
  107. }
  108.  
  109. // pobiera dane z bazy za pomoca zapytania
  110. public void pobierzdane(String url, String user, String password) {
  111.  
  112. String SQL = "SELECT customers.country kraj, SUM(orders.netamount) przychody "
  113. + "FROM customers INNER JOIN orders ON customers.customerid=orders.customerid "
  114. + "WHERE orders.orderdate BETWEEN '2004-01-01' AND '2004-06-01' "
  115. + "GROUP BY kraj ORDER BY przychody DESC ";
  116.  
  117. try (Connection conn = connect(url, user, password);
  118. Statement stmt = conn.createStatement();
  119. ResultSet rs = stmt.executeQuery(SQL)) {
  120. zaladujdane(rs);
  121. } catch (SQLException ex) {
  122. System.out.println(ex.getMessage());
  123. }
  124. }
  125.  
  126. // dodaje dane do listy
  127. private void zaladujdane(ResultSet rs) throws SQLException {
  128. while (rs.next()) {
  129. DaneTabeli x = new DaneTabeli(rs.getString("kraj"), rs.getInt("przychody"));
  130. datax.add(x);
  131.  
  132. }
  133. }
  134.  
  135. public static void main(String[] args) throws FileNotFoundException {
  136. Scanner odczyt = new Scanner(new File("dane.txt"));
  137. adres = odczyt.nextLine();
  138. port = odczyt.nextLine();
  139. baza = odczyt.nextLine();
  140. user = odczyt.nextLine();
  141. password = odczyt.nextLine();
  142. url = "jdbc:postgresql://" + adres + ":" + port + "/" + baza;
  143.  
  144. test.pobierzdane(url, user, password);
  145. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  146. public void run() {
  147. createAndShowGUI();
  148. }
  149. });
  150. }
  151.  
  152. // ponownie laczy z baza i odswierza dane w tablicy
  153. @Override
  154. public void actionPerformed(ActionEvent e) {
  155. datax.clear();
  156. test.pobierzdane(url, user, password);
  157. for (int i = 0; i < datax.size(); i++) {
  158. table.getModel().setValueAt(datax.get(i).kraj, i, 0);
  159. table.getModel().setValueAt(datax.get(i).przych, i, 1);
  160. }
  161. frame.setVisible(false);
  162. frame.setVisible(true);
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement