Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TableColumn StudentIDColumn = new TableColumn("StudentID");
- StudentIDColumn.setMinWidth(100);
- StudentIDColumn.setCellValueFactory(
- new PropertyValueFactory<StudentCourse, String>("StudentID"));
- TableColumn CourseIDColumn = new TableColumn("CourseID");
- CourseIDColumn.setMinWidth(100);
- CourseIDColumn.setCellValueFactory(
- new PropertyValueFactory<StudentCourse, String>("CourseID"));
- TableColumn GradeColumn =
- new TableColumn("Grade");
- GradeColumn.setMinWidth(100);
- GradeColumn.setCellValueFactory(
- new PropertyValueFactory<StudentCourse, String>("Grade"));
- tableView.getColumns().addAll(StudentIDColumn, CourseIDColumn,
- GradeColumn);
- import javafx.application.Application;
- import javafx.beans.property.SimpleBooleanProperty;
- import javafx.beans.property.SimpleDoubleProperty;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.scene.Scene;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.scene.layout.Pane;
- import javafx.stage.Stage;
- import java.sql.*;
- class StudentCourse {
- final SimpleStringProperty StudentID;
- final SimpleStringProperty CourseID;
- final SimpleStringProperty Grade;
- StudentCourse(String StudentID, String CourseID,
- String Grade) {
- this.StudentID = new SimpleStringProperty(StudentID);
- this.CourseID = new SimpleStringProperty(CourseID);
- this.Grade = new SimpleStringProperty(Grade);
- }
- String getStudentID() {
- return StudentID.get();
- }
- void setStudentID(String StudentID) {
- this.StudentID.set(StudentID);
- }
- String getCourseID() {
- return CourseID.get();
- }
- void setCourseID(String CourseID) {
- this.CourseID.set(CourseID);
- }
- String getGrade() {
- return Grade.get();
- }
- void setGrade(String Grade) {
- this.Grade.set(Grade);
- }
- }
- class DataConn {
- //Interface to connect to the database
- private Connection con = null;
- String url = "jdbc:mysql://localhost:3306/Registration?useSSL=false";
- String uname = "root";
- String pass1 = "Root";
- public DataConn() {
- try {
- //step2: load and register the driver
- Class.forName("com.mysql.cj.jdbc.Driver");
- //step 3: establish connection
- con = DriverManager.getConnection(url, uname, pass1);
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- //load data from database
- ObservableList<StudentCourse> selectStatementGrade() {
- Statement st = null;
- ResultSet rs = null;
- String query = "SELECT * FROM Registration.StudentCourse";
- ObservableList<StudentCourse> data = FXCollections.observableArrayList();
- try {
- st = con.createStatement();
- rs = st.executeQuery(query);
- while(rs.next()) {
- String StudentID = rs.getString("StudentID");
- String CourseID = rs.getString("CourseID");
- String Grade = rs.getString("Grade");
- data.add(new StudentCourse(StudentID, CourseID, Grade));
- }
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- System.out.println(data.get(0).getStudentID());
- return data;
- }
- }
- public class TableView extends Application{
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) throws Exception {
- //some code //
- Stage temp = new Stage();
- TableView<StudentCourse> tableView = new TableView<>();
- //constructs connection object
- DataConn dbc = new DataConn();
- ObservableList<StudentCourse> data = dbc.selectStatementGrade();
- System.out.println(data.get(0).getCourseID());
- tableView.setItems(data);
- TableColumn StudentIDColumn = new TableColumn("StudentID");
- StudentIDColumn.setMinWidth(100);
- StudentIDColumn.setCellValueFactory(
- new PropertyValueFactory<StudentCourse, String>("StudentID"));
- TableColumn CourseIDColumn = new TableColumn("CourseID");
- CourseIDColumn.setMinWidth(100);
- CourseIDColumn.setCellValueFactory(
- new PropertyValueFactory<StudentCourse, String>("CourseID"));
- TableColumn GradeColumn =
- new TableColumn("Grade");
- GradeColumn.setMinWidth(100);
- GradeColumn.setCellValueFactory(
- new PropertyValueFactory<StudentCourse, String>("Grade"));
- tableView.getColumns().addAll(StudentIDColumn, CourseIDColumn,
- GradeColumn);
- Pane paneTemp = new Pane();
- paneTemp.getChildren().add(tableView);
- Scene sceneTemp = new Scene(paneTemp, 300, 250);
- temp.setTitle("TableViewDemo"); // Set the window title
- temp.setScene(sceneTemp); // Place the scene in the window
- temp.show(); // Display the window
- }
- }
Add Comment
Please, Sign In to add comment