Java Reference
In-Depth Information
F IGURE 32.22
You must include the driver file to run Java database programs.
The classpath directory and jar files are separated by commas. The period ( . ) represents
the current directory. For convenience, the driver files are placed under the c:\book\lib
directory.
Caution
Do not use a semicolon ( ; ) to end the Oracle SQL command in a Java program. The
semicolon may not work with the Oracle JDBC drivers. It does work, however, with the
other drivers used in this topic.
the semicolon issue
Note
The Connection interface handles transactions and specifies how they are processed.
By default, a new connection is in autocommit mode, and all its SQL statements are
executed and committed as individual transactions. The commit occurs when the state-
ment completes or the next execute occurs, whichever comes first. In the case of state-
ments returning a result set, the statement completes when the last row of the result set
has been retrieved or the result set has been closed. If a single statement returns multiple
results, the commit occurs when all the results have been retrieved. You can use the
setAutoCommit(false) method to disable autocommit, so that all SQL statements
are grouped into one transaction that is terminated by a call to either the commit() or
the rollback() method. The rollback() method undoes all the changes made
by the transaction.
auto commit
32.4.2 Accessing a Database from JavaFX
This section gives an example that demonstrates connecting to a database from a JavaFX pro-
gram. The program lets the user enter the SSN and the course ID to find a student's grade, as
shown in FigureĀ 32.23. The code in Listing 32.2 uses the MySQL database on the localhost.
F IGURE 32.23
A JavaFX client can access the database on the server.
L ISTING 32.2
FindGrade.java
1 import javafx.application.Application;
2 import javafx.scene.Scene;
3 import javafx.scene.control.Button;
4 import javafx.scene.control.Label;
5 import javafx.scene.control.TextField;
6 import javafx.scene.layout.HBox;
7 import javafx.scene.layout.VBox;
8 import javafx.stage.Stage;
9 import java.sql.*;
 
 
Search WWH ::




Custom Search