vladkomarr

Untitled

Dec 15th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.52 KB | None | 0 0
  1. package com.albums;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.event.TableModelEvent;
  5. import javax.swing.event.TableModelListener;
  6. import javax.swing.tree.ExpandVetoException;
  7. import java.awt.*;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.WindowAdapter;
  11. import java.awt.event.WindowEvent;
  12. import java.io.IOException;
  13. import java.sql.*;
  14.  
  15. /**
  16.  * Created by fakepotato on 10/19/15.
  17.  */
  18.  
  19. public class MainForm extends JFrame implements ActionListener,
  20.         TableModelListener {
  21.     /**
  22.      * Name of main table.
  23.      */
  24.     private static final String TABLE_NAME = "albums";
  25.  
  26.     /**
  27.      * For connection to db.
  28.      */
  29.     private static final String
  30.             URL = "jdbc:sqlite:identifier.sqlite",
  31.             URL_PG = "jdbc:postgresql://localhost:5432/lab4",
  32.             NAME = "postgres",
  33.             PASS = "root";
  34.  
  35.  
  36.     /**
  37.      * Default size of list.
  38.      */
  39.     private static final int DEFAULT_SIZE = 5;
  40.     /**
  41.      * Default year.
  42.      */
  43.     private static final int DEFAULT_YEAR = 1980;
  44.     /**
  45.      * list of albums.
  46.      */
  47.     private final AlbumList albumList;
  48.     /**
  49.      * button, button and 3 buttons.
  50.      */
  51.     private final JButton loadButton, saveButton,
  52.             exitButton, addButton, deleteButton;
  53.     /**
  54.      * Combo box.
  55.      */
  56.     private final JComboBox<String> comboBox;
  57.     /**
  58.      * Main table.
  59.      */
  60.     private final JTable table;
  61.     /**
  62.      * Constructor of form.
  63.      */
  64.  
  65.     public MainForm() {
  66.         super("Albums");
  67.         setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  68.         addWindowListener(new WindowAdapter() {
  69.             @Override
  70.             public void windowClosing(final WindowEvent e) {
  71.                 super.windowClosing(e);
  72.                 if (albumList.isDataSaved()) {
  73.                     Runtime.getRuntime().exit(0);
  74.                 } else {
  75.                     askSaving();
  76.                 }
  77.             }
  78.         });
  79.  
  80.         albumList = new AlbumList();
  81.         for (int i = 0; i < DEFAULT_SIZE; i++) {
  82.             albumList.add(new Album("Title" + i, "Band " + i,
  83.                     DEFAULT_YEAR + 1));
  84.         }
  85.  
  86.         albumList.addTableModelListener(this);
  87.         table = new JTable(albumList);
  88.         table.getColumnModel().getColumn(2).setCellEditor(
  89.                 new YearCellEditor(new JTextField()));
  90.         getContentPane().add(new JScrollPane(table));
  91.         JPanel panel = new JPanel();
  92.         panel.setLayout(new FlowLayout());
  93.         comboBox = new JComboBox<>(new String[]{"SQLite", "PostgreSQL"});
  94.         saveButton = new JButton("Save");
  95.         saveButton.addActionListener(this);
  96.         loadButton = new JButton("Load");
  97.         loadButton.addActionListener(this);
  98.         exitButton = new JButton("Exit");
  99.         exitButton.addActionListener(this);
  100.         addButton = new JButton("Add");
  101.         addButton.addActionListener(this);
  102.         deleteButton = new JButton("Remove");
  103.         deleteButton.addActionListener(this);
  104.         panel.add(comboBox);
  105.         panel.add(exitButton);
  106.         panel.add(addButton);
  107.         panel.add(deleteButton);
  108.         panel.add(loadButton);
  109.         panel.add(saveButton);
  110.         getContentPane().add(panel, BorderLayout.SOUTH);
  111.         pack();
  112.         setLocationRelativeTo(null);
  113.         setVisible(true);
  114.     }
  115.  
  116.     @Override
  117.     public final void actionPerformed(final ActionEvent e) {
  118.         if (e.getSource().equals(exitButton)) {
  119.             if (albumList.isDataSaved()) {
  120.                 Runtime.getRuntime().exit(0);
  121.             } else {
  122.                 askSaving();
  123.             }
  124.         } else if (e.getSource().equals(addButton)) {
  125.             albumList.add(new Album("Title", "Band", DEFAULT_YEAR));
  126.         } else if (e.getSource().equals(deleteButton)) {
  127.             if (table.getSelectedRowCount() == 1) {
  128.                 albumList.remove(albumList.get(table.getSelectedRow()));
  129.             }
  130.         } else {
  131.             if (e.getSource().equals(loadButton)) {
  132.                 if(comboBox.getSelectedIndex() == 0) {
  133.                     loadFromSQLite();
  134.                 } else {
  135.                     loadFromPostgres();
  136.                 }
  137.             } else if (e.getSource().equals(saveButton)) {
  138.                 if (comboBox.getSelectedIndex() == 0) {
  139.                     saveToSQLite();
  140.                 } else {
  141.                     saveToPostgres();
  142.                 }
  143.             }
  144.         }
  145.     }
  146.  
  147.     @Override
  148.     public final void tableChanged(final TableModelEvent e) {
  149.         saveButton.setEnabled(!albumList.isDataSaved());
  150.         deleteButton.setEnabled(albumList.count() > 0);
  151.     }
  152.  
  153.     /**
  154.      * Ask user for saving changes.
  155.      */
  156.     private void askSaving() {
  157.         switch (JOptionPane.showConfirmDialog(this,
  158.                 "Data has changed. Save it?",
  159.                 "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION)) {
  160.             case JOptionPane.YES_OPTION:
  161.                 saveButton.doClick();
  162.             case JOptionPane.NO_OPTION:
  163.                 Runtime.getRuntime().exit(0);
  164.                 break;
  165.             default:
  166.                 //
  167.         }
  168.     }
  169.  
  170.     /**
  171.      * Load data from SQLite.
  172.      */
  173.     private void loadFromSQLite() {
  174.         Connection connection = null;
  175.         Statement statement = null;
  176.         try {
  177.             Class.forName("org.sqlite.JDBC");
  178.             connection = DriverManager.getConnection(URL);
  179.             statement = connection.createStatement();
  180.             // Load from base
  181.             ResultSet resultSet = statement.executeQuery("select * from " + TABLE_NAME);
  182.  
  183.             //albumList.clear();
  184.             while (resultSet.next()) {
  185.                 albumList.add(new Album(
  186.                         resultSet.getString("Title"),
  187.                         resultSet.getString("Band"),
  188.                         resultSet.getInt("Year")));
  189.             }
  190.             table.updateUI();
  191.             JOptionPane.showMessageDialog(this, "Data loaded from SQLite");
  192.         } catch (Exception e) {
  193.             JOptionPane.showMessageDialog(this, e.getMessage());
  194.         }
  195.     }
  196.  
  197.     /**
  198.      * Save data to SQLite.
  199.      */
  200.     private void saveToSQLite() {
  201.         // create driver & connection
  202.         Connection connection = null;
  203.         // 2 statements for save & load
  204.         Statement statement = null;
  205.         PreparedStatement preStatement = null;
  206.         try {
  207.             Class.forName("org.sqlite.JDBC");
  208.             connection = DriverManager.getConnection(URL);
  209.             statement = connection.createStatement();
  210.             preStatement = connection.prepareStatement("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
  211.             preStatement.executeUpdate();
  212.  
  213.             String queryCreateTable = "CREATE TABLE " + TABLE_NAME +
  214.                     "(Title text," +
  215.                     "Band text," +
  216.                     "Year int);";
  217.  
  218.             preStatement = connection.prepareStatement(queryCreateTable);
  219.             preStatement.executeUpdate();
  220.  
  221.             String queryInsertDataFromAlbums =
  222.                     "INSERT INTO " + TABLE_NAME +
  223.                             " (Title, Band, Year) VALUES (?,?,?);";
  224.  
  225.             preStatement = connection.prepareStatement(queryInsertDataFromAlbums);
  226.  
  227.             for (Album a : albumList) {
  228.                 preStatement.setString(1, a.getTitle());
  229.                 preStatement.setString(2, a.getBand());
  230.                 preStatement.setInt(3, a.getReleaseYear());
  231.  
  232.                 preStatement.executeUpdate();
  233.             }
  234.             albumList.onStateChanged();
  235.             JOptionPane.showMessageDialog(this, "Data saved to SQLite db.");
  236.  
  237.         } catch (Exception e) {
  238.             JOptionPane.showMessageDialog(this, e.getMessage());
  239.         }
  240.     }
  241.  
  242.     /**
  243.      * Load data from PostgreSQL
  244.      */
  245.     private void loadFromPostgres(){
  246.         // create driver & connection
  247.         Connection connection = null;
  248.         // 2 statements for save & load
  249.         Statement statement = null;
  250.         try {
  251.             Class.forName("org.postgresql.Driver");
  252.             connection = DriverManager.getConnection(URL_PG, NAME, PASS);
  253.             statement = connection.createStatement();
  254.             // load from base
  255.             ResultSet resultSet = statement.executeQuery("select * from " + TABLE_NAME);
  256.  
  257.             //albumList.clear();
  258.             while (resultSet.next()) {
  259.                 albumList.add(new Album(
  260.                         resultSet.getString("Title"),
  261.                         resultSet.getString("Band"),
  262.                         resultSet.getInt("Year")));
  263.             }
  264.             table.updateUI();
  265.             JOptionPane.showMessageDialog(this, "Data loaded from PostgreSQL db.");
  266.         } catch (Exception e) {
  267.             JOptionPane.showMessageDialog(this, e.getMessage());
  268.         }
  269.     }
  270.     /**
  271.      * Save data to PostgreSQL
  272.      */
  273.     private void saveToPostgres() {
  274.         // create driver & connection
  275.         Connection connection = null;
  276.         // 2 statements for save & load
  277.         Statement statement = null;
  278.         PreparedStatement preStatement = null;
  279.         try {
  280.             Class.forName("org.postgresql.Driver");
  281.             connection = DriverManager.getConnection(URL_PG, NAME, PASS);
  282.             statement = connection.createStatement();
  283.             preStatement = connection.prepareStatement("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
  284.             preStatement.executeUpdate();
  285.  
  286.             String queryCreateTable = "CREATE TABLE " + TABLE_NAME +
  287.                     "(Title text," +
  288.                     "Band text," +
  289.                     "Year int);";
  290.  
  291.             preStatement = connection.prepareStatement(queryCreateTable);
  292.             preStatement.executeUpdate();
  293.  
  294.             String queryInsertDataFromAlbums =
  295.                     "INSERT INTO " + TABLE_NAME +
  296.                             " (Title, Band, Year) VALUES (?,?,?);";
  297.  
  298.             preStatement = connection.prepareStatement(queryInsertDataFromAlbums);
  299.  
  300.             for (Album a : albumList) {
  301.                 preStatement.setString(1, a.getTitle());
  302.                 preStatement.setString(2, a.getBand());
  303.                 preStatement.setInt(3, a.getReleaseYear());
  304.  
  305.                 preStatement.executeUpdate();
  306.             }
  307.             albumList.onStateChanged();
  308.             JOptionPane.showMessageDialog(this, "Data saved to PostgreSQL db.");
  309.         } catch (Exception e) {
  310.             JOptionPane.showMessageDialog(this, e.getMessage());
  311.         }
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment