Ch21 JDBC Flashcards
What are the five key interfaces of JDBC in the JDK?
- Driver
- Connection
- PreparedStatement
- CallableStatement
- ResultSet
What does the Driver interface do?
Establishes a connection to the database
What does the Connection interface do?
Sends commands to a database
What does the PreparedStatement interface do?
Executes a SQL query
What does the CallableStatement interface do?
Executes commands stored in the database
What does the ResultSet interface do?
Reads results of a query
In what package are the five key interfaces of JDBC stored?
java.sql
(Assuming no specific server names were given)
Is the following JDBC url valid?
jdbc:postgresql://localhost/zoo
This URL is valid. The format is split into three parts, seperated by a colon (:) -> jdbc:subprotocol:subname
Write a main method that makes a connection with with ‘jdbc:derby:zoo’ using DriverManager without username and password.
import java.sql.*;
…
public static void main (String[] args) throws SQLException {
Drivermanager.getConnection(“jdbc:derby:zoo”);
}
Write a main method that makes a connection with with ‘jdbc:derby:zoo’ using DriverManager with username: “user” and password: “test”.
import java.sql.*;
…
public static void main (String[] args) throws SQLException {
Drivermanager.getConnection(“jdbc:derby:zoo”, “user, “test);
}
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”);
}
Both getConnection() and prepareStatement() might throw an SQLException, which is checked. This is missing from the code.
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();
}
Passing a SQL Statement when creating the PreparedStatement object is mandatory, so this will not compile.
The prepareStatement() method of a Connection object implements the AutoClosable interface
True
Write the missing code to read the data using the query
…
var sql = “SELECT * FROM exhibits”;
try (var ps = conn.prepareStatement(sql); …) {
}
var sql = “SELECT * FROM exhibits”;
try (var ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
}
What are the three PreparedStatement methods to run a query or perform an update and what do they return?
- 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
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();
}
This throws a SQLException because the method executeUpdate() can’t be used to with a READ query
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));
}
}
}
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.