rootUser

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

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