Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Use the following code for the following changes: 1. change the combo box for courses into Listview with multiple select. 2. Register button will be
Use the following code for the following changes:
1. change the combo box for courses into Listview with multiple select.
2. Register button will be able to register more than one courses using Listview
3. You must use Batch statements to register all courses user selected in one execute.
PLEASE MAKE SURE THE CODE IS WORKING 100% AND SHOW THE OUTPUT
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import java.sql.*; public class Project9 extends Application { // Declare an array of Strings for Course Names private String connectionUrl = "jdbc:sqlserver://s16988308.onlinehome-server.com"; // Declare the JDBC objects. private Connection connection = null; private PreparedStatement statement = null; private ResultSet resultSet = null; private CallableStatement callableStatement = null; @Override // Override the start method in the Application class public void start(Stage primaryStage) { primaryStage.setTitle("Registration"); // Set the stage title // Create GridPane GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(25,25,25,25)); // Title Text scenetitle = new Text("Registration"); grid.add(scenetitle, 0, 0, 2, 1); // SSN TextField Label userName = new Label("StudentID:"); grid.add(userName, 0, 1); final TextField ssnTextField = new TextField(); grid.add(ssnTextField, 1, 1); //ssnTextField.setPrefWidth(200); ssnTextField.setMaxWidth(200); //ssnTextField.setMinWidth(100); // Course ComboBox Label pw = new Label("Course: "); grid.add(pw, 0, 2); final ComboBoxcourseCombo = new ComboBox<>(); grid.add(courseCombo, 1, 2); // Add Register and Exit buttons Button registerbtn = new Button("Register"); Button showbtn = new Button("Enrollment View"); Button exitBtn = new Button("Exit"); EXITHandlerClass exitHandler = new EXITHandlerClass(); exitBtn.setOnAction(exitHandler); HBox hbBtn = new HBox(10); hbBtn.setAlignment(Pos.BOTTOM_RIGHT); hbBtn.getChildren().addAll(showbtn,registerbtn,exitBtn); grid.add(hbBtn, 1, 4); // TextArea to display my registration HBox txtHbox = new HBox(10); txtHbox.setAlignment(Pos.BOTTOM_CENTER); final TextArea txtArea = new TextArea(); txtArea.setMinWidth(200); txtArea.setMinHeight(150); txtHbox.getChildren().add(txtArea); grid.add(txtHbox, 0, 5, 2, 1); Scene scene = new Scene(grid, 600, 500); primaryStage.setScene(scene); primaryStage.show(); primaryStage.setTitle("Status: Getting Courses from Database"); // Set the stage title // Establish the connection and get courses try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); connection = DriverManager.getConnection(connectionUrl); Statement statement = connection.createStatement(); // Execute a statement ResultSet resultSet = statement.executeQuery("select courseID from Course order by courseID;"); // Iterate through the result and print the student names ObservableList options = FXCollections.observableArrayList(); while (resultSet.next()) options.add(resultSet.getString(1)); // Close the connection resultSet.close(); courseCombo.getItems().addAll(options); courseCombo.setPrefWidth(200); primaryStage.setTitle("Status: Courses loaded from Database"); } catch (ClassNotFoundException | SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); primaryStage.setTitle("Status: Getting Courses from database failed."); } primaryStage.setTitle("Registration"); // Button Action settings registerbtn.setOnAction(new EventHandler () { @Override public void handle(ActionEvent e) { // register action txtArea.clear(); // Create a variable for the connection string. try { // Input SSN to variable String ssn = ssnTextField.getText(); // Input Course ID from Combobox String courseId = courseCombo.getValue().toString(); // Prepare call to stored procedure callableStatement = connection.prepareCall("{call insertStudent(?,?)}"); callableStatement.setString(1,ssn); callableStatement.setString(2,courseId); callableStatement.execute(); txtArea.appendText("Course added. "); } // Handle any errors that may have occurred. catch (Exception f) { // Error Handling here?? txtArea.appendText("Failed to add course "); f.printStackTrace(); } finally { if (resultSet != null) try { resultSet.close(); } catch(Exception f) {} if (statement != null) try { statement.close(); } catch(Exception f) {} // if (connection != null) try { connection.close(); } catch(Exception f) {} } } }); // Button Action settings showbtn.setOnAction(new EventHandler () { @Override public void handle(ActionEvent e) { // register action txtArea.clear(); // Create a variable for the connection string. try { // Input SSN to variable String ssn = ssnTextField.getText(); // Create and execute an SQL statement that returns some data. String SQL = "SELECT cs.courseID, cs.title, en.grade FROM students st, enrollment en, course cs " + "WHERE st.ssn = en.ssn AND en.courseId = cs.courseID " + "AND st.ssn = ?;"; statement = connection.prepareStatement(SQL); statement.setString(1,ssn); resultSet = statement.executeQuery(); if (resultSet.next() == false) txtArea.appendText("No result found. "); // Iterate through the data in the result set and display it. while (resultSet.next()) { txtArea.appendText(resultSet.getString("courseID") + "\t" + resultSet.getString("title") + "\t" + resultSet.getString("grade") + " "); } } // Handle any errors that may have occurred. catch (Exception f) { // Error Handling here?? f.printStackTrace(); } finally { if (resultSet != null) try { resultSet.close(); } catch(Exception f) {} if (statement != null) try { statement.close(); } catch(Exception f) {} // if (connection != null) try { connection.close(); } catch(Exception f) {} } } }); } class EXITHandlerClass implements EventHandler { @Override public void handle(ActionEvent e) { System.exit(1); } } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started