Describe JDBC
It is a simple API for connecting from Java applications
to multiple databases that lets one translate between
the database and the Java application.
What is a JDBC driver?
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.
What is JAR file
*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.
What are the steps to using JDBC
Describe the process of loading the JDBC drive
• 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.
Describe the process of establishing connection
• 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.
Describe the process of creating a statement object
• The JDBC Statement object sends SQL statements to
the database.
• Statement objects are created from active Connection objects.
• With a Statement object, one can issue SQL calls
directly to the database.
Describe the process of executing a query
ResultSet results =
statement.executeQuery(“SELECT a, b FROM table”);
Describe the process of processing the results and give useful methods during this instance
• 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
Describe the process of closing the connection
• To close the database connection, the following
methods are used:
• stmt.close();
• connection.close();