Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. package customereditor;
  2.  
  3. public class Customer {
  4.  
  5. private int ID;
  6. private String name;
  7. private String surname;
  8.  
  9. public int getID() {
  10. return ID;
  11. }
  12.  
  13. public void setID(int ID) {
  14. this.ID = ID;
  15. }
  16.  
  17. public String getName() {
  18. return name;
  19. }
  20.  
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24.  
  25. public String getSurname() {
  26. return surname;
  27. }
  28.  
  29. public void setSurname(String surname) {
  30. this.surname = surname;
  31. }
  32.  
  33. }
  34.  
  35. package customereditor;
  36.  
  37. import java.sql.*;
  38. import java.util.*;
  39. import java.util.logging.*;
  40.  
  41. public class CustomerEditorDao {
  42.  
  43. public List<Customer> getCustomersByPage(int page) {
  44. List<Customer> customers = new ArrayList();
  45. try {
  46. Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/CustomerEditorDb");
  47. Statement stmt = conn.createStatement();
  48. String subSql = "SELECT * FROM Customer OFFSET %d ROWS FETCH NEXT 5 ROWS ONLY";
  49. String sql = String.format(subSql, page);
  50. ResultSet rs = stmt.executeQuery(sql);
  51. while (rs.next()) {
  52. Customer customer = new Customer();
  53. customer.setID(rs.getInt(1));
  54. customer.setName(rs.getString(2));
  55. customer.setSurname((rs.getString(3)));
  56. customers.add(customer);
  57. }
  58.  
  59. } catch (SQLException ex) {
  60. Logger.getLogger(CustomerEditorDao.class.getName()).log(Level.SEVERE, null, ex);
  61. }
  62. return customers;
  63. }
  64.  
  65. public int getRowCount() {
  66. int columns = 0;
  67. try {
  68. Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/CustomerEditorDb");
  69. Statement stmt = conn.createStatement();
  70. String sql = "SELECT COUNT(*) FROM Customer";
  71. ResultSet rs = stmt.executeQuery(sql);
  72. while (rs.next()) {
  73. columns = rs.getInt(1);
  74. }
  75.  
  76. } catch (SQLException ex) {
  77. Logger.getLogger(CustomerEditorDao.class.getName()).log(Level.SEVERE, null, ex);
  78. }
  79. return columns;
  80. }
  81.  
  82. }
  83.  
  84. package customereditor;
  85.  
  86. import javax.swing.*;
  87.  
  88. public class CustomerEditorMain {
  89.  
  90.  
  91. private static void createAndShowGUI() {
  92. //Create and set up the window.
  93. JFrame frame = new JFrame("TableDemo");
  94. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  95.  
  96. CustomerView view = new CustomerView();
  97. frame.add(view);
  98.  
  99. //Display the window.
  100. frame.pack();
  101. frame.setVisible(true);
  102. }
  103.  
  104. public static void main(String[] args) {
  105. //Schedule a job for the event-dispatching thread:
  106. //creating and showing this application's GUI.
  107. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  108. public void run() {
  109. createAndShowGUI();
  110. }
  111. });
  112. }
  113.  
  114. }
  115.  
  116. package customereditor;
  117.  
  118. import java.util.List;
  119. import javax.swing.table.AbstractTableModel;
  120.  
  121. public class CustomerTableModel extends AbstractTableModel {
  122.  
  123. private final String[] COLUMN_NAMES;
  124. List<Customer> customers;
  125.  
  126. public CustomerTableModel(List<Customer> customers) {
  127. this.COLUMN_NAMES = new String[]{"ID", "Name", "Surname"};
  128. this.customers = customers;
  129. }
  130.  
  131. @Override
  132. public int getColumnCount() {
  133.  
  134. return COLUMN_NAMES.length;
  135. }
  136.  
  137. @Override
  138. public int getRowCount() {
  139.  
  140. return customers.size();
  141. }
  142.  
  143. @Override
  144. public String getColumnName(int col) {
  145.  
  146. return COLUMN_NAMES[col];
  147. }
  148.  
  149. @Override
  150. public Object getValueAt(int row, int col) {
  151.  
  152. Customer customer = customers.get(row);
  153.  
  154. switch (col) {
  155. case 0:
  156. return customer.getID();
  157. case 1:
  158. return customer.getName();
  159. case 2:
  160. return customer.getSurname();
  161. }
  162.  
  163. return customer;
  164. }
  165.  
  166. }
  167.  
  168. package customereditor;
  169.  
  170. import java.awt.*;
  171. import java.awt.event.ActionEvent;
  172. import java.awt.event.ActionListener;
  173. import java.util.HashMap;
  174. import java.util.Map;
  175. import javax.swing.*;
  176.  
  177. public class CustomerView extends JPanel {
  178.  
  179. private CustomerTableModel model;
  180. private JTable table;
  181. private CustomerEditorDao dao;
  182. JButton next;
  183. JButton previous;
  184.  
  185. public CustomerView() {
  186. initComponents();
  187. }
  188.  
  189. private void initComponents() {
  190. setLayout(new BorderLayout());
  191. dao = new CustomerEditorDao();
  192. model = new CustomerTableModel(dao.getCustomersByPage(0));
  193. table = new JTable(model);
  194. add(table.getTableHeader(), BorderLayout.PAGE_START);
  195. add(table, BorderLayout.CENTER);
  196.  
  197. JPanel buttonsPanel = new JPanel();
  198. Map<Integer, JButton> buttons = new HashMap<Integer, JButton>();
  199. int pages;
  200. int rowCount = dao.getRowCount();
  201. if (rowCount % 5 == 0) {
  202. pages = rowCount / 5;
  203. } else {
  204. pages = rowCount / 5 + 1;
  205. }
  206.  
  207. for (int i = 1; i <= pages; i++) {
  208. String buttonText = Integer.toString(i);
  209. JButton button = new JButton(buttonText);
  210. buttons.put(i, button);
  211. buttons.get(i).addActionListener(new ButtonActionListener(table, buttons.get(i)));
  212. buttonsPanel.add(buttons.get(i));
  213. }
  214.  
  215. add(buttonsPanel, BorderLayout.PAGE_END);
  216.  
  217. }
  218.  
  219. }
  220.  
  221. class ButtonActionListener implements ActionListener {
  222.  
  223. JTable table;
  224. JButton button;
  225.  
  226. public ButtonActionListener(JTable table, JButton button) {
  227. this.table = table;
  228. this.button = button;
  229. }
  230.  
  231. @Override
  232. public void actionPerformed(ActionEvent ae) {
  233. CustomerEditorDao dao = new CustomerEditorDao();
  234. int number = Integer.parseInt(button.getText());
  235. CustomerTableModel model = new CustomerTableModel(dao.getCustomersByPage((number * 5)-5));
  236. table.setModel(model);
  237. }
  238.  
  239.  
  240. }
  241.  
  242. String subSql = "SELECT * FROM Customer OFFSET %d ROWS FETCH NEXT 5 ROWS ONLY";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement