Advertisement
Guest User

Untitled

a guest
Apr 11th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. student fxml controller
  2.  
  3. i have a similar code for teacher that populate database to table view and it works but dont know why am having error in this one the last error points to
  4. teacherTable.setItems(ban);
  5.  
  6. import java.sql.Connection;
  7.  
  8. import java.sql.DriverManager;
  9.  
  10.  
  11. import java.sql.ResultSet;
  12.  
  13. import com.mysql.jdbc.PreparedStatement;
  14.  
  15. import java.sql.SQLException;
  16. import java.util.Calendar;
  17.  
  18. import javafx.collections.FXCollections;
  19. import javafx.collections.ObservableList;
  20. import javafx.fxml.FXML;
  21. import javafx.scene.control.Alert;
  22. import javafx.scene.control.Alert.AlertType;
  23. import javafx.scene.control.cell.PropertyValueFactory;
  24. import javafx.scene.control.DatePicker;
  25. import javafx.scene.control.TableColumn;
  26. import javafx.scene.control.TableView;
  27. import javafx.scene.control.TextField;
  28.  
  29.  
  30. public class StudentZoneController {
  31. Connection con;
  32. PreparedStatement p;
  33. String rowid,fname,lname,age,sex,cl;
  34. ResultSet set;
  35. Alert popup;
  36.  
  37.  
  38.  
  39.  
  40. @FXML
  41. private TableView<usera> teacherTable;
  42.  
  43.  
  44. @FXML
  45. private TableColumn<usera, String> ROWID;
  46.  
  47. @FXML
  48. private TableColumn<usera, String> FIRSTNAME;
  49.  
  50. @FXML
  51. private TableColumn<usera, String> LASTNAME;
  52.  
  53. @FXML
  54. private TableColumn<usera, String> AGE;
  55.  
  56. @FXML
  57. private TableColumn<usera, String> SEX;
  58.  
  59. @FXML
  60. private TableColumn<usera, String> CLASS;
  61.  
  62.  
  63.  
  64. @FXML
  65. private void load ()
  66. {
  67.  
  68.  
  69. ObservableList<usera> ban =FXCollections.observableArrayList( );
  70.  
  71. teacherTable.setItems(ban);
  72.  
  73. }
  74.  
  75. @FXML
  76. private void initialize()
  77. {
  78. ROWID.setCellValueFactory(new PropertyValueFactory<usera,String>("rowid"));
  79. FIRSTNAME.setCellValueFactory(new PropertyValueFactory<usera,String>("firstname"));
  80. LASTNAME.setCellValueFactory(new PropertyValueFactory<usera,String>("lastname"));
  81. AGE.setCellValueFactory(new PropertyValueFactory<usera,String>("age"));
  82. SEX.setCellValueFactory(new PropertyValueFactory<usera,String>("sex"));
  83. CLASS.setCellValueFactory(new PropertyValueFactory<usera,String>("primary"));
  84.  
  85. }
  86.  
  87. @FXML
  88. private TextField t1;
  89.  
  90. @FXML
  91. private TextField t2;
  92.  
  93. @FXML
  94. private TextField t3;
  95.  
  96. @FXML
  97. private DatePicker dob;
  98.  
  99.  
  100. @FXML
  101. private TextField t4;
  102.  
  103. @FXML
  104. private TextField t5;
  105.  
  106. @FXML
  107. private TextField t6;
  108.  
  109. @FXML
  110. private TextField t7;
  111.  
  112. @FXML
  113. private TextField t8;
  114.  
  115. @FXML
  116. private TextField t9;
  117.  
  118. @FXML
  119. private TextField t10;
  120.  
  121. @FXML
  122. private TextField t11;
  123.  
  124. @FXML
  125. private TextField t12;
  126.  
  127. @FXML
  128. private TextField t13;
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. @FXML
  140. private void showAge()
  141. {
  142. Calendar now = Calendar.getInstance();
  143. int year = now.get(Calendar.YEAR);
  144. int birth = (dob.getValue().getYear());
  145. int age = year-birth;
  146. t4.setText(Integer.toString(age) + "years old");
  147.  
  148.  
  149.  
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156. @FXML
  157. private void addNewUser() throws SQLException
  158. {
  159.  
  160. try
  161. {
  162. Class.forName("com.mysql.jdbc.Driver").newInstance();
  163. com.mysql.jdbc.Connection con =(com.mysql.jdbc.Connection) DriverManager.getConnection("jdbc:mysql://localhost/school?"+"user=root&password =");
  164. p = (com.mysql.jdbc.PreparedStatement)con.prepareStatement("insert into students values(?,?,?,?,?,?)");
  165. p.setString(1, t1.getText());
  166. p.setString(2, t2.getText());
  167. p.setString(3, t3.getText());
  168. p.setString(4, t4.getText());
  169. p.setString(5, t5.getText());
  170. p.setString(6, t6.getText());
  171.  
  172. popup = new Alert(AlertType.INFORMATION);
  173. popup.setTitle("INFORMATION DIALOG");
  174. popup.setHeaderText(null);
  175. popup.setContentText("user created");
  176. popup.showAndWait();
  177.  
  178. p.executeUpdate();
  179. p.close();
  180. clearFields();
  181. }
  182.  
  183. catch(Exception eb)
  184. {
  185.  
  186. }
  187.  
  188.  
  189.  
  190. }
  191.  
  192.  
  193.  
  194. @FXML
  195. private void deleteStudent()
  196. {
  197.  
  198. try
  199. {
  200. Class.forName("com.mysql.jdbc.Driver").newInstance();
  201. com.mysql.jdbc.Connection con =(com.mysql.jdbc.Connection) DriverManager.getConnection("jdbc:mysql://localhost/school?"+"user=root&password =");
  202. p = (com.mysql.jdbc.PreparedStatement)con.prepareStatement("delete from students where FIRSTNAME = ? || LASTNAME = ?");
  203. p.setString(1, t2.getText());
  204. p.setString(2, t3.getText());
  205.  
  206. popup = new Alert(AlertType.INFORMATION);
  207. popup.setTitle("INFORMATION DIALOG");
  208. popup.setHeaderText(null);
  209. popup.setContentText("STUDENT DELETED");
  210. popup.showAndWait();
  211. refreshDelete();
  212.  
  213. p.executeUpdate();
  214. }
  215.  
  216. catch(Exception l)
  217. {
  218. popup = new Alert(AlertType.INFORMATION);
  219. popup.setTitle("INFORMATION DIALOG");
  220. popup.setHeaderText(null);
  221. popup.setContentText(l.getMessage());
  222. popup.showAndWait();
  223.  
  224.  
  225. }
  226.  
  227.  
  228. }
  229.  
  230.  
  231. private void clearFields()
  232. {
  233. t1.setText("");
  234. t2.setText("");
  235. t3.setText("");
  236. t4.setText("");
  237. t5.setText("");
  238. t6.setText("");
  239. dob.setValue(null);
  240.  
  241.  
  242.  
  243. }
  244.  
  245. @FXML
  246. private void resetFields()
  247. {
  248. t9.clear();
  249. t10.clear();
  250. t11.clear();
  251. t12.clear();
  252. t13.clear();
  253. }
  254. private void refreshDelete()
  255. {
  256.  
  257. t2.clear();
  258. t3.clear();
  259. }
  260.  
  261.  
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement