Guest User

Untitled

a guest
Nov 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. TableColumn StudentIDColumn = new TableColumn("StudentID");
  2. StudentIDColumn.setMinWidth(100);
  3. StudentIDColumn.setCellValueFactory(
  4. new PropertyValueFactory<StudentCourse, String>("StudentID"));
  5.  
  6. TableColumn CourseIDColumn = new TableColumn("CourseID");
  7. CourseIDColumn.setMinWidth(100);
  8. CourseIDColumn.setCellValueFactory(
  9. new PropertyValueFactory<StudentCourse, String>("CourseID"));
  10.  
  11. TableColumn GradeColumn =
  12. new TableColumn("Grade");
  13. GradeColumn.setMinWidth(100);
  14. GradeColumn.setCellValueFactory(
  15. new PropertyValueFactory<StudentCourse, String>("Grade"));
  16.  
  17. tableView.getColumns().addAll(StudentIDColumn, CourseIDColumn,
  18. GradeColumn);
  19.  
  20. import javafx.application.Application;
  21. import javafx.beans.property.SimpleBooleanProperty;
  22. import javafx.beans.property.SimpleDoubleProperty;
  23. import javafx.beans.property.SimpleStringProperty;
  24. import javafx.collections.FXCollections;
  25. import javafx.collections.ObservableList;
  26. import javafx.scene.Scene;
  27. import javafx.scene.control.TableColumn;
  28. import javafx.scene.control.TableView;
  29. import javafx.scene.control.cell.PropertyValueFactory;
  30. import javafx.scene.layout.Pane;
  31. import javafx.stage.Stage;
  32. import java.sql.*;
  33.  
  34. class StudentCourse {
  35. final SimpleStringProperty StudentID;
  36. final SimpleStringProperty CourseID;
  37. final SimpleStringProperty Grade;
  38.  
  39. StudentCourse(String StudentID, String CourseID,
  40. String Grade) {
  41. this.StudentID = new SimpleStringProperty(StudentID);
  42. this.CourseID = new SimpleStringProperty(CourseID);
  43. this.Grade = new SimpleStringProperty(Grade);
  44. }
  45.  
  46. String getStudentID() {
  47. return StudentID.get();
  48. }
  49.  
  50. void setStudentID(String StudentID) {
  51. this.StudentID.set(StudentID);
  52. }
  53.  
  54. String getCourseID() {
  55. return CourseID.get();
  56. }
  57.  
  58. void setCourseID(String CourseID) {
  59. this.CourseID.set(CourseID);
  60. }
  61.  
  62. String getGrade() {
  63. return Grade.get();
  64. }
  65.  
  66. void setGrade(String Grade) {
  67. this.Grade.set(Grade);
  68. }
  69. }
  70. class DataConn {
  71. //Interface to connect to the database
  72. private Connection con = null;
  73. String url = "jdbc:mysql://localhost:3306/Registration?useSSL=false";
  74. String uname = "root";
  75. String pass1 = "Root";
  76. public DataConn() {
  77. try {
  78. //step2: load and register the driver
  79. Class.forName("com.mysql.cj.jdbc.Driver");
  80. //step 3: establish connection
  81. con = DriverManager.getConnection(url, uname, pass1);
  82. } catch(Exception ex) {
  83. ex.printStackTrace();
  84. }
  85. }
  86. //load data from database
  87. ObservableList<StudentCourse> selectStatementGrade() {
  88. Statement st = null;
  89. ResultSet rs = null;
  90. String query = "SELECT * FROM Registration.StudentCourse";
  91. ObservableList<StudentCourse> data = FXCollections.observableArrayList();
  92. try {
  93. st = con.createStatement();
  94. rs = st.executeQuery(query);
  95. while(rs.next()) {
  96. String StudentID = rs.getString("StudentID");
  97. String CourseID = rs.getString("CourseID");
  98. String Grade = rs.getString("Grade");
  99. data.add(new StudentCourse(StudentID, CourseID, Grade));
  100. }
  101. } catch(Exception ex) {
  102. ex.printStackTrace();
  103. }
  104. System.out.println(data.get(0).getStudentID());
  105. return data;
  106. }
  107. }
  108.  
  109. public class TableView extends Application{
  110.  
  111. public static void main(String[] args) {
  112. launch(args);
  113.  
  114. }
  115.  
  116. @Override
  117. public void start(Stage primaryStage) throws Exception {
  118. //some code //
  119.  
  120. Stage temp = new Stage();
  121. TableView<StudentCourse> tableView = new TableView<>();
  122. //constructs connection object
  123. DataConn dbc = new DataConn();
  124. ObservableList<StudentCourse> data = dbc.selectStatementGrade();
  125.  
  126. System.out.println(data.get(0).getCourseID());
  127. tableView.setItems(data);
  128.  
  129. TableColumn StudentIDColumn = new TableColumn("StudentID");
  130. StudentIDColumn.setMinWidth(100);
  131. StudentIDColumn.setCellValueFactory(
  132. new PropertyValueFactory<StudentCourse, String>("StudentID"));
  133.  
  134. TableColumn CourseIDColumn = new TableColumn("CourseID");
  135. CourseIDColumn.setMinWidth(100);
  136. CourseIDColumn.setCellValueFactory(
  137. new PropertyValueFactory<StudentCourse, String>("CourseID"));
  138.  
  139. TableColumn GradeColumn =
  140. new TableColumn("Grade");
  141. GradeColumn.setMinWidth(100);
  142. GradeColumn.setCellValueFactory(
  143. new PropertyValueFactory<StudentCourse, String>("Grade"));
  144.  
  145. tableView.getColumns().addAll(StudentIDColumn, CourseIDColumn,
  146. GradeColumn);
  147.  
  148. Pane paneTemp = new Pane();
  149. paneTemp.getChildren().add(tableView);
  150. Scene sceneTemp = new Scene(paneTemp, 300, 250);
  151. temp.setTitle("TableViewDemo"); // Set the window title
  152. temp.setScene(sceneTemp); // Place the scene in the window
  153. temp.show(); // Display the window
  154.  
  155. }
  156. }
Add Comment
Please, Sign In to add comment