Files
CS1103/Labs/Lab9/src/StudentTranscriptGUI.java
2022-10-07 00:48:09 -03:00

160 lines
4.8 KiB
Java

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.sql.*;
public class StudentTranscriptGUI extends Application {
private static TextField studentIDField;
private static TableView<CourseGrade> tableView;
private static Text response;
public void start(Stage primaryStage) {
primaryStage.setTitle("Get a Transcript");
Label studentIDLabel = new Label("Student ID:");
tableView = new TableView<>();
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
TableColumn<CourseGrade, String> course = new TableColumn<>("Course");
TableColumn<CourseGrade, String> grade = new TableColumn<>("Grade");
tableView.getColumns().add(course);
tableView.getColumns().add(grade);
tableView.setPrefWidth(300);
//depreciated
course.impl_setReorderable(false);
grade.impl_setReorderable(false);
course.setCellValueFactory(new PropertyValueFactory<>("course"));
grade.setCellValueFactory(new PropertyValueFactory<>("grade"));
studentIDField = new TextField();
studentIDField.setPrefWidth(150);
studentIDField.setMaxWidth(300);
Button accept = new Button("Get Transcript");
accept.setOnAction(this::processAcceptStudent);
Button quit = new Button("Quit");
quit.setOnAction(this::quitApp);
response = new Text("");
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 10, 0, 10));
GridPane.setHalignment(quit, HPos.RIGHT);
// Row 1: Buttons
grid.add(accept, 0, 0);
grid.add(quit, 2, 0);
//Row 2: name
grid.add(studentIDLabel, 0, 1);
grid.add(studentIDField, 2, 1);
//Row 3: tableView
grid.add(tableView, 0, 2, 3, 1);
//Row 4: response
grid.add(response, 0, 3, 3, 4);
Scene scene = new Scene(grid, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public void processAcceptStudent(ActionEvent event) {
Connection connection = openConnection();
String studentID = studentIDField.getText();
tableView.getItems().clear();
try {
String query = "{CALL studentTranscript(?)}";
CallableStatement statement = connection.prepareCall(query);
statement.setString(1, studentID);
ResultSet rs = statement.executeQuery();
while(rs.next()) {
tableView.getItems().add(new CourseGrade(rs.getString(1), rs.getString(2)));
}
response.setText("Student transcript request completed.");
response.setFill(Color.GREEN);
} catch (SQLException sqlException) {
response.setFill(Color.RED);
response.setText(sqlException.getMessage());
}
closeConnection(connection);
}
@SuppressWarnings("SpellCheckingInspection")
private static Connection openConnection() {
final String url = "jdbc:mysql://cs1103.cs.unb.ca:3306/ishoebot";
final String user = "ishoebot";
final String password = "k7FOLH5B";
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
}
catch (Exception exception) {
response.setFill(Color.RED);
response.setText("Couldn't open a connection: " + exception.getMessage());
}
return conn;
}
private static void closeConnection(Connection conn) {
try {
conn.close();
}
catch (Exception exception) {
response.setFill(Color.RED);
response.setText("Couldn't close a connection: " + exception.getMessage());
}
}
public void quitApp(ActionEvent event) {
Platform.exit();
System.exit(0);
}
@SuppressWarnings("unused")
public static class CourseGrade {
private String course;
private String grade;
public CourseGrade(String course, String grade) {
this.course = course;
this.grade = grade;
}
public void setCourse(String course) {
this.course = course;
}
public String getCourse() {
return course;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
}