Professional Documents
Culture Documents
JP 6 2 Practice Solution
JP 6 2 Practice Solution
JP 6 2 Practice Solution
Lesson Objectives:
• JDBC Data Types
Vocabulary:
ResultSetMetaData An object that can be used to get information about the types and properties of the
columns in a ResultSet Object
Your task is to develop the Java FX application that will show table contents from table names that begin with the word JOB. Use
“JOB%” in the Java FX TextArea.
For example, the application can display contents from the JOBS, JOB_HISTORY, or JOB_GRADES tables.
Notes: Use the DatabaseMetaData object.
Below is the JOBS entity diagram and an example of the finished interface.
1. The following is the basic structure of the program, complete the code blocks where instructed to do so:
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
package application;
@Override
public void start(Stage primaryStage) {
//establish the database connection
initializeDB();
//Add the returned table names to the comboBox, selecting the first item
//Create query that will select from the chosen table name
//Create a ResultSet object to hold the data from the executed query.
//Use the MetaData from the ResultSet to append the column names to the text
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
2
//area
//Use a while loop to display the values of the returned data to the text
//area
}catch (SQLException e) {
e.printStackTrace();
}//end try catch
}//end method showData
Suggested Solution
package application;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import oracle.jdbc.pool.OracleDataSource;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
@Override
public void start(Stage primaryStage) {
//establish the database connection
initializeDB();
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
3
BorderPane bpane = new BorderPane();
bpane.setCenter(new ScrollPane(ta));
bpane.setTop(hBox);
stmt = conn.createStatement();
while (resultSet.next()) {
for (int i = 1; i <= rs.getColumnCount(); i++)
ta.appendText(resultSet.getObject(i) + " ");
ta.appendText("\n");
}//endfor
//endwhile
}catch (SQLException e) {
e.printStackTrace();
}//end try catch
}//end method showData