Object-Oriented Programming through Java · Strings, Threads, JDBC and JavaFX
JDBC and MySQL
In Object-Oriented Programming through Java because a booking that survives a restart lives in a database — JDBC drivers, connections and ResultSet, with a password placeholder,…
JDBC is Java's standard way to talk to a database. A driver translates Java calls into MySQL's protocol. You put Connector/J on the classpath, open a Connection with a URL, run a Statement, and walk a ResultSet. The password in every listing is the placeholder YOUR_PASSWORD — never a live credential. There is no in-browser lab: the runtime cannot host Connector/J against a real MySQL.
- Object-Oriented Programming through Java
- Easy level
- 6 concepts
1JDBC is Java, a driver, and a database
JDBC is an API in java.sql. Your code talks to interfaces: Connection, Statement, ResultSet. A driver implements those interfaces for one database product.
MySQL Connector/J is that driver for MySQL. The desk does not speak MySQL's wire protocol; the driver does.
Figure. The application uses JDBC types. The driver talks to MySQL. Swap the driver, not the desk code, to change products — in principle.
How it works
- JDBC is Java, a driver, and a databaseYour code uses java.sql. The driver is the MySQL-specific piece on the classpath.
2Put Connector/J on the classpath
Download MySQL Connector/J (a jar) and add it to the classpath the same way the packages lesson added a library. Modern Java loads the driver from that jar automatically.
The MySQL server must be running and must have a database, for example cinema. Creating that schema is SQL, not Java. This course does not ship a live password.
No diagram — the idea is carried by the prose or the Java listing.
| Piece | What you supply |
|---|---|
| Connector/J jar | on the classpath |
| MySQL server | running, with a cinema database |
| user / password | cinema_app / YOUR_PASSWORD |
3A Connection is a live session
DriverManager.getConnection(url, user, password) returns a Connection. The URL names the product, host, port and database: jdbc:mysql://localhost:3306/cinema
The password argument in listings is YOUR_PASSWORD. Read the real secret from the environment in any program that leaves your laptop.
Figure. DriverManager.getConnection reads jdbc:mysql://localhost:3306/cinema plus user cinema_app. The password in listings is YOUR_PASSWORD — a placeholder, not a secret to commit.
How it works
- A Connection is a live sessionThe URL picks host and database. YOUR_PASSWORD is a placeholder, not a secret to commit.
URL, user, placeholder password
import java.sql.Connection;
import java.sql.DriverManager;
String url =
"jdbc:mysql://localhost:3306/cinema";
String user = "cinema_app";
String password = "YOUR_PASSWORD";
Connection c = DriverManager.getConnection(
url, user, password);4A Statement runs SQL
connection.createStatement() gives a Statement. executeQuery runs SELECT and returns a ResultSet. executeUpdate runs INSERT, UPDATE or DELETE and returns how many rows changed.
A PreparedStatement with ? placeholders is the form that does not paste Meera's seat into the SQL string. Use it as soon as any part of the SQL comes from input.
Figure. PreparedStatement keeps ? in the SQL and binds A12 with setString. executeQuery then returns a ResultSet. Building SQL by joining input strings is the form this lesson refuses.
How it works
- A Statement runs SQLStatement runs SQL. PreparedStatement binds values. Do not build SQL by joining input strings.
? is bound, not concatenated
var ps = c.prepareStatement(
"SELECT seat, price FROM booking
WHERE seat = ?");
ps.setString(1, "A12");
var rs = ps.executeQuery();5ResultSet is a cursor over rows
A ResultSet starts before the first row. rs.next() moves to the next row and returns false when there are no more.
rs.getString("seat") and rs.getInt("price") read columns of the current row. Column names match the SELECT list (or aliases).
Figure. ResultSet starts before the first row. The first next() exposes A12 200, the second B7 200, the third returns false. getString("seat") and getInt("price") read the current row only.
next, then getters, until next is false
while (rs.next()) {
String seat = rs.getString("seat");
int price = rs.getInt("price");
System.out.println(seat + " " + price);
}Two booking rows
ResultSet has rows (A12, 200) then (B7, 200). What do the first two successful next() calls expose?
- before any next()no current row
- first next() trueseat A12, price 200
- second next() trueseat B7, price 200
- third next() falseloop ends
Pro tip. Forgetting next() and calling getString is an error, not the first row.
6Close ResultSet, Statement, Connection
Each JDBC object holds a server resource. Close them. try-with-resources can list Connection, then Statement, then ResultSet — or let the Connection close close the children if you opened them in the same try.
Leaving a Connection open is how a student lab exhausts the database's connection limit.
Figure. ResultSet, Statement and Connection each hold a server resource. Open them in try (...) so all three close on the way out. A leaked Connection is how a lab exhausts the database limit.
How it works
- Close ResultSet, Statement, ConnectionOpen JDBC objects in try (...). YOUR_PASSWORD stays a placeholder in the listing.
All three close on the way out
try (Connection c = DriverManager.getConnection(
url, user, "YOUR_PASSWORD");
var st = c.createStatement();
var rs = st.executeQuery(
"SELECT seat FROM booking")) {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}Notes
- In Object-Oriented Programming through Java because a booking that survives a restart lives in a database — JDBC drivers, connections and ResultSet, with a password placeholder, never a real secret.
- DriverManager.getConnection needs a jdbc:mysql URL, a user, and a password you do not commit. Statement runs SQL. ResultSet is a cursor over rows.
- Close ResultSet, Statement and Connection. try-with-resources does that in order.
Exam traps & shortcuts
- The password in course listings is YOUR_PASSWORD. A real password belongs in an environment variable or a local file that is not committed.
- SELECT * without a WHERE on a large table is a lab habit, not a desk habit.
Recap
JDBC is the API; Connector/J is the MySQL driver. Connection, Statement, ResultSet, then close. Password is YOUR_PASSWORD.
- driver on classpath
- java.sql is the API. The jar speaks MySQL.
- URL + user + placeholder
- jdbc:mysql://localhost:3306/cinema and YOUR_PASSWORD.
- next then get
- ResultSet starts before the first row.
- close
- try-with-resources. Do not leak connections.
Practise JDBC and MySQL
Reading is free and needs no account. Practice, mocks and progress live in the app.
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device