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.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JOptionPane;
- import javax.swing.table.DefaultTableModel;
- /**
- *
- * @author root
- */
- public class MemorySearch extends javax.swing.JFrame
- {
- public String[] searchParameters = new String[]
- {"(Select a parameter)","NAME","PLACE","DESCRIPTION"};
- /**
- * Creates new form Search
- */
- public MemorySearch()
- {
- initComponents();
- getContentPane().setBackground(java.awt.Color.gray);
- searchParameterComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(searchParameters));
- }
- /**
- * 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()
- {
- searchLabel = new javax.swing.JLabel();
- searchParameterLabel = new javax.swing.JLabel();
- searchParameterComboBox = new javax.swing.JComboBox<>();
- searchButton = new javax.swing.JButton();
- resultScrollPane = new javax.swing.JScrollPane();
- resultTable = new javax.swing.JTable();
- inputPromptLabel = new javax.swing.JLabel();
- memoryIDLabel = new javax.swing.JLabel();
- memoryIDTextField = new javax.swing.JTextField();
- goButton = new javax.swing.JButton();
- backToMenuButton = new javax.swing.JButton();
- backToOtherMemoryButton = new javax.swing.JButton();
- exitButton = new javax.swing.JButton();
- searchScrollPane = new javax.swing.JScrollPane();
- searchTextArea = new javax.swing.JTextArea();
- stringToSearchLabel = new javax.swing.JLabel();
- searchResultLabel = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- searchLabel.setBackground(new java.awt.Color(0, 0, 0));
- searchLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 12)); // NOI18N
- searchLabel.setForeground(new java.awt.Color(255, 102, 0));
- searchLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- searchLabel.setText("SEARCH");
- searchParameterLabel.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- searchParameterLabel.setForeground(new java.awt.Color(0, 255, 0));
- searchParameterLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- searchParameterLabel.setText("SELECT SEARCH PARAMETER :");
- searchParameterComboBox.setBackground(new java.awt.Color(255, 255, 0));
- searchParameterComboBox.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- searchParameterComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " " }));
- searchButton.setBackground(new java.awt.Color(0, 0, 0));
- searchButton.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- searchButton.setForeground(new java.awt.Color(0, 255, 0));
- searchButton.setText("SEARCH");
- searchButton.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- searchButtonActionPerformed(evt);
- }
- });
- resultScrollPane.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- resultTable.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][]
- {
- },
- new String []
- {
- "memoryID", "searchResult"
- }
- ));
- resultScrollPane.setViewportView(resultTable);
- inputPromptLabel.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- inputPromptLabel.setForeground(new java.awt.Color(0, 255, 255));
- inputPromptLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- inputPromptLabel.setText("INPUT 'MEMORY ID' BELOW FROM ABOVE TABLE FOR DETAILS");
- memoryIDLabel.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- memoryIDLabel.setForeground(new java.awt.Color(255, 255, 0));
- memoryIDLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- memoryIDLabel.setText("MEMORY ID :");
- memoryIDTextField.setBackground(new java.awt.Color(0, 0, 0));
- memoryIDTextField.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- memoryIDTextField.setForeground(new java.awt.Color(0, 255, 0));
- memoryIDTextField.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- memoryIDTextFieldActionPerformed(evt);
- }
- });
- goButton.setBackground(new java.awt.Color(0, 0, 0));
- goButton.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- goButton.setForeground(new java.awt.Color(0, 255, 0));
- goButton.setText("GO");
- goButton.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- goButtonActionPerformed(evt);
- }
- });
- backToMenuButton.setBackground(new java.awt.Color(0, 0, 0));
- backToMenuButton.setFont(new java.awt.Font("sansserif", 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);
- }
- });
- backToOtherMemoryButton.setBackground(new java.awt.Color(0, 0, 0));
- backToOtherMemoryButton.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
- backToOtherMemoryButton.setForeground(new java.awt.Color(0, 255, 0));
- backToOtherMemoryButton.setText("BACK TO OTHER MEMORIES");
- backToOtherMemoryButton.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- backToOtherMemoryButtonActionPerformed(evt);
- }
- });
- exitButton.setBackground(new java.awt.Color(0, 0, 0));
- exitButton.setFont(new java.awt.Font("sansserif", 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);
- }
- });
- searchTextArea.setBackground(new java.awt.Color(0, 0, 0));
- searchTextArea.setColumns(20);
- searchTextArea.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- searchTextArea.setForeground(new java.awt.Color(0, 255, 0));
- searchTextArea.setRows(5);
- searchScrollPane.setViewportView(searchTextArea);
- stringToSearchLabel.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- stringToSearchLabel.setForeground(new java.awt.Color(0, 255, 0));
- stringToSearchLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- stringToSearchLabel.setText("ENTER STRING TO SEARCH :");
- searchResultLabel.setFont(new java.awt.Font("Lucida Console", 1, 12)); // NOI18N
- searchResultLabel.setForeground(new java.awt.Color(0, 255, 255));
- searchResultLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- searchResultLabel.setText("SEARCH RESULT :");
- 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(searchResultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(stringToSearchLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(searchLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(searchParameterLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(searchParameterComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(inputPromptLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(searchScrollPane)
- .addComponent(resultScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(memoryIDLabel, 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))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addComponent(backToOtherMemoryButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGap(8, 8, 8)
- .addComponent(memoryIDTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(goButton, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))))
- .addGap(12, 12, 12)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(searchLabel)
- .addGap(18, 18, 18)
- .addComponent(searchParameterLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(searchParameterComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(stringToSearchLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(searchScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(searchButton)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(searchResultLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(resultScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(inputPromptLabel)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(memoryIDTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(goButton)
- .addComponent(memoryIDLabel)))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(backToMenuButton)
- .addComponent(backToOtherMemoryButton))
- .addGap(18, 18, 18)
- .addComponent(exitButton)
- .addContainerGap(26, Short.MAX_VALUE))
- );
- pack();
- setLocationRelativeTo(null);
- }// </editor-fold>
- private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here:
- System.exit(0);
- }
- private void backToOtherMemoryButtonActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here
- this.setVisible(false);
- MemoryIndex memoryIndex = new MemoryIndex();
- memoryIndex.setVisible(true);
- }
- private void backToMenuButtonActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here:
- this.setVisible(false);
- Menu menu = new Menu();
- menu.setVisible(true);
- }
- private void goButtonActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here:
- int memoryID = Integer.parseInt(memoryIDTextField.getText());
- this.setVisible(false);
- MemoryDetails memoryDetails = new MemoryDetails(memoryID);
- memoryDetails.setVisible(true);
- }
- private void searchButtonActionPerformed(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
- {
- //connect database and fetch result
- Class.forName(driver);
- Connection connection = DriverManager.getConnection(host, username, password);
- Statement statement = connection.createStatement();
- String searchType = searchParameterComboBox.getSelectedItem().toString();
- String searchItem = searchTextArea.getText();
- if(searchType.equals("NAME"))
- {
- ResultSet resultSet = statement.executeQuery
- ("SELECT * FROM diary.dbo.memoryAll " +
- " JOIN diary.dbo.memoryMoment " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryMoment.MEMORYID " +
- " JOIN diary.dbo.memoryPlace " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryPlace.MEMORYID " +
- " JOIN diary.dbo.memoryDescription " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryDescription.MEMORYID "+
- "WHERE memoryName LIKE '%"+searchItem+"%'");
- //add row to table - according to result
- DefaultTableModel defaultTableModel = (DefaultTableModel) resultTable.getModel();
- while(resultSet.next())
- {
- //System.out.println(resultSet.toString());
- int memoryID = resultSet.getInt("memoryID");
- String memoryName = resultSet.getString("memoryName");
- //System.out.println(memoryID+"\n"+memoryDescription);
- defaultTableModel.addRow(new Object[] {memoryID,memoryName});
- }
- resultSet.close();
- }
- else if(searchType.equals("PLACE"))
- {
- ResultSet resultSet = statement.executeQuery
- ("SELECT * FROM diary.dbo.memoryAll " +
- " JOIN diary.dbo.memoryMoment " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryMoment.MEMORYID " +
- " JOIN diary.dbo.memoryPlace " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryPlace.MEMORYID " +
- " JOIN diary.dbo.memoryDescription " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryDescription.MEMORYID "+
- "WHERE memoryPlaceName LIKE '%"+searchItem+"%'");
- //add row to table - according to result
- DefaultTableModel defaultTableModel = (DefaultTableModel) resultTable.getModel();
- while(resultSet.next())
- {
- //System.out.println(resultSet.toString());
- int memoryID = resultSet.getInt("memoryID");
- String memoryPlace = resultSet.getString("memoryPlaceName");
- //System.out.println(memoryID+"\n"+memoryDescription);
- defaultTableModel.addRow(new Object[] {memoryID,memoryPlace});
- }
- resultSet.close();
- }
- else if(searchType.equals("DESCRIPTION"))
- {
- ResultSet resultSet = statement.executeQuery
- ("SELECT * FROM diary.dbo.memoryAll " +
- " JOIN diary.dbo.memoryMoment " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryMoment.MEMORYID " +
- " JOIN diary.dbo.memoryPlace " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryPlace.MEMORYID " +
- " JOIN diary.dbo.memoryDescription " +
- " ON diary.dbo.memoryAll.MEMORYID=diary.dbo.memoryDescription.MEMORYID "+
- "WHERE memoryDescriptionFull LIKE '%"+searchItem+"%'");
- //add row to table - according to result
- DefaultTableModel defaultTableModel = (DefaultTableModel) resultTable.getModel();
- while(resultSet.next())
- {
- //System.out.println(resultSet.toString());
- int memoryID = resultSet.getInt("memoryID");
- String memoryDescription = resultSet.getString("memoryDescriptionFull");
- //System.out.println(memoryID+"\n"+memoryDescription);
- defaultTableModel.addRow(new Object[] {memoryID,memoryDescription});
- }
- resultSet.close();
- }
- statement.close();
- connection.close();
- }
- catch(SQLException sqlException)
- {
- sqlException.printStackTrace();
- JOptionPane.showMessageDialog
- (this,
- "Error code : "+sqlException.getErrorCode()+"\n"+
- "Error message : "+sqlException.getMessage()+"\n"+
- "SQL state : "+sqlException.getSQLState()+"\n"
- );
- }
- catch (ClassNotFoundException classNotFoundException)
- {
- classNotFoundException.printStackTrace();
- JOptionPane.showMessageDialog
- (this,
- "Error message : "+classNotFoundException.getMessage()+"\n"
- );
- }
- }
- private void memoryIDTextFieldActionPerformed(java.awt.event.ActionEvent evt)
- {
- // TODO add your handling code here:
- }
- /**
- * @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(MemorySearch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- catch (InstantiationException ex)
- {
- java.util.logging.Logger.getLogger(MemorySearch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- catch (IllegalAccessException ex)
- {
- java.util.logging.Logger.getLogger(MemorySearch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- catch (javax.swing.UnsupportedLookAndFeelException ex)
- {
- java.util.logging.Logger.getLogger(MemorySearch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- new MemorySearch().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton backToMenuButton;
- private javax.swing.JButton backToOtherMemoryButton;
- private javax.swing.JButton exitButton;
- private javax.swing.JButton goButton;
- private javax.swing.JLabel inputPromptLabel;
- private javax.swing.JLabel memoryIDLabel;
- private javax.swing.JTextField memoryIDTextField;
- private javax.swing.JScrollPane resultScrollPane;
- private javax.swing.JTable resultTable;
- private javax.swing.JButton searchButton;
- private javax.swing.JLabel searchLabel;
- private javax.swing.JComboBox<String> searchParameterComboBox;
- private javax.swing.JLabel searchParameterLabel;
- private javax.swing.JLabel searchResultLabel;
- private javax.swing.JScrollPane searchScrollPane;
- private javax.swing.JTextArea searchTextArea;
- private javax.swing.JLabel stringToSearchLabel;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement