Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.35 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.List;
  11.  
  12. public class City implements dbConnection, Comparable<City> {
  13.  
  14. private int ID;
  15. private String name;
  16. private Country country;
  17. private String district;
  18. private int population;
  19.  
  20. public City() {
  21. }
  22.  
  23. public City(int ID, String name, Country country, String district, int population) {
  24. this.ID = ID;
  25. this.name = name;
  26. this.country = country;
  27. this.district = district;
  28. this.population = population;
  29. }
  30.  
  31. public int getID() {
  32. return ID;
  33. }
  34.  
  35. public String getName() {
  36. return name;
  37. }
  38.  
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42.  
  43. public Country getCountry() {
  44. return country;
  45. }
  46.  
  47. public void setCountryCode(Country country) {
  48. this.country = country;
  49. }
  50.  
  51. public String getDistrict() {
  52. return district;
  53. }
  54.  
  55. public void setDistrict(String district) {
  56. this.district = district;
  57. }
  58.  
  59. public int getPopulation() {
  60. return population;
  61. }
  62.  
  63. public void setPopulation(int population) {
  64. this.population = population;
  65. }
  66.  
  67. @Override
  68. public int compareTo(City o) {
  69. return name.compareTo(o.name);
  70. }
  71.  
  72. public List<City> getCities(Country country) {
  73. List<City> cities = new ArrayList<>();
  74.  
  75. try (Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
  76. String selectStmt = "SELECT * FROM city "
  77. + "INNER JOIN country "
  78. + "ON city.CountryCode = country.Code "
  79. + "WHERE country.Code = ? ";
  80. PreparedStatement ps = con.prepareStatement(selectStmt);
  81. ps.setString(1, country.getCode());
  82. ResultSet rs = ps.executeQuery();
  83.  
  84. while (rs.next()) {
  85. ID = rs.getInt("ID");
  86. name = rs.getString("Name");
  87. district = rs.getString("District");
  88. population = rs.getInt("Population");
  89.  
  90. City city = new City(ID, name, country, district, population);
  91. cities.add(city);
  92. }
  93. } catch (SQLException ex) {
  94. System.out.println("SQLExeption - getCities()");
  95. }
  96. Collections.sort(cities);
  97. return cities;
  98. }
  99.  
  100. public void deleteCity(City city) {
  101. try (Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
  102. String deleteStmt = "DELETE FROM city "
  103. + "WHERE ID = ?";
  104. PreparedStatement ps = con.prepareStatement(deleteStmt);
  105. ps.setInt(1, city.ID);
  106. ps.executeUpdate();
  107. } catch (SQLException ex) {
  108. System.out.println("SQLExeption - deleteCity()");
  109. }
  110. }
  111.  
  112. }
  113.  
  114. package model;
  115.  
  116. import java.sql.Connection;
  117. import java.sql.DriverManager;
  118. import java.sql.ResultSet;
  119. import java.sql.SQLException;
  120. import java.sql.Statement;
  121. import java.util.ArrayList;
  122. import java.util.Collections;
  123. import java.util.List;
  124.  
  125. public class Country implements dbConnection, Comparable<Country> {
  126.  
  127. private String code;
  128. private String name;
  129. private String continent;
  130. private String region;
  131. private float surfaceArea;
  132. private Short indepYear;
  133. private int population;
  134. private Float lifeExpectancy;
  135. private Float GNP;
  136. private Float GNPOld;
  137. private String localName;
  138. private String governmentForm;
  139. private String headOfState;
  140. private Integer capital;
  141. private String code2;
  142.  
  143. public Country() {
  144. }
  145.  
  146. public Country(String code, String name) {
  147. this.code = code;
  148. this.name = name;
  149. }
  150.  
  151. public Country(String code, String name, String continent, String region,
  152. float surfaceArea, Short indepYear, int population,
  153. Float lifeExpectancy, Float GNP, Float GNPOld,
  154. String localName, String governmentForm,
  155. String headOfState, Integer capital, String code2) {
  156. this.code = code;
  157. this.name = name;
  158. this.continent = continent;
  159. this.region = region;
  160. this.surfaceArea = surfaceArea;
  161. this.indepYear = indepYear;
  162. this.population = population;
  163. this.lifeExpectancy = lifeExpectancy;
  164. this.GNP = GNP;
  165. this.GNPOld = GNPOld;
  166. this.localName = localName;
  167. this.governmentForm = governmentForm;
  168. this.headOfState = headOfState;
  169. this.capital = capital;
  170. this.code2 = code2;
  171. }
  172.  
  173. public String getCode() {
  174. return code;
  175. }
  176.  
  177. public void setCode(String code) {
  178. this.code = code;
  179. }
  180.  
  181. public String getName() {
  182. return name;
  183. }
  184.  
  185. public void setName(String name) {
  186. this.name = name;
  187. }
  188.  
  189. public String getContinent() {
  190. return continent;
  191. }
  192.  
  193. public void setContinent(String continent) {
  194. this.continent = continent;
  195. }
  196.  
  197. public String getRegion() {
  198. return region;
  199. }
  200.  
  201. public void setRegion(String region) {
  202. this.region = region;
  203. }
  204.  
  205. public float getSurfaceArea() {
  206. return surfaceArea;
  207. }
  208.  
  209. public void setSurfaceArea(float surfaceArea) {
  210. this.surfaceArea = surfaceArea;
  211. }
  212.  
  213. public Short getIndepYear() {
  214. return indepYear;
  215. }
  216.  
  217. public void setIndepYear(Short indepYear) {
  218. this.indepYear = indepYear;
  219. }
  220.  
  221. public int getPopulation() {
  222. return population;
  223. }
  224.  
  225. public void setPopulation(int population) {
  226. this.population = population;
  227. }
  228.  
  229. public Float getLifeExpectancy() {
  230. return lifeExpectancy;
  231. }
  232.  
  233. public void setLifeExpectancy(Float lifeExpectancy) {
  234. this.lifeExpectancy = lifeExpectancy;
  235. }
  236.  
  237. public Float getGNP() {
  238. return GNP;
  239. }
  240.  
  241. public void setGNP(Float GNP) {
  242. this.GNP = GNP;
  243. }
  244.  
  245. public Float getGNPOld() {
  246. return GNPOld;
  247. }
  248.  
  249. public void setGNPOld(Float GNPOld) {
  250. this.GNPOld = GNPOld;
  251. }
  252.  
  253. public String getLocalName() {
  254. return localName;
  255. }
  256.  
  257. public void setLocalName(String localName) {
  258. this.localName = localName;
  259. }
  260.  
  261. public String getGovernmentForm() {
  262. return governmentForm;
  263. }
  264.  
  265. public void setGovernmentForm(String governmentForm) {
  266. this.governmentForm = governmentForm;
  267. }
  268.  
  269. public String getHeadOfState() {
  270. return headOfState;
  271. }
  272.  
  273. public void setHeadOfState(String headOfState) {
  274. this.headOfState = headOfState;
  275. }
  276.  
  277. public Integer getCapital() {
  278. return capital;
  279. }
  280.  
  281. public void setCapital(Integer capital) {
  282. this.capital = capital;
  283. }
  284.  
  285. public String getCode2() {
  286. return code2;
  287. }
  288.  
  289. public void setCode2(String code2) {
  290. this.code2 = code2;
  291. }
  292.  
  293. @Override
  294. public int compareTo(Country o) {
  295. return name.compareTo(o.getName());
  296. }
  297.  
  298. public List<Country> getCountries() {
  299. List<Country> countries = new ArrayList<>();
  300.  
  301. try (Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
  302. Statement stmt = con.createStatement();
  303. String query = "SELECT Code, Name FROM country";
  304. ResultSet rs = stmt.executeQuery(query);
  305.  
  306. while (rs.next()) {
  307. code = rs.getString("Code");
  308. name = rs.getString("Name");
  309. Country country = new Country(code, name);
  310. countries.add(country);
  311. }
  312. } catch (SQLException ex) {
  313. System.out.println("SQLException - getCountries()");
  314. }
  315. Collections.sort(countries);
  316. return countries;
  317. }
  318.  
  319. @Override
  320. public String toString() {
  321. return getName();
  322. }
  323.  
  324. }
  325.  
  326. package model;
  327.  
  328. public interface dbConnection {
  329. String URL = "jdbc:mysql://localhost:3306/world";
  330. String USERNAME = "root";
  331. String PASSWORD = "1234";
  332. }
  333.  
  334. package view;
  335.  
  336. import java.awt.Graphics;
  337. import java.awt.Image;
  338. import java.awt.event.ActionListener;
  339. import java.net.URL;
  340. import javax.swing.ImageIcon;
  341. import javax.swing.JFrame;
  342. import javax.swing.JMenu;
  343. import javax.swing.JMenuBar;
  344. import javax.swing.JMenuItem;
  345. import javax.swing.JOptionPane;
  346. import javax.swing.JPanel;
  347.  
  348. public class MainWindow {
  349.  
  350. private JFrame frame;
  351. private JMenuBar menuBar;
  352. private JMenu file;
  353. private JMenu data;
  354. private JMenuItem exitMenuItem;
  355. private JMenuItem citiesMenuItem;
  356.  
  357. public MainWindow() {
  358. initComponents();
  359. }
  360.  
  361. private void initComponents() {
  362. frame = new JFrame("Swing application with JDBC using World sample database");
  363. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  364. frame.setSize(625, 650);
  365. frame.setResizable(false);
  366. frame.setJMenuBar(createMenuBar());
  367.  
  368. try {
  369. frame.getContentPane().add(new MyBackgroundPanel());
  370. } catch (NullPointerException ex) {
  371. JOptionPane.showMessageDialog(null, "Background image is missing!", "Error", JOptionPane.ERROR_MESSAGE);
  372. }
  373.  
  374. frame.setVisible(true);
  375. frame.setLocationRelativeTo(null);
  376. }
  377.  
  378. private JMenuBar createMenuBar() {
  379. menuBar = new JMenuBar();
  380. file = new JMenu("File");
  381. exitMenuItem = new JMenuItem("Exit");
  382. file.add(exitMenuItem);
  383.  
  384. data = new JMenu("Data");
  385. citiesMenuItem = new JMenuItem("Load cities");
  386. data.add(citiesMenuItem);
  387.  
  388. menuBar.add(file);
  389. menuBar.add(data);
  390. return menuBar;
  391. }
  392.  
  393. public JMenuItem getExitMenu() {
  394. return exitMenuItem;
  395. }
  396.  
  397. public JMenuItem getLoadCitiesMenu() {
  398. return citiesMenuItem;
  399. }
  400.  
  401. public void addMenuActionListener (ActionListener menuActionListener) {
  402. exitMenuItem.addActionListener(menuActionListener);
  403. citiesMenuItem.addActionListener(menuActionListener);
  404. }
  405.  
  406. private class MyBackgroundPanel extends JPanel {
  407. URL url = getClass().getResource("worldMap03.jpg");
  408. Image img = new ImageIcon(url).getImage();
  409.  
  410. @Override
  411. protected void paintComponent(Graphics g) {
  412. g.drawImage(img, 0, 0, null);
  413. }
  414. }
  415. }
  416.  
  417. package view;
  418.  
  419. import java.awt.Dimension;
  420. import java.awt.FlowLayout;
  421. import java.awt.event.ActionListener;
  422. import java.awt.event.MouseListener;
  423. import java.util.Vector;
  424. import javax.swing.Box;
  425. import javax.swing.DefaultListSelectionModel;
  426. import javax.swing.JButton;
  427. import javax.swing.JDialog;
  428. import javax.swing.JList;
  429. import javax.swing.JPanel;
  430. import javax.swing.JScrollPane;
  431. import javax.swing.JTable;
  432. import javax.swing.SwingConstants;
  433. import javax.swing.border.Border;
  434. import javax.swing.table.DefaultTableCellRenderer;
  435. import javax.swing.table.DefaultTableModel;
  436.  
  437. public class CitiesDialog extends JDialog {
  438.  
  439. private JPanel listPanel;
  440. private JPanel tablePanel;
  441. private JList list;
  442. private DefaultTableModel model;
  443. private JTable table;
  444. private JScrollPane tScrollPane;
  445. private DefaultTableCellRenderer rightRenderer;
  446. private JButton deleteButton;
  447. private Border listBorder;
  448. private final Object[] columnNames = {"City name", "District", "Population"};
  449.  
  450. public CitiesDialog() {
  451. setTitle("Country dialog");
  452. setSize(800, 600);
  453. setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
  454. setLocationRelativeTo(null);
  455. add(createListPanel());
  456. add(createTablePanel());
  457. //setModal(true);
  458. setResizable(false);
  459. setVisible(true);
  460. }
  461.  
  462. private JPanel createListPanel() {
  463. listPanel = new JPanel();
  464.  
  465. //JList for countries
  466. list = new JList();
  467. JScrollPane listScrollPane = new JScrollPane(list);
  468. listScrollPane.setPreferredSize(new Dimension(200, 560));
  469.  
  470. listPanel.add(listScrollPane);
  471. return listPanel;
  472. }
  473.  
  474. private JPanel createTablePanel() {
  475. tablePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 10));
  476. tablePanel.setPreferredSize(new Dimension(575, 560));
  477.  
  478. // Delete Button
  479. deleteButton = new JButton("Delete");
  480. deleteButton.setFocusable(false);
  481.  
  482. // JTable with DefaultTableModel
  483. model = new DefaultTableModel(columnNames, 0) {
  484. @Override
  485. public boolean isCellEditable(int row, int column) {
  486. return false;
  487. }
  488. };
  489.  
  490. table = new JTable(model);
  491. rightRenderer = new DefaultTableCellRenderer();
  492. rightRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
  493. table.getColumnModel().getColumn(2).setCellRenderer(rightRenderer);
  494. table.setPreferredScrollableViewportSize(new Dimension(570, 491));
  495. table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
  496. tScrollPane = new JScrollPane(table);
  497.  
  498. tablePanel.add(deleteButton);
  499. tablePanel.add(Box.createRigidArea(new Dimension(15, 20)));
  500. tablePanel.add(tScrollPane);
  501. return tablePanel;
  502. }
  503.  
  504. public int getSelectedIndex() {
  505. return list.getSelectedIndex();
  506. }
  507.  
  508. public int getSelectedRow() {
  509. return table.getSelectedRow();
  510. }
  511.  
  512. public void loadCountryNames(Vector listData) {
  513. list.setListData(listData);
  514. }
  515.  
  516. public void fillTableData(Vector cityData) {
  517. model.addRow(cityData);
  518. }
  519.  
  520. public void clearTable() {
  521. model.getDataVector().removeAllElements();
  522. }
  523.  
  524. public void removeRow(int row) {
  525. model.removeRow(row);
  526. }
  527.  
  528. @Override
  529. public void addMouseListener(MouseListener mouseListener) {
  530. list.addMouseListener(mouseListener);
  531. }
  532.  
  533. public void addDeleteButtonListener(ActionListener actionlistener) {
  534. deleteButton.addActionListener(actionlistener);
  535. }
  536.  
  537. }
  538.  
  539. package controller;
  540.  
  541. import java.awt.EventQueue;
  542. import view.CitiesDialog;
  543. import model.Country;
  544. import model.City;
  545. import view.MainWindow;
  546. import java.awt.event.ActionEvent;
  547. import java.awt.event.ActionListener;
  548. import java.awt.event.MouseEvent;
  549. import java.awt.event.MouseListener;
  550. import java.util.List;
  551. import java.util.Vector;
  552.  
  553. public class Controller {
  554.  
  555. private City city;
  556. private Country country;
  557. private MainWindow mainWindow;
  558. private CitiesDialog citiesDialog;
  559. private Country selectedCountry;
  560. List<Country> countries;
  561. List<City> cities;
  562.  
  563. public Controller() {
  564. }
  565.  
  566. public Controller(City city, Country country, MainWindow mainWindow) {
  567. this.city = city;
  568. this.country = country;
  569. this.mainWindow = mainWindow;
  570. selectedCountry = null;
  571. mainWindow.addMenuActionListener(new MenuActionListener());
  572. }
  573.  
  574. private class MenuActionListener implements ActionListener {
  575.  
  576. @Override
  577. public void actionPerformed(ActionEvent e) {
  578. if (e.getSource() == mainWindow.getExitMenu()) {
  579. System.exit(0);
  580. } else if (e.getSource() == mainWindow.getLoadCitiesMenu()) {
  581. EventQueue.invokeLater(new Runnable() {
  582. @Override
  583. public void run() {
  584. citiesDialog = new CitiesDialog();
  585. citiesDialog.addMouseListener(new ListMouseListener());
  586. citiesDialog.addDeleteButtonListener(new DeleteButtonActionListener());
  587.  
  588. countries = country.getCountries();
  589. Vector vector = new Vector();
  590.  
  591. for (Country ct : countries) {
  592. vector.add(ct.getName());
  593. }
  594. citiesDialog.loadCountryNames(vector);
  595. }
  596. });
  597.  
  598. }
  599. }
  600. }
  601.  
  602. private class DeleteButtonActionListener implements ActionListener {
  603.  
  604. @Override
  605. public void actionPerformed(ActionEvent e) {
  606. int selectedRow = citiesDialog.getSelectedRow();
  607.  
  608. if ((selectedCountry != null) && (selectedRow >= 0)) {
  609. city.deleteCity(cities.get(selectedRow));
  610. citiesDialog.removeRow(selectedRow);
  611. }
  612. }
  613. }
  614.  
  615. private class ListMouseListener implements MouseListener {
  616.  
  617. @Override
  618. public void mouseClicked(MouseEvent e) {
  619. citiesDialog.clearTable();
  620. selectedCountry = countries.get(citiesDialog.getSelectedIndex());
  621. cities = city.getCities(selectedCountry);
  622.  
  623. for (City c : cities) {
  624. Vector row = new Vector();
  625. row.addElement(c.getName());
  626. row.addElement(c.getDistrict());
  627. row.addElement(String.format("%, d", c.getPopulation()));
  628. citiesDialog.fillTableData(row);
  629. }
  630. }
  631.  
  632. @Override
  633. public void mousePressed(MouseEvent e) {
  634. }
  635.  
  636. @Override
  637. public void mouseReleased(MouseEvent e) {
  638. }
  639.  
  640. @Override
  641. public void mouseEntered(MouseEvent e) {
  642. }
  643.  
  644. @Override
  645. public void mouseExited(MouseEvent e) {
  646. }
  647. }
  648. }
  649.  
  650. package controller;
  651.  
  652. import model.Country;
  653. import model.City;
  654. import view.MainWindow;
  655.  
  656. public class SwingWorldJDBC {
  657.  
  658. public static void main(String[] args) {
  659. City city = new City();
  660. Country country = new Country();
  661. MainWindow mw = new MainWindow();
  662. Controller controller = new Controller(city, country, mw);
  663. }
  664. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement