-
There are 4 kkey interfaces in JDBC which are implemented in JDBC driver
-
Different vendors of databases provided different implementations. E.g. the jar for
PostgreSQL
ispostgresql-9.4-1201.jdbc4.jar
-
Here are interfaces of JDBC:
Driver
- knows how to get a connection to DBConnection
- knows how to communicatte with DBStatement
- knows how to run SQLResultSet
- knows whhat is returned from select statement
-
The implementing classes of these interfaces all have the name of
vendor name + name of interface
, e.g.FooDriver
-
Here is an example of using JDBC akin to what you would see in the exam:
public static void main(String[] args) throws SQLException {
String url = "jdbc:derby:zoo";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select name from animal")) {
while(rs.next()) {
System.out.println(rs.getString(1));
}
}
}
- This program will print the following:
Elsa
Zelda
Ester
Eddie
Zoe
- If the URL was using
FooDriver
, theDriverManager
would return an instance ofFooConnection
- The
conn.createStatement()
returns an instance ofFooStatement
- The
stmt.executeQuery()
returns an instance ofFooResultSet