Advertisement
rootUser

[DB][3.1] MemoryIndex.java

Jan 23rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 14.61 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package diary;
  7.  
  8. import java.awt.Color;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.table.DefaultTableModel;
  16.  
  17. /**
  18.  *
  19.  * @author root
  20.  */
  21. public class MemoryIndex extends javax.swing.JFrame
  22. {
  23.  
  24.     /**
  25.      * Creates new form MemoryIndex
  26.      */
  27.     public MemoryIndex()
  28.     {
  29.         initComponents();
  30.         getContentPane().setBackground(Color.black);
  31.         fetchResultForTable();
  32.     }
  33.  
  34.     public void fetchResultForTable()
  35.     {
  36.         String host = "jdbc:derby://localhost:1527/diary";
  37.         String username = "nbuser";
  38.         String password = "nbuser";
  39.         try
  40.         {
  41.             //connect database and fetch result
  42.             Connection connection = DriverManager.getConnection(host, username, password);
  43.             Statement statement = connection.createStatement();
  44.             ResultSet resultSet = statement.executeQuery("SELECT * FROM memoryAll");
  45.             //add row to table - according to result
  46.             DefaultTableModel defaultTableModel = (DefaultTableModel) resultTable.getModel();
  47.             while(resultSet.next())
  48.             {
  49.                 int memoryID = resultSet.getInt("memoryID");
  50.                 String memoryName = resultSet.getString("memoryName");
  51.                 defaultTableModel.addRow(new Object[] {memoryID,memoryName});
  52.             }
  53.             //close all
  54.             resultSet.close();
  55.             statement.close();
  56.             connection.close();
  57.         }
  58.         catch(SQLException sqlException)
  59.         {
  60.             JOptionPane.showMessageDialog
  61.             (this,
  62.              "Error code : "+sqlException.getErrorCode()+"\n"+
  63.              "Error message : "+sqlException.getMessage()+"\n"+
  64.              "SQL state : "+sqlException.getSQLState()+"\n"
  65.             );
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * This method is called from within the constructor to initialize the form.
  71.      * WARNING: Do NOT modify this code. The content of this method is always
  72.      * regenerated by the Form Editor.
  73.      */
  74.     @SuppressWarnings("unchecked")
  75.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  76.     private void initComponents()
  77.     {
  78.  
  79.         memoryLabel = new javax.swing.JLabel();
  80.         resultTableScrollPane = new javax.swing.JScrollPane();
  81.         resultTable = new javax.swing.JTable();
  82.         backToMenuButton = new javax.swing.JButton();
  83.         exitButton = new javax.swing.JButton();
  84.         memoryDetailsLabel = new javax.swing.JLabel();
  85.         memoryIDLabel = new javax.swing.JLabel();
  86.         memoryIdInputTextField = new javax.swing.JTextField();
  87.         goButton = new javax.swing.JButton();
  88.         jLabel1 = new javax.swing.JLabel();
  89.         jLabel2 = new javax.swing.JLabel();
  90.         jLabel3 = new javax.swing.JLabel();
  91.  
  92.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  93.  
  94.         memoryLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 12)); // NOI18N
  95.         memoryLabel.setForeground(new java.awt.Color(255, 51, 153));
  96.         memoryLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  97.         memoryLabel.setText("YOUR MEMORIES HERE");
  98.  
  99.         resultTableScrollPane.setFont(new java.awt.Font("Kristen ITC", 1, 12)); // NOI18N
  100.  
  101.         resultTable.setFont(new java.awt.Font("Kristen ITC", 1, 12)); // NOI18N
  102.         resultTable.setModel(new javax.swing.table.DefaultTableModel(
  103.                                  new Object [][]
  104.         {
  105.  
  106.         },
  107.         new String []
  108.         {
  109.             "memoryID", "memoryName"
  110.         }
  111.                              ));
  112.         resultTableScrollPane.setViewportView(resultTable);
  113.  
  114.         backToMenuButton.setBackground(new java.awt.Color(0, 0, 0));
  115.         backToMenuButton.setFont(new java.awt.Font("Kristen ITC", 1, 12)); // NOI18N
  116.         backToMenuButton.setForeground(new java.awt.Color(0, 255, 0));
  117.         backToMenuButton.setText("BACK TO MENU");
  118.         backToMenuButton.addActionListener(new java.awt.event.ActionListener()
  119.         {
  120.             public void actionPerformed(java.awt.event.ActionEvent evt)
  121.             {
  122.                 backToMenuButtonActionPerformed(evt);
  123.             }
  124.         });
  125.  
  126.         exitButton.setBackground(new java.awt.Color(0, 0, 0));
  127.         exitButton.setFont(new java.awt.Font("Kristen ITC", 1, 12)); // NOI18N
  128.         exitButton.setForeground(new java.awt.Color(0, 255, 0));
  129.         exitButton.setText("EXIT");
  130.         exitButton.addActionListener(new java.awt.event.ActionListener()
  131.         {
  132.             public void actionPerformed(java.awt.event.ActionEvent evt)
  133.             {
  134.                 exitButtonActionPerformed(evt);
  135.             }
  136.         });
  137.  
  138.         memoryDetailsLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  139.         memoryDetailsLabel.setForeground(new java.awt.Color(0, 255, 0));
  140.         memoryDetailsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  141.         memoryDetailsLabel.setText("TO INSERT NEW MEMORIES,");
  142.  
  143.         memoryIDLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  144.         memoryIDLabel.setForeground(new java.awt.Color(255, 255, 0));
  145.         memoryIDLabel.setText("MEMORY ID : ");
  146.  
  147.         memoryIdInputTextField.setBackground(new java.awt.Color(0, 0, 0));
  148.         memoryIdInputTextField.setFont(new java.awt.Font("Kristen ITC", 1, 12)); // NOI18N
  149.         memoryIdInputTextField.setForeground(new java.awt.Color(0, 255, 0));
  150.         memoryIdInputTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  151.  
  152.         goButton.setBackground(new java.awt.Color(0, 0, 0));
  153.         goButton.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  154.         goButton.setForeground(new java.awt.Color(0, 255, 0));
  155.         goButton.setText("GO");
  156.         goButton.addActionListener(new java.awt.event.ActionListener()
  157.         {
  158.             public void actionPerformed(java.awt.event.ActionEvent evt)
  159.             {
  160.                 goButtonActionPerformed(evt);
  161.             }
  162.         });
  163.  
  164.         jLabel1.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  165.         jLabel1.setForeground(new java.awt.Color(0, 255, 0));
  166.         jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  167.         jLabel1.setText("UPDATE OR DELETE OLD MEMORIES,");
  168.  
  169.         jLabel2.setFont(new java.awt.Font("Perpetua Titling MT", 1, 12)); // NOI18N
  170.         jLabel2.setForeground(new java.awt.Color(0, 255, 255));
  171.         jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  172.         jLabel2.setText("FIND YOUR 'ID'  NUMBER FROM ABOVE TABLE");
  173.  
  174.         jLabel3.setFont(new java.awt.Font("Perpetua Titling MT", 1, 12)); // NOI18N
  175.         jLabel3.setForeground(new java.awt.Color(0, 255, 255));
  176.         jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  177.         jLabel3.setText("THEN INSERT BELOW AND PRESS 'GO'  TO CONTINUE");
  178.  
  179.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  180.         getContentPane().setLayout(layout);
  181.         layout.setHorizontalGroup(
  182.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  183.             .addGroup(layout.createSequentialGroup()
  184.                       .addContainerGap()
  185.                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  186.                                 .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  187.                                 .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  188.                                 .addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  189.                                 .addComponent(backToMenuButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  190.                                 .addComponent(memoryDetailsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  191.                                 .addComponent(resultTableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
  192.                                 .addComponent(memoryLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  193.                                 .addGroup(layout.createSequentialGroup()
  194.                                           .addGap(12, 12, 12)
  195.                                           .addComponent(memoryIDLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
  196.                                           .addGap(18, 18, 18)
  197.                                           .addComponent(memoryIdInputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
  198.                                           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  199.                                           .addComponent(goButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  200.                                 .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  201.                       .addContainerGap())
  202.         );
  203.         layout.setVerticalGroup(
  204.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  205.             .addGroup(layout.createSequentialGroup()
  206.                       .addContainerGap()
  207.                       .addComponent(memoryLabel)
  208.                       .addGap(18, 18, 18)
  209.                       .addComponent(resultTableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
  210.                       .addGap(18, 18, 18)
  211.                       .addComponent(memoryDetailsLabel)
  212.                       .addGap(18, 18, 18)
  213.                       .addComponent(jLabel1)
  214.                       .addGap(18, 18, 18)
  215.                       .addComponent(jLabel2)
  216.                       .addGap(18, 18, 18)
  217.                       .addComponent(jLabel3)
  218.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  219.                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  220.                                 .addComponent(memoryIdInputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  221.                                 .addComponent(goButton)
  222.                                 .addComponent(memoryIDLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
  223.                       .addGap(18, 18, 18)
  224.                       .addComponent(backToMenuButton)
  225.                       .addGap(18, 18, 18)
  226.                       .addComponent(exitButton)
  227.                       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  228.         );
  229.  
  230.         pack();
  231.         setLocationRelativeTo(null);
  232.     }// </editor-fold>
  233.  
  234.     private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
  235.     {
  236.         // TODO add your handling code here:
  237.         System.exit(0);
  238.     }
  239.  
  240.     private void goButtonActionPerformed(java.awt.event.ActionEvent evt)
  241.     {
  242.         // TODO add your handling code here:
  243.         int memoryID = Integer.parseInt(memoryIdInputTextField.getText());
  244.         this.setVisible(false);
  245.         MemoryDetails memoryDetails = new MemoryDetails(memoryID);
  246.         memoryDetails.setVisible(true);
  247.     }
  248.  
  249.     private void backToMenuButtonActionPerformed(java.awt.event.ActionEvent evt)
  250.     {
  251.         // TODO add your handling code here:
  252.         this.setVisible(false);
  253.         Menu menu = new Menu();
  254.         menu.setVisible(true);
  255.     }
  256.  
  257.     /**
  258.      * @param args the command line arguments
  259.      */
  260.     public static void main(String args[])
  261.     {
  262.         /* Set the Nimbus look and feel */
  263.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  264.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  265.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  266.          */
  267.         try
  268.         {
  269.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
  270.             {
  271.                 if ("Nimbus".equals(info.getName()))
  272.                 {
  273.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  274.                     break;
  275.                 }
  276.             }
  277.         }
  278.         catch (ClassNotFoundException ex)
  279.         {
  280.             java.util.logging.Logger.getLogger(MemoryIndex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  281.         }
  282.         catch (InstantiationException ex)
  283.         {
  284.             java.util.logging.Logger.getLogger(MemoryIndex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  285.         }
  286.         catch (IllegalAccessException ex)
  287.         {
  288.             java.util.logging.Logger.getLogger(MemoryIndex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  289.         }
  290.         catch (javax.swing.UnsupportedLookAndFeelException ex)
  291.         {
  292.             java.util.logging.Logger.getLogger(MemoryIndex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  293.         }
  294.         //</editor-fold>
  295.  
  296.         /* Create and display the form */
  297.         java.awt.EventQueue.invokeLater(new Runnable()
  298.         {
  299.             public void run()
  300.             {
  301.                 new MemoryIndex().setVisible(true);
  302.             }
  303.         });
  304.     }
  305.  
  306.     // Variables declaration - do not modify
  307.     private javax.swing.JButton backToMenuButton;
  308.     private javax.swing.JButton exitButton;
  309.     private javax.swing.JButton goButton;
  310.     private javax.swing.JLabel jLabel1;
  311.     private javax.swing.JLabel jLabel2;
  312.     private javax.swing.JLabel jLabel3;
  313.     private javax.swing.JLabel memoryDetailsLabel;
  314.     private javax.swing.JLabel memoryIDLabel;
  315.     private javax.swing.JTextField memoryIdInputTextField;
  316.     private javax.swing.JLabel memoryLabel;
  317.     private javax.swing.JTable resultTable;
  318.     private javax.swing.JScrollPane resultTableScrollPane;
  319.     // End of variables declaration
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement