Chapter 21: JDBC Flashcards

1
Q

What are the five key interfaces of JDBC in the JDK?

A
  • Driver
  • Connection
  • PreparedStatement
  • CallableStatement
  • ResultSet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the Driver interface do?

A

Establishes a connection to the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the Connection interface do?

A

Sends commands to a database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the PreparedStatement interface do?

A

Executes a SQL query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the CallableStatement interface do?

A

Executes commands stored in the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the ResultSet interface do?

A

Reads results of a query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In what package are the five key interfaces of JDBC stored?

A

java.sql

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

(Assuming no specific server names were given)

Is the following JDBC url valid?

jdbc:postgresql;//localhost/zoo

A

It has a semicolon (;) instead of colons (:) so it is not valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

(Assuming no specific server names were given)

Is the following JDBC url valid?

jdbc:mysql://local/zoo

A

It uses local instead of localhost. Since no server name was given, local is invalid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

(Assuming no specific server names were given)

Is the following JDBC url valid?

jdbc:postgresql://localhost/zoo

A

This URL is valid. The format is split into three parts, seperated by a colon (:) -> jdbc:subprotocol:subname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

(Assuming no specific server names were given)

Is the following JDBC url valid?

jdbc:oracle://123456/zoo

A

According to the URL the location of the database is 123456. This is not an ip address/localhost or a domain name so it is not valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

(Assuming no specific server names were given)

Is the following JDBC url valid?

jdbc:mysql://localhost:3306/zoo

A

This URL is valid. The format is split into three parts, seperated by a colon (:) -> jdbc:subprotocol:subname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a main method that makes a connection with with ‘jdbc:derby:zoo’ using DriverManager without username and password.

A

import java.sql.*;

public static void main (String[] args) throws SQLException {
Drivermanager.getConnection(“jdbc:derby:zoo”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a main method that makes a connection with with ‘jdbc:derby:zoo’ using DriverManager with username: “user” and password: “test”.

A

import java.sql.*;

public static void main (String[] args) throws SQLException {
Drivermanager.getConnection(“jdbc:derby:zoo”, “user, “test);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Does the following code compile?

public static void main(String[] args) {
Connection conn = Drivermanager.getConnection(“jdbc:derby:zoo”, “user, “test);
PreparedStatement ps = conn.prepareStatement(“SELECT * FROM exhibits”);
}

A

Both getConnection() and prepareStatement() might throw an SQLException, which is checked. This is missing from the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Does the following code compile?

public static void main(String[] args) throws SQLException {
Connection conn = Drivermanager.getConnection(“jdbc:derby:zoo”, “user”, “test”);
PreparedStatement ps = conn.prepareStatement(“SELECT * FROM exhibits”);
}

A

This is valid.

17
Q

Does the following code compile?

public static void main(String[] args) throws SQLException {
Connection conn = Drivermanager.getConnection(“jdbc:derby:zoo”, “user”, “test”);
PreparedStatement ps = conn.prepareStatement();
}

A

Passing a SQL Statement when creating the PreparedStatement object is mandatory, so this will not compile.

18
Q

The prepareStatement() method of a Connection object implements the AutoClosable interface

A

True

19
Q

What is the result of the executeUpdate() method?

...
var insertSql = "INSERT INTO exhibits VALUES (10, 'Deer', 3)";
try (var ps = conn.prepareStatement(insertSql)) {
    var result = ps.executeUpdate(); //?
}
A

The result of the method executeUpdate() is the number of rows that were affected. In this case one record was inserted so the result is 1.

20
Q

Write the missing code to read the data using the query

...
var sql = "SELECT * FROM exhibits";
try (var ps = conn.prepareStatement(sql); ...) {
}
A
var sql = "SELECT * FROM exhibits";
try (var ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
}
21
Q

What are the three PreparedStatement methods to run a query or perform an update and what do they return?

A
  • execute(): returns boolean wether there is a ResultSet (true for read queries and false for write queries)
  • executeQuery(): returns ResultSet
  • executeUpdate(): returns number of affected rows
22
Q

What is the result of the code?

var sql = “SELECT * FROM exhibits”;

try (var conn = DriverManager.getConnection("jdbc:derby:zoo"); var ps = conn.prepareStatement(sql)) {
    var result = ps.executeUpdate();
}
A

This throws a SQLException because the method executeUpdate() can’t be used to with a READ query

23
Q

What is the result of the code?

var sql = “INSERT INTO exhibits VALUES (10, ‘Deer’, 3)”;

try (var conn = DriverManager.getConnection("jdbc:derby:zoo"); var ps = conn.prepareStatement(sql)) {
    var result = ps.executeQuery();
}
A

This throws a SQLException because the method executeQuery() can’t be used to with an update query

24
Q

What is the result of the code?

var sql = “SELECT * FROM exhibits”;

try (var conn = DriverManager.getConnection("jdbc:derby:zoo"); var ps = conn.prepareStatement(sql)) {
    var result = ps.executeQuery();
}
A

This is valid and it returns a ResultSet

25
Q

Why is it important to close database resources?

A

To avoid creating a resource leak that will eventually slow down your program.

26
Q

Given the following resources, in what order must they be closed?

  • PreparedStatement
  • Connection
  • ResultSet
A
  1. ResultSet
  2. PreparedStatement
  3. Connection
27
Q

What is wrong with the following code?

public void test() throws SQLException {
    var conn = DriverManager.getConnection(url);
    var ps = conn.prepareStatement(sql);
    var rs = ps.executeQuery();
    try (conn; ps; rs) {
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }
}
A

Suppose an exception is thrown before the try-with-resources block. In that case, we don’t benefit from automatic resource closing which means that the code will has a resource leak if it fails.

28
Q

To execute a stored SQL procedure, which JDBC interface should be used?

A. Statement Interface
B. PreparedStatement Interface
C. PrePreparedStatement Interface
D. CallableStatement Interface

A

D. CallableStatement Interface