Ch21 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

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
9
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
10
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
11
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
12
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.

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

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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()) {
}

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

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

A
  • execute(): returns boolean whether there is a ResultSet (true for read queries and false for write queries)
  • executeQuery(): returns ResultSet
  • executeUpdate(): returns number of affected rows
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
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

17
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.