AlgoMaster Logo

JDBC - Quiz

Last Updated: December 6, 2025

1 min read

JDBC Exercises

31 quizzes

1
Code Completion

Obtain a JDBC connection using DriverManager so that you can perform database operations.

java
1
Connection connection = DriverManager.(url, user, password);

Click an option to fill the blank:

2
Code Completion

Prepare a reusable SQL query with a parameter placeholder to safely filter by username.

java
1
PreparedStatement ps = connection.("SELECT * FROM users WHERE username = ?");

Click an option to fill the blank:

3
Code Completion

Start managing multiple JDBC operations as a single transaction instead of auto-committing each statement.

java
1
connection.(false);

Click an option to fill the blank:

4
Multiple Choice

Which JDBC component is responsible for managing available database drivers and establishing connections?

5
Multiple Choice

Which JDBC driver type communicates directly with the database using a native protocol and requires no native libraries?

6
Multiple Choice

In a typical JDBC URL like "jdbc:mysql://localhost:3306/mydb", what does "mysql" represent?

7
Multiple Choice

Which method on Statement is typically used to execute an INSERT, UPDATE, or DELETE statement?

8
Multiple Choice

What is a key advantage of using PreparedStatement over Statement?

9
Multiple Choice

Which interface is specifically designed for calling stored procedures in JDBC?

10
Multiple Choice

Which ResultSet type allows moving both forward and backward but does NOT see changes committed after it was created?

11
Multiple Choice

Which property of transactions ensures that either all operations complete successfully or none are applied?

12
Multiple Choice

In JDBC, what happens immediately after calling connection.commit() when auto-commit is disabled?

13
Multiple Choice

What is a primary purpose of a JDBC connection pool in a web application?

14
Multiple Choice

Which method on ResultSet moves the cursor to the next row?

15
Sequencing

Order the steps to execute a SELECT query using a simple Statement and process the results.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Arrange the steps to perform a manual JDBC transaction that transfers money between two accounts.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this JDBC-related code that reads from a ResultSet?

1int count = 0;
2if (rs.next()) {
3    count = rs.getInt("user_count");
4}
5System.out.println("Users=" + count);
18
Output Prediction

Assume rowsUpdated is the return value of executeUpdate. What is the output?

1int rowsUpdated = 3;
2System.out.println("Updated:" + rowsUpdated);
19
Output Prediction

Predict the output when printing auto-commit mode before starting an explicit transaction.

1Connection conn = null; // assume a valid connection
2boolean auto = conn.getAutoCommit();
3System.out.println(auto);
20
Output Prediction

What is printed after configuring a read-only ResultSet concurrency mode?

1int type = ResultSet.TYPE_SCROLL_INSENSITIVE;
2int concur = ResultSet.CONCUR_READ_ONLY;
3System.out.println(concur == ResultSet.CONCUR_READ_ONLY);
21
Output Prediction

Assume poolSize is the initial number of connections created in a connection pool. What is the output?

1int initialConnections = 5;
2int borrowed = 2;
3int available = initialConnections - borrowed;
4System.out.println("Available=" + available);
22
Bug Spotting

This code is intended to execute a parameterized SELECT using PreparedStatement. Identify and fix the bug.

Click on the line(s) that contain the bug.

java
1
String sql = "SELECT * FROM users WHERE id = ?";
2
PreparedStatement ps = connection.prepareStatement(sql);
3
ps.setString(1, 10);
4
ResultSet rs = ps.executeQuery();
23
Bug Spotting

This code is meant to run two updates in a single transaction. Find and correct the transaction management bug.

Click on the line(s) that contain the bug.

java
1
Connection conn = DriverManager.getConnection(url, user, pass);
2
conn.setAutoCommit(true);
3
PreparedStatement debit = conn.prepareStatement("UPDATE accounts SET balance = balance - ? WHERE id = ?");
4
PreparedStatement credit = conn.prepareStatement("UPDATE accounts SET balance = balance + ? WHERE id = ?");
5
// set parameters here
6
debit.executeUpdate();
7
credit.executeUpdate();
8
conn.commit();
24
Matching

Match each JDBC interface with what it primarily represents.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each transaction or pooling concept to its role.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to create a scroll-insensitive, read-only ResultSet using a Statement.

java
1
Statement stmt = connection.createStatement(, );
2
ResultSet rs = stmt.executeQuery("SELECT * FROM employees");

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to configure and execute a stored procedure with one IN parameter and one OUT parameter.

java
1
CallableStatement cs = connection.prepareCall("{call get_user_name(?, ?)}");
2
cs.setInt(1, 42);
3
cs.(2, java.sql.Types.VARCHAR);
4
cs.();
5
String name = cs.getString(2);

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to safely close JDBC resources using try-with-resources around a query.

java
1
try (Connection conn = DriverManager.getConnection(url, user, pass);
2
PreparedStatement ps = conn.("SELECT id FROM users");
3
ResultSet rs = ps.()) {
4
if (rs.next()) {
5
System.out.println(rs.getInt(1));
6
}
7
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to borrow and return a Connection from a simple connection pool.

java
1
ConnectionPool pool = new ConnectionPool(5);
2
Connection conn = pool.();
3
// perform JDBC work here
4
pool.(conn);

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that will most likely cause a SQLException related to using Statement with user input.

Click on the line to select.

java
1
String userInput = "alice";
2
Statement stmt = connection.createStatement();
3
String sql = "SELECT * FROM users WHERE username = '" + userInput + "'";
4
ResultSet rs = stmt.executeQuery(sql);
31
Hotspot Selection

Click the line that most directly enables manual transaction control in this code.

Click on the line to select.

java
1
Connection conn = DriverManager.getConnection(url, user, pass);
2
conn.setAutoCommit(false);
3
PreparedStatement ps = conn.prepareStatement("UPDATE products SET stock = stock - ? WHERE id = ?");
4
// execute updates and commit/rollback later

Premium Content

Subscribe to unlock full access to this content and more premium articles.