click below
click below
Normal Size Small Size show me how
OCPJP709.2
Cert Obj Connect to a Database Using DriverManager
[9.2.1]Identify the components required to connect to a database using the DriverManager class (including the JDBC URL) | jdbc:mysql://localhost:3306/ (protocol, subprotocol, server address, port) |
[9.2.2] DriverManager | concrete class with static methods // which JDBC driver |
[9.2.3]How to get a Connection? | Connection conn = DriverManager.getConnection(url, user, pwd); |
[9.2.4] No suitable driver exception | Put full path of the jar file of your JDBC driver to avoid getting the exception. In fact, entering only the path of the jar is not enough; you need to add the jar name along with the full path to the classpath variable. |
[9.2.5] How to load Driver? Class.forName()? File META-INF/services/java.sql.Driver with name of JDBC drivers implementation of java.sql.Driver is required why? | Applications > 4.0 don't need Class.forName(). DM methods getConnection, getDrivers enhanced to support JSE Service Provider mechanism. Hence JDBC 4.0 Drivers must include file |
[9.2.6] DriverManager finds Driver from those loaded at initialization + loaded explicitly using same classloader as current applet/appln | To load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry: my.sql.Driver When method getConnection is called, DriverManager attempts to locate a suitable driver. |
[9.2.7] try with resources (The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. ) | The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource. |
[9.2.8] Resource once closed cannot be accessed | 1. Once a Connection object is closed, you cannot access any of the subsequent objects such as Statement and ResultSet that are retrieved from that Connection. |
[9.2.9] Scope of resources only try not even catch | 2. The references declared in the try block (in this case, c and ResultSet) are not visible outside the try block. Not even in the catch block. |
[9.2.10] closed whether exception or not | 3. When a resource is created in the try-with-resources block ( in this case, c), it is closed at the end of the try block irrespective of whether there is an exception in the try block or not. |
[9.2.11] Callable statement easier to build and call from JDBC code | This is true because you don't have to write any SQL query in Java code. You just use the name of the stored procedure. The queries are already there inside the stored procedure, which exists in the Database and not in JDBC code. |
[9.2.12] Multiple sub-interfaces of RowSet interface? | RowSet an interface adds support to JDBC API for JavaBeansTM component model. 5 more subinterfaces : JdbcRowSet, CachedRowSet, WebRowSet, JoinRowSet, FilteredRowSet + implementation classes. |
[9.2.13] How RowSet used | A Rowset, which can be used as a JavaBeans component in a visual Bean development environment, can be created and configured at design time and executed at run time. |
[9.2.14] RowSet provides | RowSet interface provides a set of JavaBeans properties that allow a RowSet instance to be configured to connect to a JDBC data source and read some data from the data source. |
[9.2.15] RowSet setters | A group of setter methods (setInt, setBytes, setString, and so on) provide a way to pass input parameters to a rowset's command property. This command is the SQL query the rowset uses when it gets its data from a relational database, |
[9.2.16] RowSet support for events | The RowSet interface supports JavaBeans events, allowing other components in an application to be notified when an event occurs on a rowset, such as a change in its value. |
[9.2.17] Unique how? The RowSet interface is unique in that it is intended to be implemented using the rest of the JDBC API. | RowSet implementation is a layer of software that executes "on top" of a JDBC driver. RowSet interface impl can be provided by anyone, including JDBC driver vendors who want to provide a RowSet implementation as part of their JDBC products. |
[9.2.18] A connected Rowset. | RowSet object may make a connection with a data source and maintain that connection throughout its life cycle |
[9.2.19] A Rowset may also make a connection with a data source, get data from it, and then close the connection. Such a rowset is called a disconnected rowset. | A disconnected Rowset may make changes to its data while it is disconnected and then send the changes back to the original source of the data, but it must reestablish a connection to do so. |
[9.2.20] A disconnected rowset may have a reader (a RowSetReader object) associated with it. | The reader may be implemented in many different ways to populate a rowset with data, including getting data from a non-relational data source. |
[9.2.21] A disconnected rowset may have a writer (a RowSetWriter object) associated with it. | The writer can also be implemented in many different ways to propagate changes made to the rowset's data back to the underlying data source. |
[9.2.22] The RowSet interface extends the standard java.sql.ResultSet interface. The RowSetMetaData interface extends the java.sql.ResultSetMetaData interface. | Thus, developers familiar with the JDBC API will have to learn a minimal number of new APIs to use rowsets. In addition, third-party software tools that work with JDBC ResultSet objects will also easily be made to work with rowsets. |
[9.2.23] ResultSetMetaData rsmd = resultSet.getMetaData(); | rsmd.getColumnName(x) // x should be 1 or more! |
[9.2.24] Basic code structure for use RowSets | RowSetFactory rsf = RowSetProvider.newFactory); CachedRowSet studentRS = rsf.createCachedRowSet(); studentRS.setCommand("select SID, NAME from STUDENT2"); studentRS.execute(c); //at this point studentRS contains the data returned by the query. |