Advertisement
Guest User

Untitled

a guest
May 9th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 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.LineBorder;
  11. import charvax.swing.border.TitledBorder;
  12. import charvax.swing.event.ListSelectionListener;
  13. import java.sql.SQLException;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18.  
  19. //their is another class that is public, please dont point this as an error
  20. //infacti have left out more than you think, i just want you to get the code from
  21. //which the erro generated.
  22. class StudentRegistrationForm
  23. extends JDialog implements ActionListener, ListSelectionListener {
  24. private JTextField firstNameTextField = new JTextField(14);
  25. private JTextField lastNameTextField = new JTextField(14);
  26. private JTextField regNumberTextField = new JTextField(13);
  27. Validator valid = new Validator();
  28. Font textFieldFont = new Font("Monospaced",Font.BOLD,12);
  29. DateTime dt = new DateTime();
  30. public StudentRegistrationForm(Frame regForm){
  31. super(regForm,"Estudent registration | KU");
  32. Container contentPane = getContentPane();
  33.  
  34. JPanel panel = new JPanel();
  35. panel.setLayout(null);
  36. panel.setSize(50, 14);
  37. panel.setLocation(3, 4);
  38.  
  39.  
  40.  
  41. JLabel title = new JLabel("KENYATTA UNIVERSITY STUDENT");
  42. title.setLocation(15, 1);
  43. panel.add(title);
  44.  
  45. JLabel firstNameLabel = new JLabel("First Name: ");
  46. firstNameLabel.setLocation(3, 2);
  47. panel.add(firstNameLabel);
  48.  
  49. JLabel lastNameLabel = new JLabel("Last Name: ");
  50. lastNameLabel.setLocation(3, 3);
  51. panel.add(lastNameLabel);
  52.  
  53. JLabel regNumLabel = new JLabel("Reg. Number: ");
  54. regNumLabel.setLocation(3, 4);
  55. panel.add(regNumLabel);
  56.  
  57. JLabel campusLabel = new JLabel("Select campus: ");
  58. campusLabel.setLocation(3, 6);
  59. panel.add(campusLabel);
  60.  
  61.  
  62. firstNameTextField.setLocation(15, 2);
  63. firstNameTextField.setFont(textFieldFont);
  64. panel.add(firstNameTextField);
  65. lastNameTextField.setLocation(15, 3);
  66. lastNameTextField.setFont(textFieldFont);
  67. panel.add(lastNameTextField);
  68.  
  69. regNumberTextField.setLocation(16, 4);
  70. regNumberTextField.setFont(textFieldFont);
  71. panel.add(regNumberTextField);
  72.  
  73.  
  74. String [] campuses = {"MAIN","RUIRU","PARKLANDS","MOMBASA","KITUI"};
  75. campusList = new JList(campuses);
  76. campusList.setVisibleRowCount(3);
  77. campusList.addListSelectionListener(this);
  78. JScrollPane scrollPane = new JScrollPane(campusList);
  79. TitledBorder theCampusList = new TitledBorder("Campus");
  80. scrollPane.setViewportBorder(theCampusList);
  81. scrollPane.setLocation(18, 6);
  82. panel.add(scrollPane);
  83.  
  84.  
  85. JButton okButton = new JButton("OK");
  86. okButton.setLocation(19,12);
  87. okButton.addActionListener(this);
  88.  
  89. JButton cancelButton = new JButton("Cancel");
  90. cancelButton.setLocation(24,12);
  91. cancelButton.addActionListener(this);
  92.  
  93. panel.add(okButton,BorderLayout.SOUTH);
  94. panel.add(cancelButton, BorderLayout.SOUTH);
  95.  
  96.  
  97. contentPane.add(panel,BorderLayout.CENTER);
  98.  
  99.  
  100. pack();
  101. }
  102.  
  103. StudentRegistrationForm() {
  104.  
  105. }
  106.  
  107. public void actionPerformed(ActionEvent ae) {
  108. if(ae.getActionCommand().equals("OK")){
  109. if(firstNameTextField.getText().isEmpty() ||
  110. lastNameTextField.getText().isEmpty() ||
  111. regNumberTextField.getText().isEmpty())
  112. {
  113. JOptionPane.showMessageDialog(this, "All fields must be filled!","Estudent Registration" ,JOptionPane.ERROR_MESSAGE);
  114. }
  115. else if(!valid.kenyattaUniversityRegistrationNumber(regNumberTextField.getText().toUpperCase()))
  116. {
  117. String [] messages = {regNumberTextField.getText().toUpperCase(),
  118. "Invalid Kenyatta University registration number!"};
  119. JOptionPane.showMessageDialog(this, messages,"Estudent Registration" ,JOptionPane.INFORMATION_MESSAGE);
  120. }
  121. else if(!valid.name(firstNameTextField.getText()) ||
  122. !valid.name(lastNameTextField.getText())
  123. ){
  124. JOptionPane.showMessageDialog(this, "Names should not contain numbers!","Estudent Registration" ,JOptionPane.INFORMATION_MESSAGE);
  125. } else{
  126. StudentRegistrationForm registration = new StudentRegistrationForm();
  127. registration.registerNewStudent();
  128. }
  129. } else if(ae.getActionCommand().equals("Cancel")){
  130. hide(); //temporary method
  131. }
  132. }
  133. public void registerNewStudent(){
  134. try{ //smaple regNumber >> E35S/7737/2009
  135. String courseCode = regNumberTextField.getText().substring(0,3).toUpperCase(); //E35
  136.  
  137. String regParts [] = regNumberTextField.getText().split("/");
  138. String studentID = regParts[1].concat("/").concat(regParts[2]); // 7737/2009
  139. Class.forName("com.mysql.jdbc.Driver").newInstance();
  140. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/eStudents",
  141. "root",
  142. "nmvickysdatabaseaccess");
  143. String registrationQuery = "INSERT INTO KU (" +
  144. "course_code," +
  145. "student_id," +
  146. "first_name," +
  147. "last_name." +
  148. "reg_number," +
  149. "campus," +
  150. "reg_time," +
  151. "reg_date)" +
  152. "VALUES(?,?,?,?,?,?,?,?)";
  153. PreparedStatement pstmt = con.prepareStatement(registrationQuery);
  154. pstmt.setString(1, courseCode);
  155. pstmt.setString(2, studentID);
  156. pstmt.setString(3, firstNameTextField.getText().toUpperCase());
  157. pstmt.setString(4, lastNameTextField.getText().toUpperCase());
  158. pstmt.setString(5, regNumberTextField.getText().toUpperCase());
  159. pstmt.setString(6, (String)campusList.getSelectedValue().toString.toUpperCase());
  160. pstmt.setString(7, dt.getTime());
  161. pstmt.setString(8, dt.getDate());
  162. pstmt.executeQuery();
  163.  
  164. String msg = "Succesfully registered "+regNumberTextField.getText().toUpperCase()+
  165. "of "+(String)campusList.getSelectedValue().toString().toUpperCase()+" campus!";
  166. //already noted some error here
  167. JOptionPane.showMessageDialog(this, msg,"Estudent rRegistration" ,JOptionPane.INFORMATION_MESSAGE);
  168. }catch(Exception e){
  169. JOptionPane.showMessageDialog(this, "Sorry, registration failed!","Estudent Registration" ,JOptionPane.INFORMATION_MESSAGE);
  170. e.printStackTrace();
  171. }
  172. }
  173. public void valueChanged(ListSelectionEvent e_) {
  174. campusList.repaint();
  175. }
  176. private JList campusList;
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement