Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package estudentadmin;
- import charva.awt.*;
- import charva.awt.Toolkit;
- import charva.awt.event.*;
- import charvax.swing.event.ListSelectionEvent;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import charvax.swing.*;
- import charvax.swing.border.EmptyBorder;
- import charvax.swing.border.TitledBorder;
- import charvax.swing.event.ListSelectionListener;
- import java.sql.SQLException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Properties;
- class StudentRegistration
- extends JDialog implements ActionListener, ListSelectionListener {
- private String firstName;
- private String lastName;
- private String regNumber;
- private String campus;
- private JTextField firstNameTextField = new JTextField(14);
- private JTextField lastNameTextField = new JTextField(14);
- private JTextField regNumberTextField = new JTextField(13);
- Validator valid = new Validator();
- Font textFieldFont = new Font("Monospaced",Font.BOLD,12);
- DateTime dt = new DateTime();
- Connection con;
- PreparedStatement ps;
- Properties p = new Properties();
- public StudentRegistration(Frame regForm){
- super(regForm,"Estudent registration | KU");
- Container contentPane = getContentPane();
- JPanel panel = new JPanel();
- panel.setLayout(null);
- panel.setSize(50, 14);
- panel.setLocation(3, 4);
- JLabel title = new JLabel("KENYATTA UNIVERSITY STUDENT");
- title.setLocation(15, 1);
- panel.add(title);
- JLabel firstNameLabel = new JLabel("First Name: ");
- firstNameLabel.setLocation(3, 2);
- panel.add(firstNameLabel);
- JLabel lastNameLabel = new JLabel("Last Name: ");
- lastNameLabel.setLocation(3, 3);
- panel.add(lastNameLabel);
- JLabel regNumLabel = new JLabel("Reg. Number: ");
- regNumLabel.setLocation(3, 4);
- panel.add(regNumLabel);
- JLabel campusLabel = new JLabel("Select campus: ");
- campusLabel.setLocation(3, 6);
- panel.add(campusLabel);
- firstNameTextField.setLocation(15, 2);
- firstNameTextField.setFont(textFieldFont);
- panel.add(firstNameTextField);
- lastNameTextField.setLocation(15, 3);
- lastNameTextField.setFont(textFieldFont);
- panel.add(lastNameTextField);
- regNumberTextField.setLocation(16, 4);
- regNumberTextField.setFont(textFieldFont);
- panel.add(regNumberTextField);
- String [] campuses = {"MAIN","RUIRU","PARKLANDS","MOMBASA","KITUI"};
- campusList = new JList(campuses);
- campusList.setVisibleRowCount(3);
- campusList.addListSelectionListener(this);
- JScrollPane scrollPane = new JScrollPane(campusList);
- TitledBorder theCampusList = new TitledBorder("Campus");
- scrollPane.setViewportBorder(theCampusList);
- scrollPane.setLocation(18, 6);
- panel.add(scrollPane);
- JButton registerButton = new JButton("Register");
- registerButton.setLocation(11,12);
- registerButton.addActionListener(this);
- JButton cancelButton = new JButton("Cancel");
- cancelButton.setLocation(24,12);
- cancelButton.addActionListener(this);
- panel.add(registerButton,BorderLayout.SOUTH);
- panel.add(cancelButton, BorderLayout.SOUTH);
- contentPane.add(panel,BorderLayout.CENTER);
- pack();
- }
- StudentRegistration() {
- }
- public void actionPerformed(ActionEvent ae) {
- if(ae.getActionCommand().equals("Register")){
- if(firstNameTextField.getText().isEmpty() ||
- lastNameTextField.getText().isEmpty() ||
- regNumberTextField.getText().isEmpty())
- {
- JOptionPane.showMessageDialog(this, "All fields must be filled!","Estudent Registration" ,JOptionPane.ERROR_MESSAGE);
- }
- else if(!valid.kenyattaUniversityRegistrationNumber(regNumberTextField.getText().toUpperCase()))
- {
- String [] messages = {regNumberTextField.getText().toUpperCase(),
- "Invalid Kenyatta University registration number!"};
- JOptionPane.showMessageDialog(this, messages,"Estudent Registration" ,JOptionPane.INFORMATION_MESSAGE);
- }
- else if(!valid.name(firstNameTextField.getText()) ||
- !valid.name(lastNameTextField.getText())
- ){
- JOptionPane.showMessageDialog(this, "Names should not contain numbers!","Estudent Registration" ,JOptionPane.INFORMATION_MESSAGE);
- } else if(new KenyattaUniversityStudent((String)campusList.getSelectedValue().toString().toUpperCase(),regNumberTextField.getText().toUpperCase()) == null){
- campus = (String)campusList.getSelectedValue().toString().toUpperCase();
- regNumber = regNumberTextField.getText().toUpperCase();
- firstName = firstNameTextField.getText().toUpperCase();
- lastName = lastNameTextField.getText().toUpperCase();
- this.registerNewStudent(campus, regNumber, firstName, lastName);
- JOptionPane.showMessageDialog(this, "Succesful registration!", "Estudent Registration", JOptionPane.INFORMATION_MESSAGE);
- } else{
- JOptionPane.showMessageDialog(this, "Already registered!", "Estudent Registration", JOptionPane.INFORMATION_MESSAGE);
- }
- } else if(ae.getActionCommand().equals("Cancel")){
- hide(); //temporary method
- }
- }
- public void registerNewStudent(String _campus,
- String _regNumber,
- String _firstName,
- String _lastName) {
- campus = _campus;
- regNumber = _regNumber;
- firstName = _firstName;
- lastName = _lastName;
- KenyattaUniversityStudent kuStudent = new KenyattaUniversityStudent(campus,regNumber);
- String courseCode = kuStudent.getCourseCode();
- String studentID = kuStudent.getStudenID();
- try { //Connect to the database
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- p.put("user", "root");
- p.put("password", "nmvickysdatabaseaccess");
- con = DriverManager.getConnection("jdbc:mysql://localhost/eStudents",p);
- //create a statement to insert into the records.
- String registerStudent = "INSERT into KU ("
- // change this t a variable ^^^
- +"course_code," //student course
- +"student_id," //student ID
- +"first_name," //student first name
- +"last_name, " //student last name
- +"reg_number,"//student registration number
- +"campus," //student campus
- +"reg_time," //registration time
- +"reg_date)" //day of registration
- +"VALUES (?,?,?,?,?,?,?,?)";
- //prepare the statement created above
- ps = con.prepareStatement(registerStudent);
- //set the values to appropriate parameteres in the statement
- ps.setString(1, courseCode);
- ps.setString(2, studentID);
- ps.setString(3, firstName);
- ps.setString(4, lastName);
- ps.setString(5, regNumber);
- ps.setString(6, campus);
- ps.setString(7, dt.getTime()); //time
- ps.setString(8, dt.getDate()); //and day of registration
- //execute the statement
- ps.executeUpdate();
- //release the statements object's database and JDBC resources
- ps.close();
- }catch(SQLException e){
- e.printStackTrace();
- }catch(Exception e){
- e.printStackTrace();
- }finally {
- //close database connection
- if( con != null){
- try { con.close(); }
- catch (Exception e){ e.printStackTrace();}
- }
- }
- } //end of method registerNewStudent
- public void valueChanged(ListSelectionEvent e_) {
- campusList.repaint();
- }
- private JList campusList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement