JDBC Flashcards

1
Q

Describe JDBC

A

It is a simple API for connecting from Java applications
to multiple databases that lets one translate between
the database and the Java application.

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

What is a JDBC driver?

A

To connect to a database, one needs a JDBC Driver.
• JDBC Driver is a set of classes that interfaces with a specific database engine.
• JDBC driver gives out the connection to the database and implements the protocol for transferring the query and result between client and database.

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

What is JAR file

A
*JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file
to distribute application software or libraries on the Java platform.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the steps to using JDBC

A
  1. Load the JDBC Driver
  2. Establish the Database Connection
  3. Create a Statement Object
  4. Execute a Query
  5. Process the Results
  6. Close the Connection
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe the process of loading the JDBC drive

A

• To use a JDBC driver, one must load the driver via the Class.forName() method.
• In general, the code syntax is as follows:
Class.forName(“jdbc.DriverXXX”);
jbdc.DriverXXX is the JDBC Driver to be loaded.

• Class.forName() will throw a
ClassNotFoundException if your CLASSPATH is not set up properly, hence, it’s always a good idea to surround the forName() with a try/catch
block.

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

Describe the process of establishing connection

A

• The following line of code illustrates the basic idea of connection establishing:

Connection con = DriverManager.getConnection(url, “myLogin”, “myPassword”);

• the URL has the following format:
jdbc:subprotocol:subname.
• JDBC indicates that this is a JDBC Connection
• The subprotocol identifies the driver you want to use.
• The subname identifies the database name/location.

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

Describe the process of creating a statement object

A

• The JDBC Statement object sends SQL statements to
the database.
• Statement objects are created from active Connection objects.

  • For example:
  • Statement stmt = con.createStatement();

• With a Statement object, one can issue SQL calls
directly to the database.

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

Describe the process of executing a query

A
  • Method executeQuery()
  • Executes the SQL query and returns the data in a table (ResultSet)
  • The resulting table may be empty but never null

ResultSet results =
statement.executeQuery(“SELECT a, b FROM table”);

  • Method executeUpdate()
  • Used to execute for INSERT, UPDATE, or DELETE SQL statements
  • The return is the number of rows that were affected in the database
  • It also Supports Data Definition Language (DDL) statements CREATE TABLE, DROP TABLE and ALTER TABLE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe the process of processing the results and give useful methods during this instance

A

• Results from the SQL query are contained in the
ResultSet.
• Useful Methods used during this instance include:
1. close
• Releases the JDBC and database resources

2.getMetaDataObject
• Returns a ResultSetMetaData object containing information
about the columns in the ResultSet

3.next
• Attempts to move to the next row in the ResultSet

4.findColumn
• Returns the corresponding integer value
corresponding to the specified column name

5.getXXX
• Returns the value from the column specified by
column name or column index as an XXX Java type

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

Describe the process of closing the connection

A

• To close the database connection, the following
methods are used:
• stmt.close();
• connection.close();

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