JDBC (Java Database Connectivity) provides a standard API for connecting to and interacting with relational databases. The key aspects are:
1) JDBC standardizes the connection establishment and query execution process but not the actual SQL syntax.
2) There are three main types of statements - regular statements, prepared statements, and callable statements - that allow executing SQL queries and commands.
3) The basic steps to use JDBC are: load the driver, define the connection URL, establish a connection, create a statement, execute queries, process results, and close the connection.
4) Transactions allow grouping statements together to commit or rollback as a single unit of work.
4. 7 Steps Using JDBC
Load the
driver
Define the
Connection
URL
Establish
The
Connection
Create a
Statement
Object
Execute a
Query
Process
Results
Close the
Connection
1 2 3 4 5
7/26/2023 JDBC 4
6 7
9. Step 5 – Execute a Query
String query = “SELECT column1, column2, column3 FROM table”;
ResultSet rs = statement.executeQuery(query);
• Use statement.executeUpdate if you want to modify the database.
These modifying operations includes INSERT, UPDATE and
DELETE.
String query = “INSERT INTO table VALUES (value1, value2,
value3)”;
ResultSet rs = statement.executeUpdate(query);
7/26/2023 JDBC 9
10. Step 6 – Process the Result
String nama = “”;
String alamat = “”;
float nilai = 0.0;
int noUrut = 0;
while (rs.next()) {
nama = rs.getString(1); // the 1st column number is 1 not 0.
alamat = rs.getString(2);
nilai = rs.getFloat(3);
noUrut = rs.getInt(4);
}
7/26/2023 JDBC 10
11. Step 7 – Close the Connection
connection.close();
• Use this statement only if you are sure that there is
no more database operation will be proceed.
7/26/2023 JDBC 11
12. Statement
• SQL statements are sent to the database through
Statement object.
• Three types of statement objects :
• Statement - for executing a simple SQL statement
• Prepared Statement - for executing a pre-compiled
SQL statement that use parameters
• Callable Statement - for executing a database stored
procedure
7/26/2023 JDBC 12
13. Prepared Statement
• Execute the same SQL statement multiple times.
• Create statement in standard form that is sent to the database
for compilation before actually being used
• Mark the parameters using "?" and replace it using setXXX
methods when ready to manipulate them
• Prepared Statement execute these parameters without
arguments.
• execute
• executeQuery()
• executeUpdate()
7/26/2023 JDBC 13
15. Transaction
• Every time we execute an SQL statement, it is
automatically committed to database by default.
• Turn auto commit off to group two or more
statements together into a transaction
• connection.setAutoCommit(false);
• Call commit to permanently record the changes to
database after executing a group of statements
• Call rollback if an error occurs
7/26/2023 JDBC 15
17. Tugas
Buat sebuah program, menggunakan Java Swing, connect ke database
(boleh MySQL, boleh PostgreSQL), setidaknya memiliki fitur CRUD
- Create a single record
- Retrieve data from table
- Update a single record
- Delete a single record
7/26/2023 JDBC 17