Java project

profileSosso
DataGUI.java

package com.company; import java.sql.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.*; /** * * @author Sosso */ public class DataGUI extends JFrame implements ActionListener { JTabbedPane tab = new JTabbedPane(); JPanel dialP = new JPanel(new GridLayout(5, 2)); JPanel viewP = new JPanel(); JPanel insertP = new JPanel(); JPanel deleteP = new JPanel(); //View Panel JLabel label1 = new JLabel("Student ID:"); JTextField id = new JTextField(); JButton viewB = new JButton("View"); JDialog dialogView = new JDialog(); //Insert Panel JPanel topI = new JPanel(new GridLayout(6, 2)); JPanel bottomI = new JPanel(new FlowLayout(FlowLayout.CENTER)); JLabel firstName = new JLabel("First Name:"); JLabel lastName = new JLabel("Last Name:"); JLabel studentID = new JLabel("Student ID:"); JLabel viewFN = new JLabel("First Name:"); JLabel viewLN = new JLabel("Last Name:"); JLabel viewSID = new JLabel("Student ID:"); JLabel v1 = new JLabel(); JLabel v2 = new JLabel(); JLabel v3 = new JLabel(); JButton insertI = new JButton("Add Record"); JTextField t1 = new JTextField(10); JTextField t2 = new JTextField(10); JTextField t3 = new JTextField(10); JTextField t4 = new JTextField(10); JTextField t5 = new JTextField(10); //Delete JLabel delete = new JLabel("Enter the ID of the student whose record you want to delete."); JLabel sIDD = new JLabel("Student ID:"); JTextField idD = new JTextField(); JButton deleteD = new JButton("Delete Record"); //DB Bootup static final String MY_DRIVER = "net.ucanaccess.jdbc.UcanaccessDriver"; static final String DB_USERNAME = ""; static final String DB_PASSWORD = ""; static String tableName = "StudentInfo"; DataGUI() { //JDialog dialP.add(viewSID); dialP.add(v1); dialP.add(viewFN); dialP.add(v2); dialP.add(viewLN); dialP.add(v3); v1.setBackground(Color.white); v2.setBackground(Color.white); v3.setBackground(Color.white); dialogView.add(dialP); dialogView.setSize(800, 600); dialogView.setPreferredSize(new Dimension(800, 600)); //View Subpanel viewP.setLayout(new GridLayout(2, 2)); viewP.add(label1); viewP.add(id); viewP.add(viewB); tab.addTab("View Record", viewP); tab.setMnemonicAt(0, KeyEvent.VK_1); //Insert Subpanel insertP.setLayout(new GridLayout(1, 1)); topI.add(studentID); topI.add(t1); topI.add(firstName); topI.add(t2); topI.add(lastName); topI.add(t3); topI.add(insertI); insertP.add(topI); tab.addTab("Insert Record", insertP); tab.setMnemonicAt(1, KeyEvent.VK_2); //Delete Subpanel deleteP.setLayout(new GridLayout(2, 2)); deleteP.add(sIDD); deleteP.add(idD); deleteP.add(deleteD); tab.addTab("Delete Record", deleteP); tab.setMnemonicAt(2, KeyEvent.VK_3); //Add ActionListener viewB.addActionListener(this); deleteD.addActionListener(this); insertI.addActionListener(this); //Framing this.setLocation(30, 30); this.getContentPane().add(tab); this.pack(); this.setTitle("Student Record System."); this.setSize(800, 600); this.setVisible(true); setDefaultCloseOperation(this.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(viewB) && !id.getText().equals("")) { try { //load the driver Class.forName(MY_DRIVER); String database = "jdbc:ucanaccess://C:/Users/maxisoffack/Downloads/StudentDB.accdb"; Connection conn = DriverManager.getConnection(database, DB_USERNAME, DB_PASSWORD); Statement s = conn.createStatement();// use PreparedStatement in professional code INSTEAD OF Statement // get table String selectStatementSQL = "SELECT * FROM " + tableName + " WHERE ID = " + id.getText(); s.execute(selectStatementSQL); ResultSet rs = s.getResultSet(); if ((rs != null || rs == null) && rs.next()) { v1.setText(rs.getString(1)); v2.setText(rs.getString(2)); v3.setText(rs.getString(3)); dialogView.setTitle(rs.getString(2) + " " + rs.getString(3) + "'s Record"); dialogView.setVisible(true); dialogView.setDefaultCloseOperation(dialogView.DISPOSE_ON_CLOSE); } else { dialogView.dispose(); JDialog a = new JDialog(this); a.setSize(400, 200); a.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); JPanel p = new JPanel(new GridLayout(1, 1)); JLabel w = new JLabel("ID not in records.\n\nClose this window and enter a new ID #"); p.add(w); a.add(p); a.setVisible(true); a.setDefaultCloseOperation(dialogView.DISPOSE_ON_CLOSE); } s.close(); conn.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (e.getSource().equals(insertI)) { try { //load the driver Class.forName(MY_DRIVER); String database = "jdbc:ucanaccess://C:/Users/maxisoffack/Downloads/StudentDB.accdb"; Connection conn = DriverManager.getConnection(database, DB_USERNAME, DB_PASSWORD); Statement s = conn.createStatement();// use PreparedStatement in professional code INSTEAD OF Statement // get table if (t1 != null) { String insertStatementSQL = "INSERT INTO " + tableName + " (ID, FirstName, LastName, HomeTown, FavouriteColour) " + "VALUES(" + t1.getText() + ", " + t2.getText() + ", " + t3.getText() + ", " + t4.getText() + ", " + t5.getText() + ")"; s.execute(insertStatementSQL); } else { dialogView.dispose(); JDialog a = new JDialog(this); a.setSize(400, 200); a.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); JPanel p = new JPanel(new GridLayout(1, 1)); JLabel w = new JLabel("ID not in records.\n\nClose this window and enter a new ID #"); p.add(w); a.add(p); a.setVisible(true); a.setDefaultCloseOperation(dialogView.DISPOSE_ON_CLOSE); } s.close(); conn.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (e.getSource().equals(deleteD)) { try { //load the driver Class.forName(MY_DRIVER); String database = "jdbc:ucanaccess://C:/Users/maxisoffack/Downloads/StudentDB.accdb"; Connection conn = DriverManager.getConnection(database, DB_USERNAME, DB_PASSWORD); Statement ds = conn.createStatement();// use PreparedStatement in professional code INSTEAD OF Statement // get table String selectStatementSQL = "DELETE * FROM " + tableName + " WHERE ID = " + idD.getText(); ds.execute(selectStatementSQL); ds.close(); conn.close(); } catch (Exception ex) { ex.printStackTrace(); } } } }