Java Reference
In-Depth Information
10
11
public class
FindGrade
extends
Application {
12
// Statement for executing queries
13
private
Statement stmt;
14
private
TextField tfSSN =
new
TextField();
15
private
TextField tfCourseId =
new
TextField();
16
private
Label lblStatus =
new
Label();
17
18 @Override
// Override the start method in the Application class
19
public void
start(Stage primaryStage) {
20
// Initialize database connection and create a Statement object
21 initializeDB();
22
23 Button btShowGrade =
new
Button(
"Show Grade"
);
24 HBox hBox =
new
HBox(
5
);
25 hBox.getChildren().addAll(
new
Label(
"SSN"
), tfSSN,
26
new
Label(
"Course ID"
), tfCourseId, (btShowGrade));
27
28 VBox vBox =
new
VBox(
10
);
29 vBox.getChildren().addAll(hBox, lblStatus);
30
31 tfSSN.setPrefColumnCount(
6
);
32 tfCourseId.setPrefColumnCount(
6
);
33 btShowGrade.setOnAction(e -> showGrade());
34
35
// Create a scene and place it in the stage
36 Scene scene =
new
Scene(vBox,
420
,
80
);
37 primaryStage.setTitle(
"FindGrade"
);
// Set the stage title
38 primaryStage.setScene(scene);
// Place the scene in the stage
39 primaryStage.show();
// Display the stage
40 }
41
42
private void
initializeDB() {
43
try
{
44
// Load the JDBC driver
45 Class.forName(
"com.mysql.jdbc.Driver"
);
46
// Class.forName("oracle.jdbc.driver.OracleDriver");
47 System.out.println(
"Driver loaded"
);
48
49
// Establish a connection
50 Connection connection = DriverManager.getConnection
51 (
"jdbc:mysql://localhost/javabook"
,
"scott"
,
"tiger"
);
52
// ("jdbc:oracle:thin:@liang.armstrong.edu:1521:orcl",
53
// "scott", "tiger");
54 System.out.println(
"Database connected"
);
55
56
button listener
load driver
Oracle driver commented
connect to MySQL database
connect to Oracle commented
// Create a statement
57
stmt = connection.createStatement();
create statement
58 }
59
catch
(Exception ex) {
60 ex.printStackTrace();
61 }
62 }
63
64
private void
showGrade() {
65 String ssn = tfSSN.getText();
66 String courseId = tfCourseId.getText();
67
try
{
68 String queryString =
"select firstName, mi, "
+
69
show result
"lastName, title, grade from Student, Enrollment, Course "
+
execute statement
Search WWH ::
Custom Search