Advertisement
rootUser

[DB][3.1] MemoryInsert.java

Jan 23rd, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 18.95 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 java.text.ParseException;
  15. import java.text.SimpleDateFormat;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.swing.JOptionPane;
  19.  
  20. /**
  21.  *
  22.  * @author root
  23.  */
  24. public class MemoryInsert extends javax.swing.JFrame
  25. {
  26.  
  27.     /**
  28.      * Creates new form MemoryInsert
  29.      */
  30.     public MemoryInsert()
  31.     {
  32.         initComponents();
  33.         getContentPane().setBackground(Color.CYAN);
  34.     }
  35.  
  36.     /**
  37.      * This method is called from within the constructor to initialize the form.
  38.      * WARNING: Do NOT modify this code. The content of this method is always
  39.      * regenerated by the Form Editor.
  40.      */
  41.     @SuppressWarnings("unchecked")
  42.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  43.     private void initComponents()
  44.     {
  45.  
  46.         insertMemoryLabel = new javax.swing.JLabel();
  47.         nameLabel = new javax.swing.JLabel();
  48.         memoryNameTextField = new javax.swing.JTextField();
  49.         dateLabel = new javax.swing.JLabel();
  50.         memoryDateTextField = new javax.swing.JTextField();
  51.         timeLabel = new javax.swing.JLabel();
  52.         memoryTimeTextField = new javax.swing.JTextField();
  53.         placeLabel = new javax.swing.JLabel();
  54.         memoryPlaceTextField = new javax.swing.JTextField();
  55.         descriptionLabel = new javax.swing.JLabel();
  56.         memoryDescriptionScrollPane = new javax.swing.JScrollPane();
  57.         memoryDescriptionTextArea = new javax.swing.JTextArea();
  58.         exitButton = new javax.swing.JButton();
  59.         insertButton = new javax.swing.JButton();
  60.         backToMenuButton = new javax.swing.JButton();
  61.  
  62.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  63.  
  64.         insertMemoryLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 12)); // NOI18N
  65.         insertMemoryLabel.setForeground(new java.awt.Color(255, 0, 0));
  66.         insertMemoryLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  67.         insertMemoryLabel.setText("INSERT A MEMORY");
  68.  
  69.         nameLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  70.         nameLabel.setForeground(new java.awt.Color(0, 0, 255));
  71.         nameLabel.setText("NAME :");
  72.  
  73.         memoryNameTextField.setBackground(new java.awt.Color(0, 0, 0));
  74.         memoryNameTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
  75.         memoryNameTextField.setForeground(new java.awt.Color(0, 255, 0));
  76.  
  77.         dateLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  78.         dateLabel.setForeground(new java.awt.Color(0, 0, 255));
  79.         dateLabel.setText("DATE (YYYY-MM-DD) :");
  80.  
  81.         memoryDateTextField.setBackground(new java.awt.Color(0, 0, 0));
  82.         memoryDateTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
  83.         memoryDateTextField.setForeground(new java.awt.Color(0, 255, 0));
  84.         memoryDateTextField.addActionListener(new java.awt.event.ActionListener()
  85.         {
  86.             public void actionPerformed(java.awt.event.ActionEvent evt)
  87.             {
  88.                 memoryDateTextFieldActionPerformed(evt);
  89.             }
  90.         });
  91.  
  92.         timeLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  93.         timeLabel.setForeground(new java.awt.Color(0, 0, 255));
  94.         timeLabel.setText("TIME(HH:MM:SS) :");
  95.  
  96.         memoryTimeTextField.setBackground(new java.awt.Color(0, 0, 0));
  97.         memoryTimeTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
  98.         memoryTimeTextField.setForeground(new java.awt.Color(0, 255, 0));
  99.  
  100.         placeLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  101.         placeLabel.setForeground(new java.awt.Color(0, 0, 255));
  102.         placeLabel.setText("PLACE :");
  103.  
  104.         memoryPlaceTextField.setBackground(new java.awt.Color(0, 0, 0));
  105.         memoryPlaceTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
  106.         memoryPlaceTextField.setForeground(new java.awt.Color(0, 255, 0));
  107.  
  108.         descriptionLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  109.         descriptionLabel.setForeground(new java.awt.Color(0, 0, 255));
  110.         descriptionLabel.setText("DESCRIPTION :");
  111.  
  112.         memoryDescriptionTextArea.setBackground(new java.awt.Color(0, 0, 0));
  113.         memoryDescriptionTextArea.setColumns(20);
  114.         memoryDescriptionTextArea.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
  115.         memoryDescriptionTextArea.setForeground(new java.awt.Color(0, 255, 0));
  116.         memoryDescriptionTextArea.setRows(5);
  117.         memoryDescriptionScrollPane.setViewportView(memoryDescriptionTextArea);
  118.  
  119.         exitButton.setBackground(new java.awt.Color(0, 0, 0));
  120.         exitButton.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  121.         exitButton.setForeground(new java.awt.Color(0, 255, 0));
  122.         exitButton.setText("EXIT");
  123.         exitButton.addActionListener(new java.awt.event.ActionListener()
  124.         {
  125.             public void actionPerformed(java.awt.event.ActionEvent evt)
  126.             {
  127.                 exitButtonActionPerformed(evt);
  128.             }
  129.         });
  130.  
  131.         insertButton.setBackground(new java.awt.Color(0, 0, 0));
  132.         insertButton.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  133.         insertButton.setForeground(new java.awt.Color(0, 255, 0));
  134.         insertButton.setText("INSERT");
  135.         insertButton.addActionListener(new java.awt.event.ActionListener()
  136.         {
  137.             public void actionPerformed(java.awt.event.ActionEvent evt)
  138.             {
  139.                 insertButtonActionPerformed(evt);
  140.             }
  141.         });
  142.  
  143.         backToMenuButton.setBackground(new java.awt.Color(0, 0, 0));
  144.         backToMenuButton.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
  145.         backToMenuButton.setForeground(new java.awt.Color(0, 255, 0));
  146.         backToMenuButton.setText("BACK TO MENU");
  147.         backToMenuButton.addActionListener(new java.awt.event.ActionListener()
  148.         {
  149.             public void actionPerformed(java.awt.event.ActionEvent evt)
  150.             {
  151.                 backToMenuButtonActionPerformed(evt);
  152.             }
  153.         });
  154.  
  155.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  156.         getContentPane().setLayout(layout);
  157.         layout.setHorizontalGroup(
  158.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  159.             .addGroup(layout.createSequentialGroup()
  160.                       .addContainerGap()
  161.                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  162.                                 .addComponent(insertButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  163.                                 .addComponent(insertMemoryLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  164.                                 .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  165.                                 .addComponent(memoryNameTextField)
  166.                                 .addComponent(memoryPlaceTextField)
  167.                                 .addComponent(descriptionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  168.                                 .addComponent(memoryDescriptionScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
  169.                                 .addGroup(layout.createSequentialGroup()
  170.                                           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  171.                                                   .addComponent(memoryDateTextField)
  172.                                                   .addComponent(dateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  173.                                           .addGap(18, 18, 18)
  174.                                           .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  175.                                                   .addComponent(memoryTimeTextField)
  176.                                                   .addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)))
  177.                                 .addComponent(placeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  178.                                 .addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  179.                                 .addComponent(backToMenuButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  180.                       .addContainerGap())
  181.         );
  182.         layout.setVerticalGroup(
  183.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  184.             .addGroup(layout.createSequentialGroup()
  185.                       .addContainerGap()
  186.                       .addComponent(insertMemoryLabel)
  187.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  188.                       .addComponent(nameLabel)
  189.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  190.                       .addComponent(memoryNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  191.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  192.                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  193.                                 .addComponent(dateLabel)
  194.                                 .addComponent(timeLabel))
  195.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  196.                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  197.                                 .addComponent(memoryDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  198.                                 .addComponent(memoryTimeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  199.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  200.                       .addComponent(placeLabel)
  201.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  202.                       .addComponent(memoryPlaceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  203.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  204.                       .addComponent(descriptionLabel)
  205.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  206.                       .addComponent(memoryDescriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  207.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  208.                       .addComponent(insertButton)
  209.                       .addGap(18, 18, 18)
  210.                       .addComponent(backToMenuButton)
  211.                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  212.                       .addComponent(exitButton)
  213.                       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  214.         );
  215.  
  216.         pack();
  217.         setLocationRelativeTo(null);
  218.     }// </editor-fold>
  219.  
  220.     private void insertButtonActionPerformed(java.awt.event.ActionEvent evt)
  221.     {
  222.         // TODO add your handling code here:
  223.         String host = "jdbc:derby://localhost:1527/diary";
  224.         String username = "nbuser";
  225.         String password = "nbuser";
  226.         try
  227.         {
  228.             String memoryName = memoryNameTextField.getText();
  229.             String memoryDate = memoryDateTextField.getText();
  230.             String memoryTime = memoryTimeTextField.getText();
  231.             String memoryPlace = memoryPlaceTextField.getText();
  232.             String memoryDescription = memoryDescriptionTextArea.getText();
  233.             int maxID=0;
  234.  
  235.             //fetch result
  236.             //connect database and fetch result
  237.             Connection connection = DriverManager.getConnection(host, username, password);
  238.             Statement statement = connection.createStatement();
  239.             statement.executeUpdate
  240.             (
  241.                 " INSERT INTO NBUSER.memoryAll(memoryName) "+
  242.                 " VALUES('"+memoryName+"')"
  243.             );
  244.  
  245.             ResultSet resultSet = statement.executeQuery("SELECT memoryID FROM memoryAll");
  246.             while(resultSet.next())
  247.             {
  248.                 int variableMemoryID=resultSet.getInt("memoryID");
  249.                 maxID=variableMemoryID;
  250.             }
  251.  
  252.             statement.executeUpdate
  253.             (
  254.                 " INSERT INTO NBUSER.memoryPlace(memoryPlaceName,memoryID) "+
  255.                 " VALUES('"+memoryPlace+"',"+maxID+")"
  256.             );
  257.  
  258.             statement.executeUpdate
  259.             (
  260.                 " INSERT INTO NBUSER.memoryDescription(memoryDescriptionFull,memoryID) "+
  261.                 " VALUES('"+memoryDescription+"',"+maxID+")"
  262.             );
  263.  
  264.             //convert string to util date
  265.             SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
  266.             java.util.Date utilDate1 = null;
  267.             utilDate1 = formatter1.parse(memoryDate);
  268.             //covert util date to sql date
  269.             java.sql.Date sqlDate = new java.sql.Date(utilDate1.getTime());
  270.  
  271.             //convert string to util time
  272.             SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
  273.             java.util.Date utilDate2 = null;
  274.             utilDate2 = formatter2.parse(memoryTime);
  275.             //covert util date to sql date
  276.             java.sql.Time sqlTime = new java.sql.Time(utilDate2.getTime());
  277.  
  278.             statement.executeUpdate
  279.             (
  280.                 " INSERT INTO NBUSER.memoryMoment(memoryMomentDate,memoryMomentTime,memoryID) "+
  281.                 " VALUES(DATE('"+sqlDate+"'),TIME('"+sqlTime+"'),"+maxID+")"
  282.             );
  283.             //close all
  284.             resultSet.close();
  285.             statement.close();
  286.             connection.close();
  287.             this.setVisible(false);
  288.             MemoryIndex memoryIndex = new MemoryIndex();
  289.             memoryIndex.setVisible(true);
  290.         }
  291.         catch(SQLException sqlException)
  292.         {
  293.             sqlException.printStackTrace();
  294.             JOptionPane.showMessageDialog
  295.             (this,
  296.              "Error code : "+sqlException.getErrorCode()+"\n"+
  297.              "Error message : "+sqlException.getMessage()+"\n"+
  298.              "SQL state : "+sqlException.getSQLState()+"\n"
  299.             );
  300.         }
  301.         catch (ParseException parseException)
  302.         {
  303.             parseException.printStackTrace();
  304.             System.out.println();
  305.             System.out.println("Error message : "+parseException.getMessage());
  306.             System.out.println("Error offset : "+parseException.getErrorOffset());
  307.  
  308.         }
  309.         this.setVisible(false);
  310.         MemoryIndex memoryIndex = new MemoryIndex();
  311.         memoryIndex.setVisible(true);
  312.     }
  313.  
  314.     private void memoryDateTextFieldActionPerformed(java.awt.event.ActionEvent evt)
  315.     {
  316.         // TODO add your handling code here:
  317.     }
  318.  
  319.     private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
  320.     {
  321.         // TODO add your handling code here:
  322.         System.exit(0);
  323.     }
  324.  
  325.     private void backToMenuButtonActionPerformed(java.awt.event.ActionEvent evt)
  326.     {
  327.         // TODO add your handling code here:
  328.         this.setVisible(false);
  329.         Menu menu = new Menu();
  330.         menu.setVisible(true);
  331.     }
  332.  
  333.     /**
  334.      * @param args the command line arguments
  335.      */
  336.     public static void main(String args[])
  337.     {
  338.         /* Set the Nimbus look and feel */
  339.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  340.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  341.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  342.          */
  343.         try
  344.         {
  345.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
  346.             {
  347.                 if ("Nimbus".equals(info.getName()))
  348.                 {
  349.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  350.                     break;
  351.                 }
  352.             }
  353.         }
  354.         catch (ClassNotFoundException ex)
  355.         {
  356.             java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  357.         }
  358.         catch (InstantiationException ex)
  359.         {
  360.             java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  361.         }
  362.         catch (IllegalAccessException ex)
  363.         {
  364.             java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  365.         }
  366.         catch (javax.swing.UnsupportedLookAndFeelException ex)
  367.         {
  368.             java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  369.         }
  370.         //</editor-fold>
  371.  
  372.         /* Create and display the form */
  373.         java.awt.EventQueue.invokeLater(new Runnable()
  374.         {
  375.             public void run()
  376.             {
  377.                 new MemoryInsert().setVisible(true);
  378.             }
  379.         });
  380.     }
  381.  
  382.     // Variables declaration - do not modify
  383.     private javax.swing.JButton backToMenuButton;
  384.     private javax.swing.JLabel dateLabel;
  385.     private javax.swing.JLabel descriptionLabel;
  386.     private javax.swing.JButton exitButton;
  387.     private javax.swing.JButton insertButton;
  388.     private javax.swing.JLabel insertMemoryLabel;
  389.     private javax.swing.JTextField memoryDateTextField;
  390.     private javax.swing.JScrollPane memoryDescriptionScrollPane;
  391.     private javax.swing.JTextArea memoryDescriptionTextArea;
  392.     private javax.swing.JTextField memoryNameTextField;
  393.     private javax.swing.JTextField memoryPlaceTextField;
  394.     private javax.swing.JTextField memoryTimeTextField;
  395.     private javax.swing.JLabel nameLabel;
  396.     private javax.swing.JLabel placeLabel;
  397.     private javax.swing.JLabel timeLabel;
  398.     // End of variables declaration
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement