12. JDBC Flashcards
What are the 3 important steps when working with a database?
- Creating a connection to the database
- Creating a statement to execute in the database
- Getting back a set of data that represents the result
What is the driving force behind JDBC?
To provide a standard way to acces relational databases, but JDBC can also be used to acces file systems and OO data sources
How is a JDBC driver provided?
A JDBC driver is typically provided by the vendor in a JAR or ZIP file.
What are the requirements for a JDBC driver?
- Fully implement the interfaces: java.sql.Driver, java.sql.DatabaseMetaData, java.sql.ResultSetMetaData
- Implement the java.sql.Connection interface
- Implement java.sql.Statement and java.sql.PreparedStatement
- Implement the java.sql.CallableStatement interface if the database supports stored procedure
- Implement the java.sql.ResultSet interface
What does the DriverManager class do?
This concrete class is used to interact with a JDBC driver and return instances of Connection objects
Which method does the DriverManager class have?
getConnection() method
What does the DriverManager.getConnection() method do?
When you invoke the DriverManager’s getConnection(), you are asking the DriverManager to try passing the first String in the statement, the driver url, along with the username and password to each of the driver classes registered with the DriverManager in turn. If one of the driver classes
recognizes the URL string and the username and password are accepted, the driver returns an instance of a Connection object. If however, the URL is incorrect, or username or password or if there are no registered drivers, the method will throw an SQLException
What does a JDBC URL look like?
jdbc:derby://localhost:1521/BookSellerDB
The first part (jdbc) simply identifies that this is a JDBC URL (versus HTTP or something else). The second part (derby) indicates that driver vendor is derby driver. The third part indicates that the database is on the localhost of this machine at port 1521. And the final part indicates that we are interested in the BookSellerDB database.
What to do with JDBC < version 4?
If you use a JDBC driver that is an earlier version, then you must explicitly load the class provided by the database vendor that implements the java.sql.Driver interface. For example: Class.forName(“org.apache.derby.jdbc.ClientDriver”);
Wich of the JDBC API method invocations throw SQLExceptions?
All of the JDBC API method invocations throw SQLException. A SQLException can be thrown when a method is used improperly of if the database is no longer responding.
How can we create a Statement Object?
We can create an instance of a statement object from a connection object.
What does a Statement object do?
The primary purpose of a Statement is to execute a SQL statement using a method and return
some type of result.
public ResultSet executeQuery(String sql) throws SQLException
this most common method is used when we know that we want to return result – we are querying the database for one of more rows of data. This method must be called in a try-catch block or must be called in a method that also throws SQLException. One reason that these methods all throw SQLException is that a connection to the database is likely to a database on a network.
public int executeUpdate(String sql) throws SQLException
This method is used for SQL operation that affects one or more rows and does not return results (vb. SQL INSERT, UPDATE, DELETE and DDL queries). These statements do not return results, but do return a count of the number of rows affected by the SQL query. Note that this Statement method can also be used to execute SQL queries that do not return a row count, such as CREATE TABLE or DROP TABLE. For DDL queries the return value is 0.
public boolean execute(String sql) throws SQLException
This method is used when you are not sure what the result will be – perhaps the query will return a result set and perhaps not. This method can be used to excute a query whose type may not be known until runtime. The return value is true if the query resulted in a result set and false if the query resulted in an update count or no result.