Advertisement
Guest User

Untitled

a guest
May 4th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. package estudentadmin;
  2. import charva.awt.*;
  3. import charva.awt.Toolkit;
  4. import charva.awt.event.*;
  5. import charvax.swing.event.ListSelectionEvent;
  6. import org.apache.commons.logging.Log;
  7. import org.apache.commons.logging.LogFactory;
  8. import charvax.swing.*;
  9. import charvax.swing.border.EmptyBorder;
  10. import charvax.swing.border.TitledBorder;
  11. import charvax.swing.event.ListSelectionListener;
  12. import java.sql.SQLException;
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.PreparedStatement;
  16. import java.sql.ResultSet;
  17. import java.util.Properties;
  18.  
  19. class StudentRegistration
  20. extends JDialog implements ActionListener, ListSelectionListener {
  21. private String firstName;
  22. private String lastName;
  23. private String regNumber;
  24. private String campus;
  25. private JTextField firstNameTextField = new JTextField(14);
  26. private JTextField lastNameTextField = new JTextField(14);
  27. private JTextField regNumberTextField = new JTextField(13);
  28. Validator valid = new Validator();
  29. Font textFieldFont = new Font("Monospaced",Font.BOLD,12);
  30. DateTime dt = new DateTime();
  31. Connection con;
  32. PreparedStatement ps;
  33. Properties p = new Properties();
  34. public StudentRegistration(Frame regForm){
  35. super(regForm,"Estudent registration | KU");
  36. Container contentPane = getContentPane();
  37.  
  38. JPanel panel = new JPanel();
  39. panel.setLayout(null);
  40. panel.setSize(50, 14);
  41. panel.setLocation(3, 4);
  42.  
  43.  
  44.  
  45. JLabel title = new JLabel("KENYATTA UNIVERSITY STUDENT");
  46. title.setLocation(15, 1);
  47. panel.add(title);
  48.  
  49. JLabel firstNameLabel = new JLabel("First Name: ");
  50. firstNameLabel.setLocation(3, 2);
  51. panel.add(firstNameLabel);
  52.  
  53. JLabel lastNameLabel = new JLabel("Last Name: ");
  54. lastNameLabel.setLocation(3, 3);
  55. panel.add(lastNameLabel);
  56.  
  57. JLabel regNumLabel = new JLabel("Reg. Number: ");
  58. regNumLabel.setLocation(3, 4);
  59. panel.add(regNumLabel);
  60.  
  61. JLabel campusLabel = new JLabel("Select campus: ");
  62. campusLabel.setLocation(3, 6);
  63. panel.add(campusLabel);
  64.  
  65.  
  66. firstNameTextField.setLocation(15, 2);
  67. firstNameTextField.setFont(textFieldFont);
  68. panel.add(firstNameTextField);
  69. lastNameTextField.setLocation(15, 3);
  70. lastNameTextField.setFont(textFieldFont);
  71. panel.add(lastNameTextField);
  72.  
  73. regNumberTextField.setLocation(16, 4);
  74. regNumberTextField.setFont(textFieldFont);
  75. panel.add(regNumberTextField);
  76.  
  77.  
  78. String [] campuses = {"MAIN","RUIRU","PARKLANDS","MOMBASA","KITUI"};
  79. campusList = new JList(campuses);
  80. campusList.setVisibleRowCount(3);
  81. campusList.addListSelectionListener(this);
  82. JScrollPane scrollPane = new JScrollPane(campusList);
  83. TitledBorder theCampusList = new TitledBorder("Campus");
  84. scrollPane.setViewportBorder(theCampusList);
  85. scrollPane.setLocation(18, 6);
  86. panel.add(scrollPane);
  87.  
  88.  
  89. JButton registerButton = new JButton("Register");
  90. registerButton.setLocation(11,12);
  91. registerButton.addActionListener(this);
  92.  
  93. JButton cancelButton = new JButton("Cancel");
  94. cancelButton.setLocation(24,12);
  95. cancelButton.addActionListener(this);
  96.  
  97. panel.add(registerButton,BorderLayout.SOUTH);
  98. panel.add(cancelButton, BorderLayout.SOUTH);
  99.  
  100.  
  101. contentPane.add(panel,BorderLayout.CENTER);
  102.  
  103.  
  104. pack();
  105. }
  106.  
  107. StudentRegistration() {
  108.  
  109. }
  110.  
  111. public void actionPerformed(ActionEvent ae) {
  112. if(ae.getActionCommand().equals("Register")){
  113. if(firstNameTextField.getText().isEmpty() ||
  114. lastNameTextField.getText().isEmpty() ||
  115. regNumberTextField.getText().isEmpty())
  116. {
  117. JOptionPane.showMessageDialog(this, "All fields must be filled!","Estudent Registration" ,JOptionPane.ERROR_MESSAGE);
  118. }
  119. else if(!valid.kenyattaUniversityRegistrationNumber(regNumberTextField.getText().toUpperCase()))
  120. {
  121. String [] messages = {regNumberTextField.getText().toUpperCase(),
  122. "Invalid Kenyatta University registration number!"};
  123. JOptionPane.showMessageDialog(this, messages,"Estudent Registration" ,JOptionPane.INFORMATION_MESSAGE);
  124. }
  125. else if(!valid.name(firstNameTextField.getText()) ||
  126. !valid.name(lastNameTextField.getText())
  127. ){
  128. JOptionPane.showMessageDialog(this, "Names should not contain numbers!","Estudent Registration" ,JOptionPane.INFORMATION_MESSAGE);
  129. } else if(new KenyattaUniversityStudent((String)campusList.getSelectedValue().toString().toUpperCase(),regNumberTextField.getText().toUpperCase()) == null){
  130. campus = (String)campusList.getSelectedValue().toString().toUpperCase();
  131. regNumber = regNumberTextField.getText().toUpperCase();
  132. firstName = firstNameTextField.getText().toUpperCase();
  133. lastName = lastNameTextField.getText().toUpperCase();
  134. this.registerNewStudent(campus, regNumber, firstName, lastName);
  135. JOptionPane.showMessageDialog(this, "Succesful registration!", "Estudent Registration", JOptionPane.INFORMATION_MESSAGE);
  136. } else{
  137. JOptionPane.showMessageDialog(this, "Already registered!", "Estudent Registration", JOptionPane.INFORMATION_MESSAGE);
  138. }
  139. } else if(ae.getActionCommand().equals("Cancel")){
  140. hide(); //temporary method
  141. }
  142. }
  143. public void registerNewStudent(String _campus,
  144. String _regNumber,
  145. String _firstName,
  146. String _lastName) {
  147.  
  148. campus = _campus;
  149. regNumber = _regNumber;
  150. firstName = _firstName;
  151. lastName = _lastName;
  152.  
  153. KenyattaUniversityStudent kuStudent = new KenyattaUniversityStudent(campus,regNumber);
  154. String courseCode = kuStudent.getCourseCode();
  155. String studentID = kuStudent.getStudenID();
  156. try { //Connect to the database
  157. Class.forName("com.mysql.jdbc.Driver").newInstance();
  158. p.put("user", "root");
  159. p.put("password", "nmvickysdatabaseaccess");
  160. con = DriverManager.getConnection("jdbc:mysql://localhost/eStudents",p);
  161.  
  162. //create a statement to insert into the records.
  163. String registerStudent = "INSERT into KU ("
  164. // change this t a variable ^^^
  165. +"course_code," //student course
  166. +"student_id," //student ID
  167. +"first_name," //student first name
  168. +"last_name, " //student last name
  169. +"reg_number,"//student registration number
  170. +"campus," //student campus
  171. +"reg_time," //registration time
  172. +"reg_date)" //day of registration
  173. +"VALUES (?,?,?,?,?,?,?,?)";
  174. //prepare the statement created above
  175. ps = con.prepareStatement(registerStudent);
  176.  
  177. //set the values to appropriate parameteres in the statement
  178. ps.setString(1, courseCode);
  179. ps.setString(2, studentID);
  180. ps.setString(3, firstName);
  181. ps.setString(4, lastName);
  182. ps.setString(5, regNumber);
  183. ps.setString(6, campus);
  184. ps.setString(7, dt.getTime()); //time
  185. ps.setString(8, dt.getDate()); //and day of registration
  186.  
  187. //execute the statement
  188. ps.executeUpdate();
  189.  
  190. //release the statements object's database and JDBC resources
  191. ps.close();
  192.  
  193. }catch(SQLException e){
  194. e.printStackTrace();
  195. }catch(Exception e){
  196. e.printStackTrace();
  197.  
  198. }finally {
  199. //close database connection
  200. if( con != null){
  201. try { con.close(); }
  202. catch (Exception e){ e.printStackTrace();}
  203. }
  204.  
  205.  
  206. }
  207. } //end of method registerNewStudent
  208.  
  209. public void valueChanged(ListSelectionEvent e_) {
  210. campusList.repaint();
  211. }
  212. private JList campusList;
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement