Question
JAVA ASSIGNMENT 1. This is a walk-through lab. Your job is to implement a database application using JDBC. You need to follow the code below
JAVA ASSIGNMENT
1. This is a walk-through lab. Your job is to implement a database application using JDBC. You need to follow the code below and debug your code as problems arise. In order to run the application, you need create a database table named Student. Use your Oracle account provided to you during the class hour to create the table Student. Please note that the application uses prepared statements. All the concepts used were explained during the lecture time. The Student table should have the following fields: sin, firstName, mi, lastName, and grade. Please insert at least 10 realistic data into the table. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.cotrol.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.sql.*; public class FindGradeWithPreparedStatement extends Application { private PreparedStatement preparedStatement; private TextField tfSIN = new TextField(); private Label lblStatus = new Label(); @Override public void start(Stage primaryStage) { //initialize database connection and create a statement object initializeDB(); Button btShowGrade = new Button("Show Grade"); HBox hBox = new HBox(5); hBox.getChildren().addAll(new Label("SIN"), tfSIN, (btShowGrade)); VBox vBox = new VBox(10); vBox.getChildren().addAll(hBox, lblStatus); tfSIN.setPrefColumnCount(6); //Register handler btShowGrade.setOnAction(e -> showGrade()); //routine for UI Scene scene = new Scene(vBox, 420, 80); primaryStage.setTitle("FindGrade"); primaryStage.setScene(scene); primaryStage.show(); } private void initializeDB() { try { //load jdbc driver Class.forName("oracle.jdbc.driver.OracleDriver"); //for sqlserver //Class.forName(com.mysql.jdbc.Driver"); System.out.println("Driver loaded."); //establish a connection Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@oracle1.centennialcollege.ca:1528:SQLD",user, password); System.out.println("Database connected."); String queryString = "select firstName, mi, lastName, grade from Student where sin = ?"; //create a statement preparedStatement = connection.prepareStatement(queryString); } catch(Exception ex) { ex.printStackTrace(); } } private void showGrade() { String sin = tfSIN.getText(); try { preparedStatement.setString(1, sin); ResultSet rset = preparedStatement.executeQuery(); if(rset.next()) { String lastName = rset.getNString(1); String mi = rset.getString(2); String firstName = rset.getString(3); String grade = rset.getString(4); //display result in a label lblStatus.setText(firstName + " " + mi + " " + lastName + "'s grade is " + grade); } else { lblStatus.setText("Not found."); } } catch(SQLException ex) { ex.printStackTrace(); } } private static void main(String[] args) { Application.launch(); } }
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