12. JDBC Flashcards

1
Q

What are the 3 important steps when working with a database?

A
  1. Creating a connection to the database
  2. Creating a statement to execute in the database
  3. Getting back a set of data that represents the result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the driving force behind JDBC?

A

To provide a standard way to acces relational databases, but JDBC can also be used to acces file systems and OO data sources

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

How is a JDBC driver provided?

A

A JDBC driver is typically provided by the vendor in a JAR or ZIP file.

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

What are the requirements for a JDBC driver?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the DriverManager class do?

A
This concrete class is used to interact with a JDBC driver and return instances
of Connection objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which method does the DriverManager class have?

A

getConnection() method

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

What does the DriverManager.getConnection() method do?

A

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

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

What does a JDBC URL look like?

A

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.

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

What to do with JDBC < version 4?

A
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”);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Wich of the JDBC API method invocations throw SQLExceptions?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we create a Statement Object?

A

We can create an instance of a statement object from a connection object.

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

What does a Statement object do?

A

The primary purpose of a Statement is to execute a SQL statement using a method and return
some type of result.

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

public ResultSet executeQuery(String sql) throws SQLException

A

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.

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

public int executeUpdate(String sql) throws SQLException

A

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.

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

public boolean execute(String sql) throws SQLException

A

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.

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

What if the execute method returns true / false?

A

The getResultSet() is used to retrieve results when the execute() method return true (if there are no results you will get null), and the getUpdateCount() is used to retrieve the count when the execute() method returns false (if there are no result you will get -1).

17
Q

What is a ResultSet?

A
When a query returns a result set, an instance of a class that implements the ResulSet interface is returned. The ResultSet object represent the result of the query – all of the data in each row on a per column basis. Using the methods defined in the ResultSet interface, you can read and manipulate
data.
18
Q

What does the ResultSet.next() method do?

A

The next() method moves the cursor forward one row and returns true if the cursor now points to a row of data in the ResultSet. If the cursor is beyond the last row of data or contains no rows, its return value is 0.

19
Q

With which number starts a column index?

A

1

20
Q

How to get information about the returned results?

A

Using ResultSetMetaData

21
Q

ResultSetMetaData.getColumnCount()

A

is the most used ResultSetMetaData method, it returns the
integer count of the number of columns returned by the query. With this method you can iterate through the columns to get information about each column.

22
Q

ResultSetMetaData.getColumnName(int column)

A

returns the string name of this column.

23
Q

ResultSetMetaData.getTableName(int column)

A

returns the String name of the table that this column belongs to. This
method is useful when the query is a join of two or more tables and we need to know which table a column came from.

24
Q

ResultSetMetaData.getColumnDisplaySize(int column)

A

method returns an integer of the size of the column. This
information is useful for determining the maximum number of characters a column can hold, and the spacing that is required between columns for a report.

25
Q

What does the try with resources do with Connections, Statements and ResultSets?

A

The try-with resources statement will automatically call the close() method on any resource declared in the parantheses at the end of the try block.

26
Q

How are the resources closed?

A

when more than one resource is declared in the try-with0resources statement, the
resources are closed in the reverse order of their declaration.

27
Q

What happens with exceptions thrown when closing resources?

A

Any exception thrown as a result of closing resources at the end of the try block are sppressed, if there was also an exception thrown in the try block. These exceptions can be retrieved from the exception thrown by calling the getSuppressed() method on the exception thrown.