Advertisement
rootUser

[DB][3.1] MemoryInsert.java (SSMS)

Jan 28th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 19.35 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 driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
  224.         String host = "jdbc:sqlserver://localhost:61271";
  225.         String username = "sa";
  226.         String password = "alvi";
  227.         try
  228.         {
  229.             String memoryName = memoryNameTextField.getText();
  230.             String memoryDate = memoryDateTextField.getText();
  231.             String memoryTime = memoryTimeTextField.getText();
  232.             String memoryPlace = memoryPlaceTextField.getText();
  233.             String memoryDescription = memoryDescriptionTextArea.getText();
  234.             int maxID=0;
  235.  
  236.             //fetch result
  237.             //connect database and fetch result
  238.             Class.forName(driver);
  239.             Connection connection = DriverManager.getConnection(host, username, password);
  240.             Statement statement = connection.createStatement();
  241.             statement.executeUpdate
  242.             (
  243.                 " INSERT INTO diary.dbo.memoryAll(memoryName) "+
  244.                 " VALUES('"+memoryName+"')"
  245.             );
  246.  
  247.             ResultSet resultSet = statement.executeQuery("SELECT memoryID FROM diary.dbo.memoryAll");
  248.             while(resultSet.next())
  249.             {
  250.                 int variableMemoryID=resultSet.getInt("memoryID");
  251.                 maxID=variableMemoryID;
  252.             }
  253.  
  254.             statement.executeUpdate
  255.             (
  256.                 " INSERT INTO diary.dbo.memoryPlace(memoryPlaceName,memoryID) "+
  257.                 " VALUES('"+memoryPlace+"',"+maxID+")"
  258.             );
  259.  
  260.             statement.executeUpdate
  261.             (
  262.                 " INSERT INTO diary.dbo.memoryDescription(memoryDescriptionFull,memoryID) "+
  263.                 " VALUES('"+memoryDescription+"',"+maxID+")"
  264.             );
  265.  
  266.             //convert string to util date
  267.             SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
  268.             java.util.Date utilDate1 = null;
  269.             utilDate1 = formatter1.parse(memoryDate);
  270.             //covert util date to sql date
  271.             java.sql.Date sqlDate = new java.sql.Date(utilDate1.getTime());
  272.  
  273.             //convert string to util time
  274.             SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
  275.             java.util.Date utilDate2 = null;
  276.             utilDate2 = formatter2.parse(memoryTime);
  277.             //covert util date to sql date
  278.             java.sql.Time sqlTime = new java.sql.Time(utilDate2.getTime());
  279.  
  280.             statement.executeUpdate
  281.             (
  282.                 " INSERT INTO diary.dbo.memoryMoment(memoryMomentDate,memoryMomentTime,memoryID) "+
  283.                 " VALUES('"+sqlDate+"','"+sqlTime+"',"+maxID+")"
  284.             );
  285.             //close all
  286.             resultSet.close();
  287.             statement.close();
  288.             connection.close();
  289.             this.setVisible(false);
  290.             MemoryIndex memoryIndex = new MemoryIndex();
  291.             memoryIndex.setVisible(true);
  292.         }
  293.         catch(SQLException sqlException)
  294.         {
  295.             sqlException.printStackTrace();
  296.             JOptionPane.showMessageDialog
  297.             (this,
  298.              "Error code : "+sqlException.getErrorCode()+"\n"+
  299.              "Error message : "+sqlException.getMessage()+"\n"+
  300.              "SQL state : "+sqlException.getSQLState()+"\n"
  301.             );
  302.         }
  303.         catch (ParseException parseException)
  304.         {
  305.             parseException.printStackTrace();
  306.             System.out.println();
  307.             System.out.println("Error message : "+parseException.getMessage());
  308.             System.out.println("Error offset : "+parseException.getErrorOffset());
  309.  
  310.         }
  311.         catch (ClassNotFoundException classNotFoundException)
  312.         {
  313.             classNotFoundException.printStackTrace();
  314.             JOptionPane.showMessageDialog
  315.             (this,
  316.              "Error message : "+classNotFoundException.getMessage()+"\n"
  317.             );
  318.         }
  319.         this.setVisible(false);
  320.         MemoryIndex memoryIndex = new MemoryIndex();
  321.         memoryIndex.setVisible(true);
  322.     }
  323.  
  324.     private void memoryDateTextFieldActionPerformed(java.awt.event.ActionEvent evt)
  325.     {
  326.         // TODO add your handling code here:
  327.     }
  328.  
  329.     private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
  330.     {
  331.         // TODO add your handling code here:
  332.         System.exit(0);
  333.     }
  334.  
  335.     private void backToMenuButtonActionPerformed(java.awt.event.ActionEvent evt)
  336.     {
  337.         // TODO add your handling code here:
  338.         this.setVisible(false);
  339.         Menu menu = new Menu();
  340.         menu.setVisible(true);
  341.     }
  342.  
  343.     /**
  344.      * @param args the command line arguments
  345.      */
  346.     public static void main(String args[])
  347.     {
  348.         /* Set the Nimbus look and feel */
  349.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  350.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  351.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  352.          */
  353.         try
  354.         {
  355.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
  356.             {
  357.                 if ("Nimbus".equals(info.getName()))
  358.                 {
  359.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  360.                     break;
  361.                 }
  362.             }
  363.         }
  364.         catch (ClassNotFoundException ex)
  365.         {
  366.             java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  367.         }
  368.         catch (InstantiationException ex)
  369.         {
  370.             java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  371.         }
  372.         catch (IllegalAccessException ex)
  373.         {
  374.             java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  375.         }
  376.         catch (javax.swing.UnsupportedLookAndFeelException ex)
  377.         {
  378.             java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  379.         }
  380.         //</editor-fold>
  381.  
  382.         /* Create and display the form */
  383.         java.awt.EventQueue.invokeLater(new Runnable()
  384.         {
  385.             public void run()
  386.             {
  387.                 new MemoryInsert().setVisible(true);
  388.             }
  389.         });
  390.     }
  391.  
  392.     // Variables declaration - do not modify
  393.     private javax.swing.JButton backToMenuButton;
  394.     private javax.swing.JLabel dateLabel;
  395.     private javax.swing.JLabel descriptionLabel;
  396.     private javax.swing.JButton exitButton;
  397.     private javax.swing.JButton insertButton;
  398.     private javax.swing.JLabel insertMemoryLabel;
  399.     private javax.swing.JTextField memoryDateTextField;
  400.     private javax.swing.JScrollPane memoryDescriptionScrollPane;
  401.     private javax.swing.JTextArea memoryDescriptionTextArea;
  402.     private javax.swing.JTextField memoryNameTextField;
  403.     private javax.swing.JTextField memoryPlaceTextField;
  404.     private javax.swing.JTextField memoryTimeTextField;
  405.     private javax.swing.JLabel nameLabel;
  406.     private javax.swing.JLabel placeLabel;
  407.     private javax.swing.JLabel timeLabel;
  408.     // End of variables declaration
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement