Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package studentClient.simpledb;
- import java.awt.event.*;
- import java.sql.*;
- import java.awt.*;
- import javax.swing.*;
- import javax.swing.border.*;
- import javax.swing.table.*;
- import simpledb.remote.SimpleDriver;
- public class StudentManager extends JFrame {
- private JTabbedPane tabbedPane1;
- private JPanel panel1;
- private JScrollPane scrollPane1;
- private JTable table1;
- private JPanel panel4;
- private JTextField textField9;
- private JLabel label9;
- private JTextField textField10;
- private JLabel label10;
- private JTextField textField11;
- private JLabel label11;
- private JTextField textField12;
- private JLabel label12;
- private JButton button1;
- private JLabel label13;
- private JPanel panel2;
- private JPanel panel15;
- private JPanel panel13;
- private JPanel panel14;
- private JLabel label6;
- private JTextField textField5;
- private JButton button2;
- private JLabel label14;
- private JComboBox comboBox4;
- private JPanel panel16;
- private JLabel label15;
- private JScrollPane scrollPane2;
- private JTable table2;
- Driver d;
- Connection conn=null;
- Statement stmt;
- ResultSet rs;
- String query;
- int datasize=0;
- public StudentManager(int datasize) {
- initComponents();
- this.datasize=datasize;
- //Our JTable model
- updateTable();
- }
- public void updateTable(){
- try{
- d = new SimpleDriver();
- conn = d.connect("jdbc:simpledb://localhost", null);
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select SId,SName,MajorId,GradYear from STUDENT");
- Object table[][]=new Object[datasize][4];
- int i=0;
- while(rs.next()){
- table[i][0]=rs.getInt("SId");
- table[i][1]=rs.getString("SName");
- table[i][2]=rs.getInt("MajorId");
- table[i][3]=rs.getInt("GradYear");
- i++;
- }
- table1.setModel(new DefaultTableModel(table,new String[] {"SId", "SName", "MajorId", "GradYear"}));
- }
- catch(Exception e){
- e.printStackTrace();
- }
- try{
- if(conn!= null){
- conn.close();
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- private void button1ActionPerformed(ActionEvent e) {
- String SId = textField9.getText();
- String SName = textField10.getText();
- String MajorId = textField11.getText();
- String GradYear = textField12.getText();
- try{
- String cmd = "update STUDENT set Sname='"+SName +"' "
- +"where SId = "+SId;
- stmt.executeUpdate(cmd);
- cmd = "update STUDENT set MajorId="+MajorId +" "
- + "where SId = "+SId;
- stmt.executeUpdate(cmd);
- cmd = "update STUDENT set GradYear="+GradYear +" "
- + "where SId = "+SId;
- stmt.executeUpdate(cmd);
- }
- catch(Exception se){
- se.printStackTrace();
- }
- try{
- if(conn!= null){
- conn.close();
- }
- }
- catch(Exception z){
- z.printStackTrace();
- }
- updateTable();
- table1.repaint();
- }
- private void button2ActionPerformed(ActionEvent e) {
- try{
- conn = d.connect("jdbc:simpledb://localhost", null);
- stmt = conn.createStatement();
- if(comboBox4.getSelectedIndex()==0){
- query="select sid,sname,majorid,gradyear from student where Sid="+textField5.getText();
- }
- else if(comboBox4.getSelectedIndex()==1){
- query="select sid,sname,majorid,gradyear from student where Sname='"+textField5.getText()+"'";
- }
- else if(comboBox4.getSelectedIndex()==2){
- query="select sid,sname,majorid,gradyear from student where majorId="+textField5.getText();
- }
- else{
- query="select sid,sname,majorid,gradyear from student where gradyear="+textField5.getText();
- }
- rs=stmt.executeQuery(query);
- Object results[][] = new Object[datasize][4];
- int i=0;
- while(rs.next()){
- results[i][0]=rs.getInt("SId");
- results[i][1]=rs.getString("SName");
- results[i][2]=rs.getInt("MajorId");
- results[i][3]=rs.getInt("GradYear");
- i++;
- }
- table2.setModel(new DefaultTableModel(results,new String[] {"SId", "SName", "MajorId", "GradYear"}));
- table2.repaint();
- try{
- if(conn!=null)
- conn.close();
- }
- catch(Exception err){
- err.printStackTrace();
- }
- }
- catch(SQLException err ){
- err.printStackTrace();
- }
- }
- private void comboBox4ItemStateChanged(ItemEvent e) {
- label6.setText(comboBox4.getSelectedItem().toString());
- }
- private void initComponents() {
- // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
- tabbedPane1 = new JTabbedPane();
- panel1 = new JPanel();
- scrollPane1 = new JScrollPane();
- table1 = new JTable();
- panel4 = new JPanel();
- textField9 = new JTextField();
- label9 = new JLabel();
- textField10 = new JTextField();
- label10 = new JLabel();
- textField11 = new JTextField();
- label11 = new JLabel();
- textField12 = new JTextField();
- label12 = new JLabel();
- button1 = new JButton();
- label13 = new JLabel();
- panel2 = new JPanel();
- panel15 = new JPanel();
- panel13 = new JPanel();
- panel14 = new JPanel();
- label6 = new JLabel();
- textField5 = new JTextField();
- button2 = new JButton();
- label14 = new JLabel();
- comboBox4 = new JComboBox();
- panel16 = new JPanel();
- label15 = new JLabel();
- scrollPane2 = new JScrollPane();
- table2 = new JTable();
- //======== this ========
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- setFont(new Font("Calibri", Font.PLAIN, 12));
- setTitle("Student Viewer/Editor");
- setResizable(false);
- Container contentPane = getContentPane();
- contentPane.setLayout(null);
- //======== tabbedPane1 ========
- {
- tabbedPane1.setTabPlacement(SwingConstants.LEFT);
- //======== panel1 ========
- {
- panel1.setLayout(null);
- //======== scrollPane1 ========
- {
- //---- table1 ----
- table1.setModel(new DefaultTableModel(
- new Object[][] {
- {null, null, null, null},
- {null, null, null, null},
- },
- new String[] {
- "SId", "SName", "MajorId", "GradYear"
- }
- ) {
- Class[] columnTypes = new Class[] {
- Integer.class, String.class, Integer.class, String.class
- };
- boolean[] columnEditable = new boolean[] {
- false, true, false, true
- };
- @Override
- public Class<?> getColumnClass(int columnIndex) {
- return columnTypes[columnIndex];
- }
- @Override
- public boolean isCellEditable(int rowIndex, int columnIndex) {
- return columnEditable[columnIndex];
- }
- });
- scrollPane1.setViewportView(table1);
- }
- panel1.add(scrollPane1);
- scrollPane1.setBounds(20, 5, 630, 435);
- //======== panel4 ========
- {
- panel4.setBorder(LineBorder.createGrayLineBorder());
- panel4.setBackground(new Color(204, 255, 204));
- panel4.setLayout(null);
- panel4.add(textField9);
- textField9.setBounds(10, 45, 90, 20);
- //---- label9 ----
- label9.setText("SId");
- panel4.add(label9);
- label9.setBounds(30, 25, 26, 14);
- panel4.add(textField10);
- textField10.setBounds(110, 45, 225, 20);
- //---- label10 ----
- label10.setText("SName");
- panel4.add(label10);
- label10.setBounds(210, 20, 45, 20);
- panel4.add(textField11);
- textField11.setBounds(345, 45, 90, 20);
- //---- label11 ----
- label11.setText("MajorId");
- panel4.add(label11);
- label11.setBounds(365, 20, 45, 20);
- panel4.add(textField12);
- textField12.setBounds(445, 45, 90, 20);
- //---- label12 ----
- label12.setText("GradYear");
- panel4.add(label12);
- label12.setBounds(460, 20, 55, 20);
- //---- button1 ----
- button1.setText("Edit");
- button1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- button1ActionPerformed(e);
- }
- });
- panel4.add(button1);
- button1.setBounds(545, 40, 80, 25);
- }
- panel1.add(panel4);
- panel4.setBounds(20, 480, 630, 85);
- //---- label13 ----
- label13.setText("Edit");
- label13.setFont(new Font("Calibri", Font.PLAIN, 18));
- label13.setForeground(new Color(51, 153, 0));
- panel1.add(label13);
- label13.setBounds(310, 450, 45, label13.getPreferredSize().height);
- { // compute preferred size
- Dimension preferredSize = new Dimension();
- for(int i = 0; i < panel1.getComponentCount(); i++) {
- Rectangle bounds = panel1.getComponent(i).getBounds();
- preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
- preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
- }
- Insets insets = panel1.getInsets();
- preferredSize.width += insets.right;
- preferredSize.height += insets.bottom;
- panel1.setMinimumSize(preferredSize);
- panel1.setPreferredSize(preferredSize);
- }
- }
- tabbedPane1.addTab("View / Edit", panel1);
- //======== panel2 ========
- {
- panel2.setLayout(null);
- //======== panel15 ========
- {
- panel15.setBorder(LineBorder.createGrayLineBorder());
- panel15.setBackground(new Color(204, 255, 204));
- panel15.setLayout(null);
- //======== panel13 ========
- {
- panel13.setBorder(LineBorder.createGrayLineBorder());
- panel13.setBackground(new Color(153, 255, 153));
- panel13.setLayout(null);
- //======== panel14 ========
- {
- panel14.setBorder(LineBorder.createGrayLineBorder());
- panel14.setBackground(new Color(204, 255, 204));
- panel14.setLayout(null);
- //---- label6 ----
- label6.setText("Student ID");
- panel14.add(label6);
- label6.setBounds(20, 15, 95, 14);
- panel14.add(textField5);
- textField5.setBounds(170, 10, 330, 20);
- { // compute preferred size
- Dimension preferredSize = new Dimension();
- for(int i = 0; i < panel14.getComponentCount(); i++) {
- Rectangle bounds = panel14.getComponent(i).getBounds();
- preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
- preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
- }
- Insets insets = panel14.getInsets();
- preferredSize.width += insets.right;
- preferredSize.height += insets.bottom;
- panel14.setMinimumSize(preferredSize);
- panel14.setPreferredSize(preferredSize);
- }
- }
- panel13.add(panel14);
- panel14.setBounds(15, 25, 525, 40);
- //---- button2 ----
- button2.setText("Search");
- button2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- button2ActionPerformed(e);
- button2ActionPerformed(e);
- }
- });
- panel13.add(button2);
- button2.setBounds(225, 80, 95, 20);
- { // compute preferred size
- Dimension preferredSize = new Dimension();
- for(int i = 0; i < panel13.getComponentCount(); i++) {
- Rectangle bounds = panel13.getComponent(i).getBounds();
- preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
- preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
- }
- Insets insets = panel13.getInsets();
- preferredSize.width += insets.right;
- preferredSize.height += insets.bottom;
- panel13.setMinimumSize(preferredSize);
- panel13.setPreferredSize(preferredSize);
- }
- }
- panel15.add(panel13);
- panel13.setBounds(25, 70, 555, 105);
- //---- label14 ----
- label14.setText("Search By:");
- label14.setFont(new Font("Calibri", Font.PLAIN, 16));
- label14.setForeground(new Color(51, 153, 0));
- panel15.add(label14);
- label14.setBounds(195, 40, 67, 20);
- //---- comboBox4 ----
- comboBox4.setModel(new DefaultComboBoxModel(new String[] {
- "Student Id",
- "Student Name",
- "Major Id",
- "Graduation Year"
- }));
- comboBox4.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent e) {
- comboBox4ItemStateChanged(e);
- }
- });
- panel15.add(comboBox4);
- comboBox4.setBounds(290, 40, 103, 20);
- { // compute preferred size
- Dimension preferredSize = new Dimension();
- for(int i = 0; i < panel15.getComponentCount(); i++) {
- Rectangle bounds = panel15.getComponent(i).getBounds();
- preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
- preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
- }
- Insets insets = panel15.getInsets();
- preferredSize.width += insets.right;
- preferredSize.height += insets.bottom;
- panel15.setMinimumSize(preferredSize);
- panel15.setPreferredSize(preferredSize);
- }
- }
- panel2.add(panel15);
- panel15.setBounds(25, 5, 610, 200);
- //======== panel16 ========
- {
- panel16.setBorder(LineBorder.createGrayLineBorder());
- panel16.setBackground(new Color(204, 255, 204));
- panel16.setLayout(null);
- //---- label15 ----
- label15.setText("Search Results");
- label15.setFont(new Font("Calibri", Font.PLAIN, 16));
- label15.setForeground(new Color(51, 153, 0));
- panel16.add(label15);
- label15.setBounds(225, 5, 95, 20);
- //======== scrollPane2 ========
- {
- //---- table2 ----
- table2.setModel(new DefaultTableModel(
- new Object[][] {
- {null, null, null, null},
- },
- new String[] {
- "SId", "SName", "MajorId", "GradYear"
- }
- ));
- scrollPane2.setViewportView(table2);
- }
- panel16.add(scrollPane2);
- scrollPane2.setBounds(15, 40, 580, 300);
- { // compute preferred size
- Dimension preferredSize = new Dimension();
- for(int i = 0; i < panel16.getComponentCount(); i++) {
- Rectangle bounds = panel16.getComponent(i).getBounds();
- preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
- preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
- }
- Insets insets = panel16.getInsets();
- preferredSize.width += insets.right;
- preferredSize.height += insets.bottom;
- panel16.setMinimumSize(preferredSize);
- panel16.setPreferredSize(preferredSize);
- }
- }
- panel2.add(panel16);
- panel16.setBounds(25, 220, 610, 350);
- { // compute preferred size
- Dimension preferredSize = new Dimension();
- for(int i = 0; i < panel2.getComponentCount(); i++) {
- Rectangle bounds = panel2.getComponent(i).getBounds();
- preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
- preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
- }
- Insets insets = panel2.getInsets();
- preferredSize.width += insets.right;
- preferredSize.height += insets.bottom;
- panel2.setMinimumSize(preferredSize);
- panel2.setPreferredSize(preferredSize);
- }
- }
- tabbedPane1.addTab("Search", panel2);
- }
- contentPane.add(tabbedPane1);
- tabbedPane1.setBounds(5, 5, 745, 585);
- { // compute preferred size
- Dimension preferredSize = new Dimension();
- for(int i = 0; i < contentPane.getComponentCount(); i++) {
- Rectangle bounds = contentPane.getComponent(i).getBounds();
- preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
- preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
- }
- Insets insets = contentPane.getInsets();
- preferredSize.width += insets.right;
- preferredSize.height += insets.bottom;
- contentPane.setMinimumSize(preferredSize);
- contentPane.setPreferredSize(preferredSize);
- }
- setSize(765, 625);
- setLocationRelativeTo(getOwner());
- // JFormDesigner - End of component initialization //GEN-END:initComponents
- }
- public static void main(String args[]){
- new StudentManager(60).setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement