Advertisement
Guest User

Untitled

a guest
May 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.77 KB | None | 0 0
  1. package studentClient.simpledb;
  2. import java.awt.event.*;
  3. import java.sql.*;
  4. import java.awt.*;
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7. import javax.swing.table.*;
  8.  
  9. import simpledb.remote.SimpleDriver;
  10.  
  11. public  class StudentManager extends JFrame {
  12.    
  13.     private JTabbedPane tabbedPane1;
  14.     private JPanel panel1;
  15.     private JScrollPane scrollPane1;
  16.     private JTable table1;
  17.     private JPanel panel4;
  18.     private JTextField textField9;
  19.     private JLabel label9;
  20.     private JTextField textField10;
  21.     private JLabel label10;
  22.     private JTextField textField11;
  23.     private JLabel label11;
  24.     private JTextField textField12;
  25.     private JLabel label12;
  26.     private JButton button1;
  27.     private JLabel label13;
  28.     private JPanel panel2;
  29.     private JPanel panel15;
  30.     private JPanel panel13;
  31.     private JPanel panel14;
  32.     private JLabel label6;
  33.     private JTextField textField5;
  34.     private JButton button2;
  35.     private JLabel label14;
  36.     private JComboBox comboBox4;
  37.     private JPanel panel16;
  38.     private JLabel label15;
  39.     private JScrollPane scrollPane2;
  40.     private JTable table2;
  41.  
  42.     Driver d;
  43.     Connection conn=null;
  44.     Statement stmt;
  45.     ResultSet rs;
  46.     String query;
  47.     int datasize=0;
  48.    
  49.     public StudentManager(int datasize) {
  50.         initComponents();
  51.         this.datasize=datasize;
  52.         //Our JTable model
  53.         updateTable();
  54.  
  55.     }
  56.    
  57.  
  58.     public void updateTable(){
  59.         try{
  60.             d = new SimpleDriver();
  61.             conn = d.connect("jdbc:simpledb://localhost", null);
  62.             stmt = conn.createStatement();
  63.             rs = stmt.executeQuery("select SId,SName,MajorId,GradYear from STUDENT");
  64.             Object table[][]=new Object[datasize][4];
  65.             int i=0;
  66.             while(rs.next()){
  67.                 table[i][0]=rs.getInt("SId");
  68.                 table[i][1]=rs.getString("SName");
  69.                 table[i][2]=rs.getInt("MajorId");
  70.                 table[i][3]=rs.getInt("GradYear");
  71.                 i++;
  72.             }
  73.             table1.setModel(new DefaultTableModel(table,new String[] {"SId", "SName", "MajorId", "GradYear"}));
  74.         }
  75.         catch(Exception e){
  76.             e.printStackTrace();
  77.            
  78.         }
  79.         try{
  80.             if(conn!= null){
  81.                 conn.close();
  82.             }
  83.         }
  84.         catch(Exception e){
  85.             e.printStackTrace();
  86.            
  87.         }
  88.     }
  89.    
  90.     private void button1ActionPerformed(ActionEvent e) {
  91.         String SId = textField9.getText();
  92.         String SName = textField10.getText();
  93.         String MajorId = textField11.getText();
  94.         String GradYear = textField12.getText();
  95.         try{
  96.             String cmd = "update STUDENT set Sname='"+SName +"' "
  97.                             +"where SId = "+SId;
  98.             stmt.executeUpdate(cmd);
  99.            
  100.              cmd = "update STUDENT set MajorId="+MajorId +" "
  101.                     + "where SId = "+SId;
  102.             stmt.executeUpdate(cmd);
  103.             cmd = "update STUDENT set GradYear="+GradYear +" "
  104.                 + "where SId = "+SId;
  105.    
  106.             stmt.executeUpdate(cmd);
  107.         }
  108.         catch(Exception se){
  109.             se.printStackTrace();
  110.         }
  111.         try{
  112.             if(conn!= null){
  113.                 conn.close();
  114.             }
  115.         }
  116.         catch(Exception z){
  117.             z.printStackTrace();
  118.            
  119.         }
  120.         updateTable();
  121.         table1.repaint();
  122.     }
  123.  
  124.     private void button2ActionPerformed(ActionEvent e) {
  125.    
  126.         try{
  127.             conn = d.connect("jdbc:simpledb://localhost", null);
  128.             stmt = conn.createStatement();
  129.             if(comboBox4.getSelectedIndex()==0){
  130.                 query="select sid,sname,majorid,gradyear from student where Sid="+textField5.getText();
  131.             }
  132.             else if(comboBox4.getSelectedIndex()==1){
  133.                 query="select sid,sname,majorid,gradyear from student where Sname='"+textField5.getText()+"'";
  134.             }
  135.             else if(comboBox4.getSelectedIndex()==2){
  136.                 query="select sid,sname,majorid,gradyear from student where majorId="+textField5.getText();
  137.             }
  138.             else{
  139.                 query="select sid,sname,majorid,gradyear from student where gradyear="+textField5.getText();
  140.             }
  141.             rs=stmt.executeQuery(query);
  142.             Object results[][] = new Object[datasize][4];
  143.             int i=0;
  144.             while(rs.next()){
  145.                 results[i][0]=rs.getInt("SId");
  146.                 results[i][1]=rs.getString("SName");
  147.                 results[i][2]=rs.getInt("MajorId");
  148.                 results[i][3]=rs.getInt("GradYear");
  149.                 i++;
  150.             }
  151.             table2.setModel(new DefaultTableModel(results,new String[] {"SId", "SName", "MajorId", "GradYear"}));
  152.             table2.repaint();
  153.             try{
  154.                 if(conn!=null)
  155.                     conn.close();
  156.             }
  157.             catch(Exception err){
  158.                 err.printStackTrace();
  159.             }
  160.            
  161.         }
  162.         catch(SQLException err ){
  163.             err.printStackTrace();
  164.         }
  165.     }
  166.  
  167.     private void comboBox4ItemStateChanged(ItemEvent e) {
  168.         label6.setText(comboBox4.getSelectedItem().toString());
  169.     }
  170.  
  171.     private void initComponents() {
  172.         // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
  173.         tabbedPane1 = new JTabbedPane();
  174.         panel1 = new JPanel();
  175.         scrollPane1 = new JScrollPane();
  176.         table1 = new JTable();
  177.         panel4 = new JPanel();
  178.         textField9 = new JTextField();
  179.         label9 = new JLabel();
  180.         textField10 = new JTextField();
  181.         label10 = new JLabel();
  182.         textField11 = new JTextField();
  183.         label11 = new JLabel();
  184.         textField12 = new JTextField();
  185.         label12 = new JLabel();
  186.         button1 = new JButton();
  187.         label13 = new JLabel();
  188.         panel2 = new JPanel();
  189.         panel15 = new JPanel();
  190.         panel13 = new JPanel();
  191.         panel14 = new JPanel();
  192.         label6 = new JLabel();
  193.         textField5 = new JTextField();
  194.         button2 = new JButton();
  195.         label14 = new JLabel();
  196.         comboBox4 = new JComboBox();
  197.         panel16 = new JPanel();
  198.         label15 = new JLabel();
  199.         scrollPane2 = new JScrollPane();
  200.         table2 = new JTable();
  201.  
  202.         //======== this ========
  203.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  204.         setFont(new Font("Calibri", Font.PLAIN, 12));
  205.         setTitle("Student Viewer/Editor");
  206.         setResizable(false);
  207.         Container contentPane = getContentPane();
  208.         contentPane.setLayout(null);
  209.  
  210.         //======== tabbedPane1 ========
  211.         {
  212.             tabbedPane1.setTabPlacement(SwingConstants.LEFT);
  213.  
  214.             //======== panel1 ========
  215.             {
  216.                 panel1.setLayout(null);
  217.  
  218.                 //======== scrollPane1 ========
  219.                 {
  220.  
  221.                     //---- table1 ----
  222.                     table1.setModel(new DefaultTableModel(
  223.                         new Object[][] {
  224.                             {null, null, null, null},
  225.                             {null, null, null, null},
  226.                         },
  227.                         new String[] {
  228.                             "SId", "SName", "MajorId", "GradYear"
  229.                         }
  230.                     ) {
  231.                         Class[] columnTypes = new Class[] {
  232.                             Integer.class, String.class, Integer.class, String.class
  233.                         };
  234.                         boolean[] columnEditable = new boolean[] {
  235.                             false, true, false, true
  236.                         };
  237.                         @Override
  238.                         public Class<?> getColumnClass(int columnIndex) {
  239.                             return columnTypes[columnIndex];
  240.                         }
  241.                         @Override
  242.                         public boolean isCellEditable(int rowIndex, int columnIndex) {
  243.                             return columnEditable[columnIndex];
  244.                         }
  245.                     });
  246.                     scrollPane1.setViewportView(table1);
  247.                 }
  248.                 panel1.add(scrollPane1);
  249.                 scrollPane1.setBounds(20, 5, 630, 435);
  250.  
  251.                 //======== panel4 ========
  252.                 {
  253.                     panel4.setBorder(LineBorder.createGrayLineBorder());
  254.                     panel4.setBackground(new Color(204, 255, 204));
  255.                     panel4.setLayout(null);
  256.                     panel4.add(textField9);
  257.                     textField9.setBounds(10, 45, 90, 20);
  258.  
  259.                     //---- label9 ----
  260.                     label9.setText("SId");
  261.                     panel4.add(label9);
  262.                     label9.setBounds(30, 25, 26, 14);
  263.                     panel4.add(textField10);
  264.                     textField10.setBounds(110, 45, 225, 20);
  265.  
  266.                     //---- label10 ----
  267.                     label10.setText("SName");
  268.                     panel4.add(label10);
  269.                     label10.setBounds(210, 20, 45, 20);
  270.                     panel4.add(textField11);
  271.                     textField11.setBounds(345, 45, 90, 20);
  272.  
  273.                     //---- label11 ----
  274.                     label11.setText("MajorId");
  275.                     panel4.add(label11);
  276.                     label11.setBounds(365, 20, 45, 20);
  277.                     panel4.add(textField12);
  278.                     textField12.setBounds(445, 45, 90, 20);
  279.  
  280.                     //---- label12 ----
  281.                     label12.setText("GradYear");
  282.                     panel4.add(label12);
  283.                     label12.setBounds(460, 20, 55, 20);
  284.  
  285.                     //---- button1 ----
  286.                     button1.setText("Edit");
  287.                     button1.addActionListener(new ActionListener() {
  288.                         public void actionPerformed(ActionEvent e) {
  289.                             button1ActionPerformed(e);
  290.                         }
  291.                     });
  292.                     panel4.add(button1);
  293.                     button1.setBounds(545, 40, 80, 25);
  294.                 }
  295.                 panel1.add(panel4);
  296.                 panel4.setBounds(20, 480, 630, 85);
  297.  
  298.                 //---- label13 ----
  299.                 label13.setText("Edit");
  300.                 label13.setFont(new Font("Calibri", Font.PLAIN, 18));
  301.                 label13.setForeground(new Color(51, 153, 0));
  302.                 panel1.add(label13);
  303.                 label13.setBounds(310, 450, 45, label13.getPreferredSize().height);
  304.  
  305.                 { // compute preferred size
  306.                     Dimension preferredSize = new Dimension();
  307.                     for(int i = 0; i < panel1.getComponentCount(); i++) {
  308.                         Rectangle bounds = panel1.getComponent(i).getBounds();
  309.                         preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  310.                         preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  311.                     }
  312.                     Insets insets = panel1.getInsets();
  313.                     preferredSize.width += insets.right;
  314.                     preferredSize.height += insets.bottom;
  315.                     panel1.setMinimumSize(preferredSize);
  316.                     panel1.setPreferredSize(preferredSize);
  317.                 }
  318.             }
  319.             tabbedPane1.addTab("View / Edit", panel1);
  320.  
  321.  
  322.             //======== panel2 ========
  323.             {
  324.                 panel2.setLayout(null);
  325.  
  326.                 //======== panel15 ========
  327.                 {
  328.                     panel15.setBorder(LineBorder.createGrayLineBorder());
  329.                     panel15.setBackground(new Color(204, 255, 204));
  330.                     panel15.setLayout(null);
  331.  
  332.                     //======== panel13 ========
  333.                     {
  334.                         panel13.setBorder(LineBorder.createGrayLineBorder());
  335.                         panel13.setBackground(new Color(153, 255, 153));
  336.                         panel13.setLayout(null);
  337.  
  338.                         //======== panel14 ========
  339.                         {
  340.                             panel14.setBorder(LineBorder.createGrayLineBorder());
  341.                             panel14.setBackground(new Color(204, 255, 204));
  342.                             panel14.setLayout(null);
  343.  
  344.                             //---- label6 ----
  345.                             label6.setText("Student ID");
  346.                             panel14.add(label6);
  347.                             label6.setBounds(20, 15, 95, 14);
  348.                             panel14.add(textField5);
  349.                             textField5.setBounds(170, 10, 330, 20);
  350.  
  351.                             { // compute preferred size
  352.                                 Dimension preferredSize = new Dimension();
  353.                                 for(int i = 0; i < panel14.getComponentCount(); i++) {
  354.                                     Rectangle bounds = panel14.getComponent(i).getBounds();
  355.                                     preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  356.                                     preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  357.                                 }
  358.                                 Insets insets = panel14.getInsets();
  359.                                 preferredSize.width += insets.right;
  360.                                 preferredSize.height += insets.bottom;
  361.                                 panel14.setMinimumSize(preferredSize);
  362.                                 panel14.setPreferredSize(preferredSize);
  363.                             }
  364.                         }
  365.                         panel13.add(panel14);
  366.                         panel14.setBounds(15, 25, 525, 40);
  367.  
  368.                         //---- button2 ----
  369.                         button2.setText("Search");
  370.                         button2.addActionListener(new ActionListener() {
  371.                             public void actionPerformed(ActionEvent e) {
  372.                                 button2ActionPerformed(e);
  373.                                 button2ActionPerformed(e);
  374.                             }
  375.                         });
  376.                         panel13.add(button2);
  377.                         button2.setBounds(225, 80, 95, 20);
  378.  
  379.                         { // compute preferred size
  380.                             Dimension preferredSize = new Dimension();
  381.                             for(int i = 0; i < panel13.getComponentCount(); i++) {
  382.                                 Rectangle bounds = panel13.getComponent(i).getBounds();
  383.                                 preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  384.                                 preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  385.                             }
  386.                             Insets insets = panel13.getInsets();
  387.                             preferredSize.width += insets.right;
  388.                             preferredSize.height += insets.bottom;
  389.                             panel13.setMinimumSize(preferredSize);
  390.                             panel13.setPreferredSize(preferredSize);
  391.                         }
  392.                     }
  393.                     panel15.add(panel13);
  394.                     panel13.setBounds(25, 70, 555, 105);
  395.  
  396.                     //---- label14 ----
  397.                     label14.setText("Search By:");
  398.                     label14.setFont(new Font("Calibri", Font.PLAIN, 16));
  399.                     label14.setForeground(new Color(51, 153, 0));
  400.                     panel15.add(label14);
  401.                     label14.setBounds(195, 40, 67, 20);
  402.  
  403.                     //---- comboBox4 ----
  404.                     comboBox4.setModel(new DefaultComboBoxModel(new String[] {
  405.                         "Student Id",
  406.                         "Student Name",
  407.                         "Major Id",
  408.                         "Graduation Year"
  409.                     }));
  410.                     comboBox4.addItemListener(new ItemListener() {
  411.                         public void itemStateChanged(ItemEvent e) {
  412.                             comboBox4ItemStateChanged(e);
  413.                         }
  414.                     });
  415.                     panel15.add(comboBox4);
  416.                     comboBox4.setBounds(290, 40, 103, 20);
  417.  
  418.                     { // compute preferred size
  419.                         Dimension preferredSize = new Dimension();
  420.                         for(int i = 0; i < panel15.getComponentCount(); i++) {
  421.                             Rectangle bounds = panel15.getComponent(i).getBounds();
  422.                             preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  423.                             preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  424.                         }
  425.                         Insets insets = panel15.getInsets();
  426.                         preferredSize.width += insets.right;
  427.                         preferredSize.height += insets.bottom;
  428.                         panel15.setMinimumSize(preferredSize);
  429.                         panel15.setPreferredSize(preferredSize);
  430.                     }
  431.                 }
  432.                 panel2.add(panel15);
  433.                 panel15.setBounds(25, 5, 610, 200);
  434.  
  435.                 //======== panel16 ========
  436.                 {
  437.                     panel16.setBorder(LineBorder.createGrayLineBorder());
  438.                     panel16.setBackground(new Color(204, 255, 204));
  439.                     panel16.setLayout(null);
  440.  
  441.                     //---- label15 ----
  442.                     label15.setText("Search Results");
  443.                     label15.setFont(new Font("Calibri", Font.PLAIN, 16));
  444.                     label15.setForeground(new Color(51, 153, 0));
  445.                     panel16.add(label15);
  446.                     label15.setBounds(225, 5, 95, 20);
  447.  
  448.                     //======== scrollPane2 ========
  449.                     {
  450.  
  451.                         //---- table2 ----
  452.                         table2.setModel(new DefaultTableModel(
  453.                             new Object[][] {
  454.                                 {null, null, null, null},
  455.  
  456.                             },
  457.                             new String[] {
  458.                                 "SId", "SName", "MajorId", "GradYear"
  459.                             }
  460.                         ));
  461.                         scrollPane2.setViewportView(table2);
  462.                     }
  463.                     panel16.add(scrollPane2);
  464.                     scrollPane2.setBounds(15, 40, 580, 300);
  465.  
  466.                     { // compute preferred size
  467.                         Dimension preferredSize = new Dimension();
  468.                         for(int i = 0; i < panel16.getComponentCount(); i++) {
  469.                             Rectangle bounds = panel16.getComponent(i).getBounds();
  470.                             preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  471.                             preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  472.                         }
  473.                         Insets insets = panel16.getInsets();
  474.                         preferredSize.width += insets.right;
  475.                         preferredSize.height += insets.bottom;
  476.                         panel16.setMinimumSize(preferredSize);
  477.                         panel16.setPreferredSize(preferredSize);
  478.                     }
  479.                 }
  480.                 panel2.add(panel16);
  481.                 panel16.setBounds(25, 220, 610, 350);
  482.  
  483.                 { // compute preferred size
  484.                     Dimension preferredSize = new Dimension();
  485.                     for(int i = 0; i < panel2.getComponentCount(); i++) {
  486.                         Rectangle bounds = panel2.getComponent(i).getBounds();
  487.                         preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  488.                         preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  489.                     }
  490.                     Insets insets = panel2.getInsets();
  491.                     preferredSize.width += insets.right;
  492.                     preferredSize.height += insets.bottom;
  493.                     panel2.setMinimumSize(preferredSize);
  494.                     panel2.setPreferredSize(preferredSize);
  495.                 }
  496.             }
  497.             tabbedPane1.addTab("Search", panel2);
  498.  
  499.         }
  500.         contentPane.add(tabbedPane1);
  501.         tabbedPane1.setBounds(5, 5, 745, 585);
  502.  
  503.         { // compute preferred size
  504.             Dimension preferredSize = new Dimension();
  505.             for(int i = 0; i < contentPane.getComponentCount(); i++) {
  506.                 Rectangle bounds = contentPane.getComponent(i).getBounds();
  507.                 preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  508.                 preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  509.             }
  510.             Insets insets = contentPane.getInsets();
  511.             preferredSize.width += insets.right;
  512.             preferredSize.height += insets.bottom;
  513.             contentPane.setMinimumSize(preferredSize);
  514.             contentPane.setPreferredSize(preferredSize);
  515.         }
  516.         setSize(765, 625);
  517.         setLocationRelativeTo(getOwner());
  518.         // JFormDesigner - End of component initialization  //GEN-END:initComponents
  519.     }
  520.  
  521.     public static void main(String args[]){
  522.         new StudentManager(60).setVisible(true);
  523.     }
  524.  
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement