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.LineBorder;
- 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;
- //their is another class that is public, please dont point this as an error
- //infacti have left out more than you think, i just want you to get the code from
- //which the erro generated.
- class StudentRegistrationForm
- extends JDialog implements ActionListener, ListSelectionListener {
- 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();
- public StudentRegistrationForm(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 okButton = new JButton("OK");
- okButton.setLocation(19,12);
- okButton.addActionListener(this);
- JButton cancelButton = new JButton("Cancel");
- cancelButton.setLocation(24,12);
- cancelButton.addActionListener(this);
- panel.add(okButton,BorderLayout.SOUTH);
- panel.add(cancelButton, BorderLayout.SOUTH);
- contentPane.add(panel,BorderLayout.CENTER);
- pack();
- }
- StudentRegistrationForm() {
- }
- public void actionPerformed(ActionEvent ae) {
- if(ae.getActionCommand().equals("OK")){
- 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{
- StudentRegistrationForm registration = new StudentRegistrationForm();
- registration.registerNewStudent();
- }
- } else if(ae.getActionCommand().equals("Cancel")){
- hide(); //temporary method
- }
- }
- public void registerNewStudent(){
- try{ //smaple regNumber >> E35S/7737/2009
- String courseCode = regNumberTextField.getText().substring(0,3).toUpperCase(); //E35
- String regParts [] = regNumberTextField.getText().split("/");
- String studentID = regParts[1].concat("/").concat(regParts[2]); // 7737/2009
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost/eStudents",
- "root",
- "nmvickysdatabaseaccess");
- String registrationQuery = "INSERT INTO KU (" +
- "course_code," +
- "student_id," +
- "first_name," +
- "last_name." +
- "reg_number," +
- "campus," +
- "reg_time," +
- "reg_date)" +
- "VALUES(?,?,?,?,?,?,?,?)";
- PreparedStatement pstmt = con.prepareStatement(registrationQuery);
- pstmt.setString(1, courseCode);
- pstmt.setString(2, studentID);
- pstmt.setString(3, firstNameTextField.getText().toUpperCase());
- pstmt.setString(4, lastNameTextField.getText().toUpperCase());
- pstmt.setString(5, regNumberTextField.getText().toUpperCase());
- pstmt.setString(6, (String)campusList.getSelectedValue().toString.toUpperCase());
- pstmt.setString(7, dt.getTime());
- pstmt.setString(8, dt.getDate());
- pstmt.executeQuery();
- String msg = "Succesfully registered "+regNumberTextField.getText().toUpperCase()+
- "of "+(String)campusList.getSelectedValue().toString().toUpperCase()+" campus!";
- //already noted some error here
- JOptionPane.showMessageDialog(this, msg,"Estudent rRegistration" ,JOptionPane.INFORMATION_MESSAGE);
- }catch(Exception e){
- JOptionPane.showMessageDialog(this, "Sorry, registration failed!","Estudent Registration" ,JOptionPane.INFORMATION_MESSAGE);
- e.printStackTrace();
- }
- }
- public void valueChanged(ListSelectionEvent e_) {
- campusList.repaint();
- }
- private JList campusList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement