Chapter 21: 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
It has a semicolon (;) instead of colons (:) so it is not valid
(Assuming no specific server names were given)
Is the following JDBC url valid?
jdbc:mysql://local/zoo
It uses local instead of localhost. Since no server name was given, local is invalid.
(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
(Assuming no specific server names were given)
Is the following JDBC url valid?
jdbc:oracle://123456/zoo
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.
(Assuming no specific server names were given)
Is the following JDBC url valid?
jdbc:mysql://localhost:3306/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.