Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package diary;
- import java.awt.Color;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- /**
- *
- * @author root
- */
- public class MemoryInsert extends javax.swing.JFrame
- {
- /**
- * Creates new form MemoryInsert
- */
- public MemoryInsert()
- {
- initComponents();
- getContentPane().setBackground(Color.CYAN);
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents()
- {
- insertMemoryLabel = new javax.swing.JLabel();
- nameLabel = new javax.swing.JLabel();
- memoryNameTextField = new javax.swing.JTextField();
- dateLabel = new javax.swing.JLabel();
- memoryDateTextField = new javax.swing.JTextField();
- timeLabel = new javax.swing.JLabel();
- memoryTimeTextField = new javax.swing.JTextField();
- placeLabel = new javax.swing.JLabel();
- memoryPlaceTextField = new javax.swing.JTextField();
- descriptionLabel = new javax.swing.JLabel();
- memoryDescriptionScrollPane = new javax.swing.JScrollPane();
- memoryDescriptionTextArea = new javax.swing.JTextArea();
- exitButton = new javax.swing.JButton();
- insertButton = new javax.swing.JButton();
- backToMenuButton = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- insertMemoryLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 12)); // NOI18N
- insertMemoryLabel.setForeground(new java.awt.Color(255, 0, 0));
- insertMemoryLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- insertMemoryLabel.setText("INSERT A MEMORY");
- nameLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
- nameLabel.setForeground(new java.awt.Color(0, 0, 255));
- nameLabel.setText("NAME :");
- memoryNameTextField.setBackground(new java.awt.Color(0, 0, 0));
- memoryNameTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- memoryNameTextField.setForeground(new java.awt.Color(0, 255, 0));
- dateLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
- dateLabel.setForeground(new java.awt.Color(0, 0, 255));
- dateLabel.setText("DATE (YYYY-MM-DD) :");
- memoryDateTextField.setBackground(new java.awt.Color(0, 0, 0));
- memoryDateTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- memoryDateTextField.setForeground(new java.awt.Color(0, 255, 0));
- memoryDateTextField.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- memoryDateTextFieldActionPerformed(evt);
- }
- });
- timeLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
- timeLabel.setForeground(new java.awt.Color(0, 0, 255));
- timeLabel.setText("TIME(HH:MM:SS) :");
- memoryTimeTextField.setBackground(new java.awt.Color(0, 0, 0));
- memoryTimeTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- memoryTimeTextField.setForeground(new java.awt.Color(0, 255, 0));
- placeLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
- placeLabel.setForeground(new java.awt.Color(0, 0, 255));
- placeLabel.setText("PLACE :");
- memoryPlaceTextField.setBackground(new java.awt.Color(0, 0, 0));
- memoryPlaceTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- memoryPlaceTextField.setForeground(new java.awt.Color(0, 255, 0));
- descriptionLabel.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
- descriptionLabel.setForeground(new java.awt.Color(0, 0, 255));
- descriptionLabel.setText("DESCRIPTION :");
- memoryDescriptionTextArea.setBackground(new java.awt.Color(0, 0, 0));
- memoryDescriptionTextArea.setColumns(20);
- memoryDescriptionTextArea.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- memoryDescriptionTextArea.setForeground(new java.awt.Color(0, 255, 0));
- memoryDescriptionTextArea.setRows(5);
- memoryDescriptionScrollPane.setViewportView(memoryDescriptionTextArea);
- exitButton.setBackground(new java.awt.Color(0, 0, 0));
- exitButton.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
- exitButton.setForeground(new java.awt.Color(0, 255, 0));
- exitButton.setText("EXIT");
- exitButton.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- exitButtonActionPerformed(evt);
- }
- });
- insertButton.setBackground(new java.awt.Color(0, 0, 0));
- insertButton.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
- insertButton.setForeground(new java.awt.Color(0, 255, 0));
- insertButton.setText("INSERT");
- insertButton.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- insertButtonActionPerformed(evt);
- }
- });
- backToMenuButton.setBackground(new java.awt.Color(0, 0, 0));
- backToMenuButton.setFont(new java.awt.Font("Papyrus", 1, 12)); // NOI18N
- backToMenuButton.setForeground(new java.awt.Color(0, 255, 0));
- backToMenuButton.setText("BACK TO MENU");
- backToMenuButton.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- backToMenuButtonActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(insertButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(insertMemoryLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(memoryNameTextField)
- .addComponent(memoryPlaceTextField)
- .addComponent(descriptionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(memoryDescriptionScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(memoryDateTextField)
- .addComponent(dateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(memoryTimeTextField)
- .addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)))
- .addComponent(placeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(backToMenuButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(insertMemoryLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(nameLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(memoryNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(dateLabel)
- .addComponent(timeLabel))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(memoryDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(memoryTimeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(placeLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(memoryPlaceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(descriptionLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(memoryDescriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(insertButton)
- .addGap(18, 18, 18)
- .addComponent(backToMenuButton)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(exitButton)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- setLocationRelativeTo(null);
- }// </editor-fold>
- private void insertButtonActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here:
- String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
- String host = "jdbc:sqlserver://localhost:61271";
- String username = "sa";
- String password = "alvi";
- try
- {
- String memoryName = memoryNameTextField.getText();
- String memoryDate = memoryDateTextField.getText();
- String memoryTime = memoryTimeTextField.getText();
- String memoryPlace = memoryPlaceTextField.getText();
- String memoryDescription = memoryDescriptionTextArea.getText();
- int maxID=0;
- //fetch result
- //connect database and fetch result
- Class.forName(driver);
- Connection connection = DriverManager.getConnection(host, username, password);
- Statement statement = connection.createStatement();
- statement.executeUpdate
- (
- " INSERT INTO diary.dbo.memoryAll(memoryName) "+
- " VALUES('"+memoryName+"')"
- );
- ResultSet resultSet = statement.executeQuery("SELECT memoryID FROM diary.dbo.memoryAll");
- while(resultSet.next())
- {
- int variableMemoryID=resultSet.getInt("memoryID");
- maxID=variableMemoryID;
- }
- statement.executeUpdate
- (
- " INSERT INTO diary.dbo.memoryPlace(memoryPlaceName,memoryID) "+
- " VALUES('"+memoryPlace+"',"+maxID+")"
- );
- statement.executeUpdate
- (
- " INSERT INTO diary.dbo.memoryDescription(memoryDescriptionFull,memoryID) "+
- " VALUES('"+memoryDescription+"',"+maxID+")"
- );
- //convert string to util date
- SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
- java.util.Date utilDate1 = null;
- utilDate1 = formatter1.parse(memoryDate);
- //covert util date to sql date
- java.sql.Date sqlDate = new java.sql.Date(utilDate1.getTime());
- //convert string to util time
- SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
- java.util.Date utilDate2 = null;
- utilDate2 = formatter2.parse(memoryTime);
- //covert util date to sql date
- java.sql.Time sqlTime = new java.sql.Time(utilDate2.getTime());
- statement.executeUpdate
- (
- " INSERT INTO diary.dbo.memoryMoment(memoryMomentDate,memoryMomentTime,memoryID) "+
- " VALUES('"+sqlDate+"','"+sqlTime+"',"+maxID+")"
- );
- //close all
- resultSet.close();
- statement.close();
- connection.close();
- this.setVisible(false);
- MemoryIndex memoryIndex = new MemoryIndex();
- memoryIndex.setVisible(true);
- }
- catch(SQLException sqlException)
- {
- sqlException.printStackTrace();
- JOptionPane.showMessageDialog
- (this,
- "Error code : "+sqlException.getErrorCode()+"\n"+
- "Error message : "+sqlException.getMessage()+"\n"+
- "SQL state : "+sqlException.getSQLState()+"\n"
- );
- }
- catch (ParseException parseException)
- {
- parseException.printStackTrace();
- System.out.println();
- System.out.println("Error message : "+parseException.getMessage());
- System.out.println("Error offset : "+parseException.getErrorOffset());
- }
- catch (ClassNotFoundException classNotFoundException)
- {
- classNotFoundException.printStackTrace();
- JOptionPane.showMessageDialog
- (this,
- "Error message : "+classNotFoundException.getMessage()+"\n"
- );
- }
- this.setVisible(false);
- MemoryIndex memoryIndex = new MemoryIndex();
- memoryIndex.setVisible(true);
- }
- private void memoryDateTextFieldActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here:
- }
- private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here:
- System.exit(0);
- }
- private void backToMenuButtonActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here:
- this.setVisible(false);
- Menu menu = new Menu();
- menu.setVisible(true);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[])
- {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try
- {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
- {
- if ("Nimbus".equals(info.getName()))
- {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- }
- catch (ClassNotFoundException ex)
- {
- java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- catch (InstantiationException ex)
- {
- java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- catch (IllegalAccessException ex)
- {
- java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- catch (javax.swing.UnsupportedLookAndFeelException ex)
- {
- java.util.logging.Logger.getLogger(MemoryInsert.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- new MemoryInsert().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton backToMenuButton;
- private javax.swing.JLabel dateLabel;
- private javax.swing.JLabel descriptionLabel;
- private javax.swing.JButton exitButton;
- private javax.swing.JButton insertButton;
- private javax.swing.JLabel insertMemoryLabel;
- private javax.swing.JTextField memoryDateTextField;
- private javax.swing.JScrollPane memoryDescriptionScrollPane;
- private javax.swing.JTextArea memoryDescriptionTextArea;
- private javax.swing.JTextField memoryNameTextField;
- private javax.swing.JTextField memoryPlaceTextField;
- private javax.swing.JTextField memoryTimeTextField;
- private javax.swing.JLabel nameLabel;
- private javax.swing.JLabel placeLabel;
- private javax.swing.JLabel timeLabel;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement