Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9...
-
Upload
sharlene-golden -
Category
Documents
-
view
212 -
download
0
Transcript of Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9...
![Page 1: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/1.jpg)
Java 7 CertificationJDBC & Related Design Pattern
www.netcomlearning.com
NetCom’s Average Instructor Rating: 8.7/9
Arthur GoberSubject Matter Expert
www.NetComLearning.com
![Page 2: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/2.jpg)
www.NetComLearning.com
JDBC
• JDBC API provides a standard database – independent interface to interact with RDMSs.
• Typically, you use the JDBC API to connect to a database, query the data, and/or update data.
• Every database provides developers with 1. Standard SQL capabilities2. Extended SQL capabilities3. A proprietary programming languageto build database applications.
![Page 3: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/3.jpg)
www.NetComLearning.com
JDBC
• Therefore, if you want to process a set of rows in a database, you need to know the specific syntax for a specific database-dependent language.
• Using JDBC API relieves you of the effort to learn specific syntaxes for different databases.
• Using JDBC API you write a query using standard SQL and the Java API processes the result in a database-independent manner.
![Page 4: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/4.jpg)
www.NetComLearning.com
JDBC
• Using JDBC API to access data in a database hides the implementation differences that exist in different databases.
• It achieves the database transparency by defining most of its API using interfaces and letting the database vendors provide the implementations for those interfaces.
• The collection of implementation classes that is supplied by a vendor to interact with a specific database is called a ‘JDBC driver’.
• The ‘JDBC driver’ is used to connect to the RDMS.
![Page 5: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/5.jpg)
www.NetComLearning.com
JDBC
To connect to a database:1. Obtain the JDBC driver class files and add them
to the CLASSPATH environment variable.2. Register the JDBC driver with the
‘DriverManager’.3. Construct a connection URL.4. Use the static ‘getConnection()’ method of
‘DriverManager’ to establish a connection.
![Page 6: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/6.jpg)
www.NetComLearning.com
JDBC
• We will be working with the ‘APACHE DERBY’ database system which is included as part of the Java JDK.
• We will be including three jar files in our CLASSPATH.
![Page 7: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/7.jpg)
www.NetComLearning.com
JDBC
1. First set an environment variable called DERBY_HOME to the following:
C:\program files\java\jdk1.7.0_03\db\
2. Add to the PATH: %DERBY_HOME%\bin3. Add to the CLASSPATH:
%DERBY_HOME%\LIB\DERBY.JAR; %DERBY_HOME%\LIB\DERBYTOOLS.JAR;%DERBY_HOME%\LIB\DERBYRUN.JAR;
![Page 8: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/8.jpg)
www.NetComLearning.com
JDBC
• To connect to our APACHE DERBY database:// Register the JDBC driver classString driver="org.apache.derby.jdbc.EmbeddedDriver";Class.forName(driver); // set up the database name String dbName="jdbcDemoDB";// define the Derby connection URL to use String connectionURL = "jdbc:derby:" + dbName + ";create=true";// establish the connection to the database.conn = DriverManager.getConnection(connectionURL);
![Page 9: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/9.jpg)
www.NetComLearning.com
JDBC
JDBC connection project exercise.
![Page 10: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/10.jpg)
www.NetComLearning.com
JDBC
• You can execute different types of SQL statements using a JDBC driver.
• We use different JDBC ‘statement’ objects depending upon the kind of SQL statement.
• An instance of ‘java.sql.Statement’ interface represents a SQL statement in a java program.
• three interfaces to represent SQL statements:1. Statement2. Prepared Statement3. Callable Statement
![Page 11: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/11.jpg)
www.NetComLearning.com
JDBC
• If you have a SQL statement in the form of a string, you can use a ‘Statement’ object to execute it. These SQL statements are compiled each time they are executed.
• If you want to pre-compile a SQL statement once and execute it multiple times, use a ‘PreparedStatement’ object. It lets you specify a SQL statement in the form of a string that uses placeholders. You supply the values of the placeholders before executing the statement.
• Use a ‘CallableStatement’ object for a stored procedure or function in a database.
![Page 12: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/12.jpg)
www.NetComLearning.com
JDBC
• When you execute a SQL statement, the DBMS may return zero or more results. The results may include ‘update counts’, for example, the number of records affected in the database, or the results may include what are known as ‘result sets’, which consists of a group of records that have been retrieved from the database.
![Page 13: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/13.jpg)
www.NetComLearning.com
JDBC
• To execute a SQL statement using a ‘Statement’ object:
1. Get a connection object.Connection conn = … (get a Connection object)
2. Use the connection object to create a ‘Statement’ object:Statement stmt = conn.createStatement();
![Page 14: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/14.jpg)
www.NetComLearning.com
JDBC
3. Set up your SQL in a string:String sql = “update person set income=income*1.1”;
4. Execute the statement by calling one of the ‘execute’ methods of the ‘Statement’ object:
int rowsUpdated = stmt.executeUpdate(sql);
5. Close the ‘Statement’ object to release resources:stmt.close();
6. Commit the transaction to the database:conn.commit();
![Page 15: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/15.jpg)
www.NetComLearning.com
JDBC
• ‘Statement’ interface ‘execute()’ method is used to execute a SQL statement which does not return a value, such as ‘CREATE TABLE’.
• ‘executeUpdate()’ method is used for SQL that updates a database, as in ‘INSERT’, ‘UPDATE’ and ‘DELETE’ SQL statements. It returns the number of rows affected.
• ‘executeQuery()’ is used for SQL that produces a resultset, as in ‘SELECT’ SQL statements.
![Page 16: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/16.jpg)
www.NetComLearning.com
JDBC
• When you connect to a database the ‘auto-commit’ property for the ‘Connection’ object is set to ‘true’ by default. If a connection is in auto-commit mode, a SQL statement is committed automatically after its successful execution.
• If a ‘Connection’ is not in auto-commit mode, you must call the ‘commit()’ or ‘rollback()’ method of the ‘Connection’ object to commit or rollback the transaction.
![Page 17: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/17.jpg)
www.NetComLearning.com
JDBC• try {• Connection conn = get the connection…• conn.setAutoCommit(false);• Statement stmt = conn.createStatement();• String sql = “update person set
income=income*1.1”;• int rowsUpdated = stmt.executeUpdate(sql);• stmt.close();• conn.commit();• conn.close();• }• catch (SQLException e)• { conn.rollback(); e.printStackTrack(); conn.close();}
![Page 18: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/18.jpg)
www.NetComLearning.com
JDBC
JDBC database update exercise
![Page 19: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/19.jpg)
www.NetComLearning.com
JDBC
• The ‘PreparedStatement’ object pre-compiles the SQL statement and reuses the pre-compiled SQL statement if the statement in executed multiple times.
• A question mark (?) in a SQL string is a placeholder for an input parameter, whose value will be supplied before the statement is executed.
![Page 20: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/20.jpg)
www.NetComLearning.com
JDBC
String sql = “insert into person” + “person_id,first_name,last_name,gender,”+ ”values (?,?,?,?,?,?)”;
• Each of the ?s is a placeholder for a value. The first ? is a placeholder for person_id, the second ? is for first_name, etc. The first placeholder is given an index of 1, the second placeholder is given an index of 2, etc.
Prepared Statement pstmt = conn.preparedStatement(sql);pstmt.setInt(1,801);
![Page 21: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/21.jpg)
www.NetComLearning.com
JDBCString sql = “insert into person” + “person_id,first_name,last_name,gender,”+ ”values (?,?,?,?,?,?)”;Connection conn = get a connection object…;Prepared Statement pstmt = conn.prepareStatement(sql);pstmt.setInt(1,801);pstmt.setString(2,”Tom”);pstmt.setString(3,”Baker”);pstmt.setString(4,”M”);java.sql.Date dob = java.sql.Date.valueOf(“1970-01-25”);pstmt.setDate(5,dob);pstmt.setDouble(6,45900);pstmt.executeUpdate();
![Page 22: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/22.jpg)
www.NetComLearning.com
JDBC
JDBC PreparedStatement Exercise
![Page 23: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/23.jpg)
www.NetComLearning.com
JDBC
• When you execute a query (a ‘SELECT’ statement) in a database, it returns the matching records in the form of a ‘ResultSet’.
• This is the data arranged in rows and columns.• The ‘SELECT’ statement determines the number of
rows and columns that are contained in a ‘ResultSet’.
• The ‘Statement’ or ‘PreparedStatement’ or ‘CallableStatement’ object returns the result of a query as a ‘ResultSet’ object.
![Page 24: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/24.jpg)
www.NetComLearning.com
JDBC
• Scrollability• A ‘ResultSet’ can be forward-only or bi-
directional (move from row to row forwards or backwards).
• A bi-directional ‘ResultSet’ can be ‘update-sensitive’ or ‘update-insensitive’ (whether changes in the underlying database will be reflected in the ‘ResultSet’ while you are scrolling through its rows).
![Page 25: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/25.jpg)
www.NetComLearning.com
JDBC
• Concurrency• A ‘ResultSet’ may be ‘read-only’ or ‘updatable’.• Holdability• This property of a ‘ResultSet’ refers to its state
after a transaction which it is associated with, has been committed. The ‘ResultSet’ may be closed or kept open after commitment of the transaction.
![Page 26: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/26.jpg)
www.NetComLearning.com
JDBC
• You can get information about the properties of a ‘ResultSet’ supported by a JDBC driver by using methods of the ‘DatabaseMetaData’ interface.
• First we get a ‘DatabaseMetaData’ object as follows:
1. Connection conn = get a connection….
2. DatabaseMetaData dmd =
conn.getMetaData();
![Page 27: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/27.jpg)
www.NetComLearning.com
JDBCboolean b1 = dmd.supportsResultSetType(TYPE_FORWARD_ONLY);boolean b2 = dmd.supportsResultSetType(TYPE_SCROLL_SENSITIVE);boolean b3 = dmd.supportsResultSetType(TYPE_SCROLL_INSENSITIVE);
boolean b4 = dmd.supportsResultSetConcurreny(TYPE_FORWARD_ONLY,CONCUR_READ_ONLY);boolean b5 = dmd.supportsResultSetConcurreny(TYPE_FORWARD_ONLY,CONCUR_UPDATABLE);boolean b6 = dmd.supportsResultSetConcurreny(TYPE_SCROLL_SENSITIVE,CONCUR_READ_ONLY);boolean b7 = dmd.supportsResultSetConcurreny(TYPE_SCROLL_SENSITIVE,CONCUR_UPDATABLE);boolean b6 = dmd.supportsResultSetConcurreny(TYPE_SCROLL_INSENSITIVE,CONCUR_READ_ONLY);boolean b7 = dmd.supportsResultSetConcurreny(TYPE_SCROLL_INSENSITIVE,CONCUR_UPDATABLE);
boolean b8 = dmd.supportsResultSetHoldability(HOLD_CURSORS_OVER_COMMIT);boolean b9 = dmd.supportsResultSetHoldability(CLOSE_CURSORS_AT_COMMIT);
![Page 28: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/28.jpg)
www.NetComLearning.com
JDBC
• Some additional ‘DatabaseMetaData’ methods:
String dbName = dmd.getDatabaseProductName();
String dbVersion = dmd.getDatabaseProductVersion();
String driverName = dmd.getDriverName();
String driverVersion = dmd.getDriverVersion();
![Page 29: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/29.jpg)
www.NetComLearning.com
JDBC
• JDBC DatabaseMetaData exercise
![Page 30: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/30.jpg)
www.NetComLearning.com
JDBC
• Typical way to get a forward-only ‘ResultSet’:
Connection conn = get a Connection object …Statement stmt = conn.getStatement();String sql = “select person_id, first_name,”+ “last_name,dob,income from person”;ResultSet rs = stmt.executeQuery(sql);
//now process the ‘ResultSet’ using the ‘rs’ variable.
![Page 31: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/31.jpg)
www.NetComLearning.com
JDBC
• The returned ‘ResultSet’ object from the ‘executeQuery()’ method is ready to be looped thru to get the associated data.
• At first, the row pointer(aka ‘cursor’) points to before the first row of the ‘ResultSet’.
• We must move the cursor to a valid row before accessing the column data for the row.
• The ‘next()’ method of the ‘ResultSet’ object is used to move the cursor to the next row.
![Page 32: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/32.jpg)
www.NetComLearning.com
JDBC
ResultSet rs = stmt.executeQuery(sql);While (rs.next()==true){
//process the current row in rs…}//done with the ResultSet rs.
![Page 33: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/33.jpg)
www.NetComLearning.com
JDBC
• A ‘ResultSet’ object lets you read the value of a column from its current row using one of the ‘getXXX()’ methods, where ‘XXX’ is the data type of the column.(e.g. ‘getInt()’, ‘getString()’)
• You must specify the index of column name in the ‘getXXX()’ method whose value you want to read.
int personID = rs.getInt(“person_id”);String firstName = rs.getString(2);
![Page 34: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/34.jpg)
www.NetComLearning.com
JDBCConnection conn = get a Connection object …Statement stmt = conn.getStatement();String sql = “select person_id, first_name,”+ “last_name,dob,income from person”;ResultSet rs = stmt.executeQuery(sql);while (rs.next()) {
int personID=rs.getInt(“person_id”);String firstName=rs.getString(“first_name”);String lastName = rs.getString(“last_name);java.sql.Date dob = rs.getDate(“dob”);double income = rs.getDouble(“income”);//do something with the retrieved values from the
cols.}
![Page 35: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/35.jpg)
www.NetComLearning.com
JDBCConnection conn = get a Connection object …Statement stmt = conn.getStatement();String sql = “select person_id, first_name,”+ “last_name,dob,income from person”;ResultSet rs = stmt.executeQuery(sql);while (rs.next()) {
int personID=rs.getInt(1);String firstName=rs.getString(2);String lastName = rs.getString(3);java.sql.Date dob = rs.getDate(4);double income = rs.getDouble(5);//do something with the retrieved values from the
cols.}
![Page 36: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/36.jpg)
www.NetComLearning.com
JDBC
• In a ‘ResultSet’ a column can have a null value. When a column has a null value a ‘getXXX()’ method returns the default value for the ‘XXX’ data type. For example, for numeric data types (int, double, byte) a ‘getXXX()’ method returns 0.
• If you want to know if the col value read was null, you need to call the ‘wasNull()’ method immediately after the ‘getXXX()’ call.
![Page 37: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/37.jpg)
www.NetComLearning.com
JDBC
ResultSet rs = get a ‘ResultSet’…java.sql.Date dob = rs.getDate(“dob”);If (rs.wasNull()) {
System.out.println(“dob is null”);}else {
System.out.println(“dob is:” + dob.toString());
}
![Page 38: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/38.jpg)
www.NetComLearning.com
JDBC
JDBC ‘ResultSet’ exercise
![Page 39: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/39.jpg)
www.NetComLearning.com
JDBC
• To get a ‘ResultSet’ that scrolls forwards and backwards, we specify the scrollable property when you create a ‘Statement’ object, as follows:
Connection conn = get Connection object…Statement stmt = conn.getStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
![Page 40: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/40.jpg)
www.NetComLearning.com
JDBC
• When you specify scrollability in creating a ‘Statement’ object, if the JDBC driver does not support that type of scrollability, it will not generate an error. Rather, the driver will return a ‘ResultSet’ with a scrollability type that it does support.
• We can check the scrollability type of a returned ‘ResultSet’ object with method ‘getType()’.
![Page 41: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/41.jpg)
www.NetComLearning.com
JDBCResultSet rs = stmt.executeQuery(sql);
//determine the scrollability type of the ResultSetint cursorType = rs.getType();if (cursorType == ResultSet.TYPE_FORWARD_ONLY) { … }if (cursorType == ResultSet.TYPE_SCROLL_INSENSITIVE) { … }if (cursorType == ResultSet.TYPE_SCROLL_SENSITIVE) { … }
//determine the concurrency of the ResultSetint concurrency = rs.getConcurrency();if (concurrency == ResultSet.CONCUR_READ_ONLY) { … }if (concurrency == ResultSet.CONCUR_UPDATABLE) { … }
![Page 42: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/42.jpg)
www.NetComLearning.com
JDBC
• To determine the number of rows in the set - after retrieving the bi-directional ‘ResultSet’ object, call the ‘last()’ method to move the cursor to the last row of the set, then call the ‘getRow()’ method to get the row number of the last row, which will be the number of rows in the set.
• To process the ‘ResultSet’ after determining the number of rows, call ‘beforeFirst()’ to scroll the cursor before the first row and then loop thru.
![Page 43: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/43.jpg)
www.NetComLearning.com
JDBC
• Cursor movement methods://Movements relative to current cursor position:boolean next();boolean previous();boolean relative(int rows); // can move either direction
//Movements to absolute row numbersboolean first();boolean last();void beforeFirst();void afterLast();boolean absolute(int row);//positive row# - from beginning //negative row# - from end
![Page 44: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/44.jpg)
www.NetComLearning.com
JDBC
• Knowing current cursor positionboolean isBeforeFirst()boolean isFirst()boolean isLast()boolean isAfterLast()int getRow() //returns 0 if the cursor is positioned // before the first row, // after the last row, //or if the ResultSet is empty.
![Page 45: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/45.jpg)
www.NetComLearning.com
JDBC
• You can close a ‘ResultSet’ object by calling its ‘close()’ method.
rs.close();• Closing the ‘ResultSet’ frees the resources
associated with it.• When the ‘Statement’ object that produces
the ‘ResultSet’ object is closed, it automatically closes the ‘ResultSet’ object.
![Page 46: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/46.jpg)
www.NetComLearning.com
JDBC
JDBC bi-directional ResultSet exercise.
![Page 47: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/47.jpg)
www.NetComLearning.com
JDBC
• You can perform insert, update and delete actions using a ‘ResultSet’.
• The concurrency for the ‘ResultSet’ object must be ‘ResultSet.CONCUR_UPDATABLE’.
• There is an imaginary row in a ‘ResultSet’ that is called an ‘insert-row’.
• Think of this row as an empty new row which acts as a staging area for a new row to be inserted.
![Page 48: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/48.jpg)
www.NetComLearning.com
JDBC
• You position the cursor to the insert-row using the ‘ResultSet’ object’s ‘moveToInsertRow()’ method.
• When the cursor moves to the insert-row, it remembers its previous position.
• You can call the ‘moveToCurrentRow()’ method to move the cursor from the insert-row back to the previously current row.
![Page 49: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/49.jpg)
www.NetComLearning.com
JDBC
• Once the cursor is positioned at the insert-row, you need to set the values for the columns using one of the ‘updateXXX()’ methods of the ‘ResultSet’ object where ‘XXX’ is the type of the column.
• The first argument to ‘updateXXX()’ is either the column index of the column name, and the second argument is the value to be set.
![Page 50: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/50.jpg)
www.NetComLearning.com
JDBC
• Next we must send the changes to the database before our new row becomes part of our ‘ResultSet’ object.
• You can send the newly inserted row to the database by calling the ‘insertRow()’ method of the ‘ResultSet’ interface.
• If ‘auto-commit’ enabled for the ‘Connection’, the transaction will be committed immediately.
• Otherwise, ‘commit()’ the transaction to make the insert permanent (or ‘rollback()’ to cancel the transaction).
![Page 51: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/51.jpg)
www.NetComLearning.com
JDBCstmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);// Get the result set ResultSet rs = stmt.executeQuery(SQL);// Make sure our resultset is updatableint concurrency = rs.getConcurrency();if (concurrency != ResultSet.CONCUR_UPDATABLE) {System.out.println("JDBC driver does not " + "support updatable result set.");return;} // First insert a new row to the ResultSetrs.moveToInsertRow();rs.updateInt("person_id", 555);rs.updateString("first_name", "Richard");rs.updateString("last_name", "Castillo");rs.updateString("gender", "M");// Send the new row to the databasers.insertRow();
![Page 52: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/52.jpg)
www.NetComLearning.com
JDBC
To update an existing row in a ‘ResultSet’:• Move cursor to valid row of ‘ResultSet’.• Call an ‘updateXXX()’ method on a column.• Call the ‘updateRow()’ method to send the
changes to the database.• If ‘auto-commit’ enabled, changes sent to
database immediately.• Otherwise, you commit the changes.
![Page 53: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/53.jpg)
www.NetComLearning.com
JDBCstmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);// Get the result set ResultSet rs = stmt.executeQuery(SQL);// Make sure our resultset is updatableint concurrency = rs.getConcurrency();if (concurrency != ResultSet.CONCUR_UPDATABLE) {System.out.println("JDBC driver does not " + "support updatable result set.");return;}// Give everyone a raisewhile (rs.next()) { double oldIncome = rs.getDouble("income"); double newIncome = 0.0; If (rs.wasNull()) { // The person with no income starts at 10000.00 oldIncome = 10000.00; newIncome = oldIncome;} else {// Increase income newIncome = oldIncome + oldIncome * (raise / 100.0);} // Update the income column for current row to newIncome rs.updateDouble("income", newIncome); // Send the changes to the database before we scroll to next row rs.updateRow();}
![Page 54: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/54.jpg)
www.NetComLearning.com
JDBC
To delete a row from a ‘ResultSet’:• Position the cursor at a valid row.• Call the ‘deleteRow()’ method of the
‘ResultSet’ object.• If ‘auto-commit’ is enabled, row will be
permanently deleted from the database.• Otherwise, commit the transaction, or cancel
the delete by rolling back the transaction.
![Page 55: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/55.jpg)
www.NetComLearning.com
JDBC
JDBC ResultSet Update Exercise
![Page 56: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/56.jpg)
www.NetComLearning.com
JDBC
• The interface ‘RowSet’ from the ‘javax.sql’ package is a wrapper for ‘ResultSet’ and inherits from the ‘ResultSet’ interface.
• A ‘RowSet’ allows for simpler JDBC programming. You need not deal directly with ‘Connection’ and ‘Statement’ objects, all you need to work with is the ‘RowSet’ object.
![Page 57: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/57.jpg)
www.NetComLearning.com
JDBC
• A ‘ResultSet’ is not serializable, where as a ‘RowSet’ is serializable.
• A ‘ResultSet’ object is always connected to a database, whereas a ‘RowSet’ object need not be connected to a data source at all times. It can connect to a database when required, e.g. when retrieving/updating data in the database.
• A ‘RowSet’ object is by default scrollable and updatable.
![Page 58: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/58.jpg)
www.NetComLearning.com
JDBC
• The ‘javax.sql.RowSet’ package defines five interfaces which inherit from the ‘RowSet’ interface, which itself inherits from the ‘ResultSet’ interface.
• Therefore, all the methods of the ‘ResultSet’ interface are available in these five types of rowsets.
• Typically, database vendors provide implementation classes for the five types of rowsets.
![Page 59: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/59.jpg)
www.NetComLearning.com
JDBC
The five types of rowsets are:
• JdbcRowSet• CachedRowSet• WebRowSet• FilteredRowSet• JoinRowSet
![Page 60: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/60.jpg)
www.NetComLearning.com
JDBC
• The ‘javax.sql.rowset.RowSetFactory’ interface lets you create the different types of ‘RowSet’ objects.
• To get an instance of ‘RowSetFactory’ we use the static ‘newFactory()’ method of the ‘javax.sql.RowSetProvider’ class.
• The ‘RowSetFactory’ interface has five methods to create the five types of rowsets.
• They are named ‘createXXXRowset()’ where XXX could be ’Jdbc’,‘Cached’, ‘Web’,‘Filtered’, and ’Join’.
![Page 61: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/61.jpg)
www.NetComLearning.com
JDBCtry { RowSetFactory rsf = RowSetProvider.newFactory();
JdbcRowSet jrs = rsf.createJdbcRowSet();
//… work with the JdbcRowSet…
}catch (SQLException e){ e.printStackTrace();}
![Page 62: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/62.jpg)
www.NetComLearning.com
JDBC
• A ‘RowSet’ object will need to connect to a data source to retrieve and update data.
• A ‘RowSet’ will use a JDBC driver to connect to a database. Set the connection properties as follows:
RowSet rs = … get a RowSet object …;Rs.setUrl = “jdbc:derby:jdbcDemoDb”;Rs.setUsername = “user”;Rs.setPassword = “password”;
• You don’t need to establish connection to the database, the ‘RowSet’ object will take care of it.
![Page 63: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/63.jpg)
www.NetComLearning.com
JDBC
• When you work with a ‘RowSet’ you need not worry about ‘Statement’, ‘PreparedStatement’ etc. objects.
• However you must specify a command that will generate the result set for the ‘RowSet’ object.
• The command would be in the form of a SQL statement in a string.
• You can use a ‘?’ as a placeholder for any parameter to be passed to the command at runtime.
• To set the parameter at runtime, use one of the ‘setXXX()’ methods of the ‘RowSet’ interface.
![Page 64: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/64.jpg)
www.NetComLearning.com
JDBC
RowSet rs = … get a RowSet object …
String sql=“select person_id,first_name”+ “last_name from person”;
rs.setCommand(sql);
![Page 65: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/65.jpg)
www.NetComLearning.com
JDBC
RowSet rs = … get a RowSet object …String sql=“select person_id,first_name”+ “last_name from person”+ “where income between ? and ?”;rs.setDouble(1,20000.0);rs.setDouble(2,40000.0);rs.setCommand(sql);
![Page 66: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/66.jpg)
www.NetComLearning.com
JDBC
To populate a ‘RowSet’ object with data by executing its command, you call its ‘execute()’ method:
rs.execute();
After the ‘execute()’ method is executed, the ‘RowSet’ object has data in it, and you need to scroll to a row to read/update its column value.
![Page 67: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/67.jpg)
www.NetComLearning.com
JDBC
• A ‘RowSet’ inherits all cursor movement methods from the ‘ResultSet’ interface.
• By default, all ‘RowSet’ objects are bi-directional scrollable and updateable.
rs.execute();while (rs.next()) { int personID = rs.getInt(“person_id”); String firstname=rs.getString(“first_name”);}
![Page 68: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/68.jpg)
www.NetComLearning.com
JDBC
• Updating data in a ‘RowSet’ is similar to updating data in a ‘ResultSet’ object.
• To update a columns value, you need to move the cursor to a row and use one of the ‘updateXXX()’ methods to set the new value for the column.
• Then you call the ‘updateRow()’ method of the ‘RowSet’ object.
![Page 69: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/69.jpg)
www.NetComLearning.com
JDBC
• To insert a new row, move the cursor to the insert-row by calling ‘moveToInsertRow()’ method of the ‘RowSet’.
• Set the column values in the insert-row by using the ‘updateXXX()’ methods of ‘RowSet’.
• Then call ‘insertRow()’ method of ‘RowSet’ to insert the row into the ‘RowSet’.
![Page 70: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/70.jpg)
www.NetComLearning.com
JDBC
• To delete a row, move the cursor to the row you want to delete and call the ‘deleteRow()’ method of the ‘RowSet’.
![Page 71: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/71.jpg)
www.NetComLearning.com
JDBC
How and when the changes to a ‘RowSet’ are actually propagated to the database depends on the type of ‘RowSet’.
![Page 72: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/72.jpg)
www.NetComLearning.com
JDBC
The five types of rowsets are:
• JdbcRowSet• CachedRowSet• WebRowSet• FilteredRowSet• JoinRowSet
![Page 73: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/73.jpg)
www.NetComLearning.com
JDBC
• A ‘JdbcRowSet’ is also called a ‘connected-rowset’ because it always maintains a database connection.
• As a ‘ResultSet’ object always maintains a database connection so does the ‘JdbcRowSet’
• The ‘setAutoCommit()’ method enables or disables auto-commit mode.
• Methods ‘commit()’ and ‘rollback()’ allow you to commit or rollback changes to data.
![Page 74: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/74.jpg)
www.NetComLearning.com
JDBC
• Updating data using a ‘JdbcRowSet’ object is similar to updating for a ‘ResultSet’.
• All methods will be called on the ‘JdbcRowSet’ instead of the ‘ResultSet’ object.
• You must call the ‘updateRow()’ method of the ‘JdbcRowSet’ after updating the column value(s) and before you scroll to another row.
• Use the ‘JdbcRowSet’ object’s ‘commit()’ or ‘rollback()’ methods to commit or rollback.
![Page 75: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/75.jpg)
www.NetComLearning.com
JDBC
JdbcRowSet Update exercise
![Page 76: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/76.jpg)
www.NetComLearning.com
JDBC
• A ‘CachedRowSet’ is also called a ‘disconnected rowset’. It keeps the database connection open only for the duration it needs to interact with the database.
• It retrieves all data generated by the command and caches it in memory.
• A ‘CachedRowSet’ is always serializable, scrollable, and updateable.
![Page 77: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/77.jpg)
www.NetComLearning.com
JDBC
• Methods to populate a ‘CachedRowSet’ object:
void execute() void execute(Connection conn)void populate(ResultSet data)void populate (ResultSet rs, int startRow)
• All of the above throw ‘SQLException’
![Page 78: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/78.jpg)
www.NetComLearning.com
JDBC
• If you have set the database connection properties for a ‘CachedRowSet’ you can use the ‘execute()’ method with no parameters.
• Another version of the ‘execute()’ method accepts a ‘Connection’ object which will be used to populate the ‘CachedRowSet’ with data.
• The ‘populate()’ method will populate the ‘CachedRowSet’ with data from the ‘ResultSet’ object passed in.
• Another version of the ‘populate()’ method accepts a starting row number of the ‘ResultSet’
![Page 79: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/79.jpg)
www.NetComLearning.com
JDBC
• You can obtain the number of rows in a ‘CachedRowSet’ using its ‘size()’ method.
• Since a ‘CachedRowSet’ caches all its rows in memory, it can provide you a count of rows at any time.
int rowCount = myCachedRowSet.size();
• Retrieving rows from a database for a ‘CachedRowSet’ is similar to that for ‘JdbcRowSet’, except for the additional ‘size()’ method of ‘CachedRowSet’.
![Page 80: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/80.jpg)
www.NetComLearning.com
JDBC
CachedRowSet exercise
![Page 81: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/81.jpg)
www.NetComLearning.com
JDBC
• ‘CachedRowSet’ provides a feature called ‘paging’ to allow you to retrieve rows in chunks.
• Use the ‘setPageSize(int size)’ method to set the pagesize.
• Use the ‘nextPage()’ method to get subsequent pages, and use the ‘previousPage()’ method to get prior pages.
• Both methods return ‘true’ if there are more pages to retrieve, otherwise they return ‘false’.
• Use an outer loop to retrieve pages, and an inner loop to retrieve rows of the page.
![Page 82: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/82.jpg)
www.NetComLearning.com
JDBCString sqlCommand = "select person_id, first_name, last_name from person";cachedRs.setCommand(sqlCommand);cachedRs.setPageSize(4); // Set page size cachedRs.execute();int pageCounter = 1;// Retrieve and print person records one page at a timedo { System.out.println("Page #" + pageCounter + " (Row Count=" + cachedRs.size() +")"); // Loop and Print the records in the current page // Increment the page count by 1 pageCounter++; } while (cachedRs.nextPage());
![Page 83: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/83.jpg)
www.NetComLearning.com
JDBC
• You can update data in a ‘CachedRowSet’ and save the changes back to the database.
• Use ‘updateRow()’, ‘insertRow()’, ‘deleteRow()’ methods as with ‘JdbcRowSet’ but in this case these methods DO NOT send the changes to the database.
• Send changes to the database by calling the ‘acceptChanges()’ method.
![Page 84: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/84.jpg)
www.NetComLearning.com
JDBC
• ‘WebRowSet’ inherits from ‘CachedRowSet’ and adds capabilities to read data from an XML doct and write data to an XML doct.
• User ‘readXML()’ to read XML data into a ‘WebRowSet, and ‘writeXML’ to export XML data from a ‘WebRowSet’.
![Page 85: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/85.jpg)
www.NetComLearning.com
JDBCWebRowSet wrs = … get a web rowset with data …;
//Create a StringWriter object to hold the exported XMLjava.io.StringWriter sw = new java.io.StringWriter();
//Write the XML representation of wrs into swwrs.writeXML(sw);
//Get the String object from swString webRsXML = sw.toString();
• String webRsXML contains the XML representation of the wrs object.
![Page 86: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/86.jpg)
www.NetComLearning.com
JDBC
Importing XML into a ‘WebRowSet’:
//Create a StringReader object from an XML StringJava.io.StringReader sr = new java.io.StringReader(webRsXML);
//Create an empty WebRowSet objectRowSetFactory rsf = RowSetProvider.newFactory();WebRowSet newWebRs = rsf.createWebRowSet();
// Import the XML content into the new WebRowSetnewWebRs.readXML(sr);
![Page 87: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/87.jpg)
www.NetComLearning.com
JDBC
WebRowSetXml exercise
![Page 88: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/88.jpg)
www.NetComLearning.com
JDBC
• ‘FilteredRowSet’ inherits from ‘WebRowSet’ and provides filtering capabilities for a rowset.
• You view the rows based on a set of criteria which is called a ‘filter’.
• Setting a ‘filter’ to the rowset does not delete rows from the rowset, rather, you only access those rows that satisfy the ‘filter’ criteria.
• You can reset the ‘filter’ to view all rows.• A ‘filter’ is an object of a class that implements the
‘javax.sql.rowset.Predicate’ interface.
![Page 89: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/89.jpg)
www.NetComLearning.com
JDBC
• ‘JoinRowSet’ inherits from ‘WebRowSet’.• It provides the ability to combine (or join) two or more
disconnected rowsets into one.• You retrieve data in multiple rowsets, using
‘CachedRowSet’, ‘WebRowSet’ or ‘FilteredRowSet’.• Create and empty ‘JoinRowSet’.• Add all rowsets to the ‘JoinRowSet’ by calling its
‘addRowSet()’ method.• You may specify the ‘JOIN’ columns in a rowset when
you add a rowset to the ‘JoinRowSet’.
![Page 90: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/90.jpg)
www.NetComLearning.com
JDBCcachedRs1 = ... get a CachedRowSet …cachedRs2 = ... get a second CachedRowSet …String sqlCommand1 = "select person_id, first_name from person " + "where person_id in (101, 102)";String sqlCommand2 = "select person_id, last_name from person " + "where person_id in (101, 102)";cachedRs1.setCommand(sqlCommand1);cachedRs2.setCommand(sqlCommand2);cachedRs1.execute();cachedRs2.execute();// Create a JoinRowSet for cachedRs1 and cachedRs2 joinRs = … get a JoinRowSet object …// Add rowsets to the join rowset - join them based on the person_id columnjoinRs.addRowSet(cachedRs1, "person_id");joinRs.addRowSet(cachedRs2, "person_id");
![Page 91: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/91.jpg)
www.NetComLearning.com
JDBC
• A database transaction consists of one or more changes as a unit of work.
• A ‘savepoint’ in a transaction is like a marker that marks a point in a transaction so that, if needed, the transaction can be undone (rolled back) up to that point.
![Page 92: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/92.jpg)
www.NetComLearning.com
JDBCConnection conn = … get a Connection object …Statement stmt = conn.createStatement();stmt.execute(“insert into person…”); // insert 1stmt.execute(“insert into person…”); // insert 2stmt.execute(“insert into person…”); // insert 3stmt.execute(“insert into person…”); // insert 4stmt.execute(“insert into person…”); // insert 5
• At this point we either commit the transaction, which will insert all five records in the ‘person’ table:
conn.commit();
• Or we rollback the transaction so that no inserts will occur.conn.rollback();
![Page 93: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/93.jpg)
www.NetComLearning.com
JDBC
• A ‘savepoint’ will let you set a marker in between any of the ‘INSERT’ statements.
• To mark a ‘savepoint’, call the ‘setSavepoint()’ method of a ‘Connection’ object.
• The ‘setSavepoint()’ method returns a ‘Savepoint’ object which is your marker, and which you must keep for future use.
![Page 94: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/94.jpg)
www.NetComLearning.com
JDBCConnection conn = … get a Connection object …Statement stmt = conn.createStatement();stmt.execute(“insert into person…”); // insert 1Savepoint sp1 = conn.setSavepoint();// savepoint 1stmt.execute(“insert into person…”); // insert 2Savepoint sp2 = conn.setSavepoint();// savepoint 2stmt.execute(“insert into person…”); // insert 3Savepoint sp3 = conn.setSavepoint();// savepoint 3stmt.execute(“insert into person…”); // insert 4Savepoint sp4 = conn.setSavepoint();// savepoint 4stmt.execute(“insert into person…”); // insert 5Savepoint sp5 = conn.setSavepoint();// savepoint 5
![Page 95: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/95.jpg)
www.NetComLearning.com
JDBC
• We can use another version of the ‘rollback()’ method of the ‘Connection’ object, which accepts a ‘Savepoint’ object.
• To undo all the changes which were made after savepoint 4:
conn.rollback(sp4);• To undo all changes made after savepoint 2:
conn.rollback(sp2);
![Page 96: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/96.jpg)
www.NetComLearning.com
JDBC
• Once you rollback to a particular ‘savepoint’, all savepoints which were created after the given ‘savepoint’ will be released, and you cannot refer to them again.
• You can also release a ‘savepoint’ explicitly by calling the ‘releaseSavepoint(Savepoint sp)’ method of a ‘Connection’ object.
• After a partial rollback, remember to ‘commit()’ the remaining portions of the transaction.
![Page 97: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/97.jpg)
www.NetComLearning.com
JDBCThe purpose of the DAO pattern is to hide the persistence mechanism of an application from its application logic. In other words, to turn this sequence of calls
application logic --> persistence mechanism
into this
application logic --> DAO's --> persistence mechanism
The advantage of this abstraction is that you can change the persistence mechanism without affecting the application logic. All you need to change is the DAO layer which, if designed properly, is a lot easier to do than changing all the application logic.
![Page 98: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/98.jpg)
www.NetComLearning.com
JDBC
• In OOP, a useful and important design principle is “separation of concerns.” The DAO design pattern helps you comply with this design principle. If you are not using DAO, then your business logic will be exposed to the concrete implementation details of the persistence mechanisms - an undesirable state of affairs. Use of the DAO design pattern ensures that you separate your core logic from your persistence mechanism.
• If your database environment changes, all you need to change is the DAO layer, which is a lot easier to do than changing all the application logic.
![Page 99: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/99.jpg)
www.NetComLearning.com
JDBC
//Student.java{…. Fields, getters, setters….}
// StudentDAO.java public interface StudentDAO { public void insertStudent( Student student); public Student findStudent( int id); public void deleteStudent( int id);}
![Page 100: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/100.jpg)
www.NetComLearning.com
JDBC
// RDBMSDAO.java public class RDBMSDAO implements StudentDAO{ public void insertStudent( Student student){ // insertStudent implementation } public Student findStudent( int id){ // findStudent implementation return Student; } public void deleteStudent( int id) { // deleteStudent implementation }}
![Page 101: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/101.jpg)
www.NetComLearning.com
JDBC
// Main.java public class Main{ public static void main( String[] args) { Student student = new Student( ... ); StudentDAO studentDAO = new RDBMSDAO(); studentDAO.insertStudent(student); }}
![Page 102: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/102.jpg)
www.NetComLearning.com
JDBC// OODBMSDAO.java public class OODBMSDAO implements StudentDAO{ public void insertStudent( Student student){ // insertStudent implementation } public Student findStudent( int id){ // findStudent implementation return Student; } public void deleteStudent( int id) { // deleteStudent implementation }}
![Page 103: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/103.jpg)
www.NetComLearning.com
JDBC
// Main.java public class Main{ public static void main( String[] args) { Student student = new Student( ... ); StudentDAO studentDAO = new OODBMSDAO(); studentDAO.insertStudent(student); }}
![Page 104: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/104.jpg)
www.NetComLearning.com
JDBC
• The ‘DAO Design Pattern’ fits nicely with the ‘Factory Design Pattern’, and both techniques are often used together.
• Let us first gain an understanding of the ‘Factory Design Pattern’, and then we will examine how to use it in combination with the ‘DAO Design Pattern’.
![Page 105: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/105.jpg)
www.NetComLearning.com
JDBC
• In real life, factories are manufacturing units that produce multiple instances of a product. For instance, a car factory produces cars. The main responsibility of the car factory is to keep producing cars of the required type and model.
• In the software environment, you can implement a factory that returns the required type of object(s) on demand in OOP. In this case, the factory decides which classes to instantiate.
![Page 106: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/106.jpg)
www.NetComLearning.com
JDBC
public interface Shape { public void drawTheShape(); public void colorTheShape(); }
![Page 107: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/107.jpg)
www.NetComLearning.com
JDBC
public class Circle implements Shape { private int xPos, yPos;private int radius;public Circle( int x, int y, int r) {xPos = x;yPos = y;radius = r;}public void drawTheShape() { implementation here }public void colorTheShape() { implementation here }}
![Page 108: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/108.jpg)
www.NetComLearning.com
JDBC
public class Rectangle implements Shape {private int length, height;public Rectangle( int l, int h){length = l;height = h;}public void drawTheShape(){ implementation code} public void colorTheShape(){ implementation code } }
![Page 109: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/109.jpg)
www.NetComLearning.com
JDBC
public class ShapeFactory {public static Shape getShape( String shapeType){switch( shapeType) {case "Circle": return new Circle( 10, 10,20);case "Rectangle": return new Rectangle(10,20);}
![Page 110: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/110.jpg)
www.NetComLearning.com
JDBC
public class Test {public static void main( String[] args) {String shapeType=“Circle”;Shape shape = ShapeFactory.getShape(shapeType);shape.drawTheShape();shape.colorTheShape();}}
![Page 111: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/111.jpg)
www.NetComLearning.com
JDBC
• You define a ‘Shape’ interface, which defines two public methods, ‘drawTheShape()’ and ‘colorTheShape()’.
• Classes ‘Circle’ and ‘Rectangle’ implement this interface and provide implementation of interface methods.
• The ‘factory’ class ‘ShapeFactory’ has a ‘getShape()’ method which, based upon the requested type of shape, creates a new instance of the appropriate ‘Shape’ class.
• From the main() method of the ‘Test’ class, you supply a String which indicates which type of ‘Shape’ you want. The main() method then calls the getShape() method of ShapeFactory class. The getShape() method creates and returns a new instance based on the requested type.
![Page 112: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/112.jpg)
www.NetComLearning.com
JDBC
• We will now look at the combination of the ‘DAO Design Pattern’ with the ‘Factory Design Pattern’.
• Recall that the purpose of the DAO pattern is to hide the persistence mechanism of an application from its application logic.
• The advantage of this abstraction is that you can change the persistence mechanism without affecting the application logic. All you need to change is the DAO layer which, if designed properly, is a lot easier to do than changing all the application logic.
![Page 113: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/113.jpg)
www.NetComLearning.com
JDBC
//Student.java{…. Fields, getters, setters….}
// StudentDAO.java public interface StudentDAO { public void insertStudent( Student student); public Student findStudent( int id); public void deleteStudent( int id);}
![Page 114: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/114.jpg)
www.NetComLearning.com
JDBC
// RDBMSDAO.java public class RDBMSDAO implements StudentDAO{ public void insertStudent( Student student){ // insertStudent implementation } public Student findStudent( int id){ // findStudent implementation return Student; } public void deleteStudent( int id) { // deleteStudent implementation }}
![Page 115: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/115.jpg)
www.NetComLearning.com
JDBC// OODBMSDAO.java public class OODBMSDAO implements StudentDAO{ public void insertStudent( Student student){ // insertStudent implementation } public Student findStudent( int id){ // findStudent implementation return Student; } public void deleteStudent( int id) { // deleteStudent implementation }}
![Page 116: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/116.jpg)
www.NetComLearning.com
JDBC
• So now we have two ‘StudentDAO’ implementations – RDBMSDAO and OODBMSDAO.
• We will define a ‘Factory’ (using the ‘Factory Design Pattern’), which will have the capability to produce each type of DAO object.
![Page 117: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/117.jpg)
www.NetComLearning.com
JDBC
public class DAOFactory {public static StudentDAO getStudentDAO(String type){switch( type){case “RDBMS”: return new RDBMSDAO();Case “OODBMS”: return new OODBMSDAO();}}
![Page 118: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/118.jpg)
www.NetComLearning.com
JDBC
• Now we will use our ‘factory’ to persist an instance of a ‘Student’.
• First we will instantiate a new ‘Student’.• Then we will use our factory to get a
‘StudentDAO’ of type ‘RDBMSDAO’. • Then we will use our ‘StudentDAO’ object to
persist our ‘Student’ object.
![Page 119: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/119.jpg)
www.NetComLearning.com
JDBC
public class Test {public static void main( String[] args) {Student s1 = new Student(…);StudentDAO DAO = DAOFactory.getStudentDAO("RDBMS");DAO.insertStudent( s1);}}
![Page 120: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/120.jpg)
Localization
When adapting an application to an international market, differences to be accounted for include:
o Differences in language;o Differences in display of numbers;o Differences in display of date information.
120
![Page 121: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/121.jpg)
Localization
The classes that take these differences into account make use of a ‘Locale’, which is an instance of the ‘Locale’ class which describes:• A language• A location (typically a country)• Optionally, a variant, specifying features such
as dialects, or spelling rules.
121
![Page 122: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/122.jpg)
Localization
Java uses codes that were defined by the International Organization for Standardization (ISO).The local language is expressed as a lowercase two-letter code.The country code is expressed as an uppercase two-letter code.
122
![Page 123: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/123.jpg)
Localization
Examples of language codes:
English enFrench frGerman deItalian it
123
![Page 124: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/124.jpg)
Localization
• Examples of country codes:
Canada CAGermany DEGreat Britain GBItaly ITUnited States US
124
![Page 125: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/125.jpg)
Localization
Given a language code, or a language code and a country code, we can construct a ‘Locale’ object:
Locale french = new Locale(“fr”);
Locale germanSwiss = new Locale(“de”,”CH”);
Local germanGermany = new Locale(“de”,”DE”);
125
![Page 126: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/126.jpg)
Localization
Java has pre-defined ‘Locale’ objects, such as:Locale.CANADALocale.CANADA_FRENCHLocale.TAIWANJava also has pre-defined locales that just specify a language without a location:Locale.CHINESELocale.ENGLISH
126
![Page 127: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/127.jpg)
Localization
• The static method ‘getDefault()’ gets the currently default locale.
• The static method ‘setDefault()’ allows you to change the default locale.
• The static method ‘getAvailableLocales()’ returns an array of locales they support.
127
![Page 128: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/128.jpg)
Localization
Other methods of the Locale class provide information about a particular Locale.
• String getDisplayName()• String getDisplayLanguage()• String getDisplayCountry()
128
![Page 129: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/129.jpg)
Localization
Formatting of numbers in highly locale-dependent. Java provides ‘formatter objects’ that can format and parse numbers, based upon the specified Locale.
NumberFormat NFormat = NumberFormat.getInstance(locale);NFormat.format(tenMillion);NFormat.parse(String-number);
129
![Page 130: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/130.jpg)
Localization
Formatting of monetary amounts is also highly locale-dependent. Java similarly provides currency formatter objects for this purpose:
NumberFormat nf = NumberFormat.getCurrencyInstance(locale); nf.format(tenMillion));
130
![Page 131: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/131.jpg)
Localization
There are also methods for obtaining information about currencies, such as the currency symbol, and the currency name.
Currency currencyInstance = Currency.getInstance(locale);currencyInstance.getSymbol();currencyInstance.getDisplayName();
131
![Page 132: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/132.jpg)
Localization
When formatting date and time information, some issues of concern include:
• Names of months and weekdays should be presented in the local language.
• The order of the day, month, and year will be determined by the particular locale in use.
132
![Page 133: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/133.jpg)
Localization
We have methods to help us localize the display of date information based upon the supplied locale:DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
dateFormat.format(today)
133
![Page 134: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/134.jpg)
Localization
In addition to locale information, there are different styles for displaying dates, such as:
DateFormat.SHORTDateFormat.MEDIUMDateFormat.LONGDateFormat.FULL
134
![Page 135: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/135.jpg)
Localization
Furthermore, time information is often displayed based upon localization:
DateFormat dtfmt = DateFormat.getDateInstance(DateFormat.SHORT, locale);DateFormat tmfmt = DateFormat.getTimeInstance(DateFormat.SHORT, locale);DateFormat dttmfmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, locale);
135
![Page 136: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/136.jpg)
Localization
• When localizing an application,you may have many Strings (messages, button labels, etc) that need to be translated to different languages.
• We will define the message Strings in an external location, called a ‘resource’.
• For different languages, work with the external resource files without having to touch the source code.
136
![Page 137: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/137.jpg)
Localization
• In Java, you can use property files to specify string resources, or you can implement classes for resources of other types.
• For an application, you provide a set of ‘resource bundles’ where each bundle will have the String messages, for example, for a given locale.
• For each ‘bundle’, you provide a version for each relevant locale.
137
![Page 138: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/138.jpg)
Localization
We have a specific naming convention for these bundles:• Resources specific to Germany will go into a
file ‘bundleName_de_DE’• Resources specific to USA will go into a file
‘bundleName_en_US’• Resources shared by all French speaking
countries will go into ‘bundleName_fr’
138
![Page 139: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/139.jpg)
Localization
Internationalization with property files:• Place your Strings into a property file such as
‘StringProps.properties’.• This is a text file with one key/value pair per
line.• Name the file as per previous slide:– StringProps_en_US.properties– StringProps_de_DE.properties– StringProps_fr.properties
139
![Page 140: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/140.jpg)
Localization
In the code, you will:• Load the bundle:ResourceBundle bundle = ResourceBundle.getBundle(“StringProps”,locale)
• To look up a specific String:String label = bundle.getString(“Greeting”);
140
![Page 141: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/141.jpg)
Localization
Internationalization with Bundle classes:• To provide resources that are not Strings, create
classes that extend ‘ListResourceBundle’ class.• Use the standard convention to name the classes:
bundleName_de_DEbundleName_fr
• Load the class with the ‘getBundle()’ method:ResourceBundle bundle = ResourceBundle.getBundle(“MyBundle”,locale)
141
![Page 142: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/142.jpg)
Localization
class MyBundle_en_US extends ListResourceBundle{private static final Object[][]contents={
{“phoneMaker”,”Apple”},{“battery life”, new double(16.0)}
}public Object[][] getContents()
{ return contents;}}
142
![Page 143: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/143.jpg)
Localization
class MyBundle_de extends ListResourceBundle{private static final Object[][]contents={
{“phoneMaker”,”Siemens”},{“battery life”, new double(8.5)}
}public Object[][] getContents()
{ return contents;}}
143
![Page 144: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/144.jpg)
Java 7 Boot Camp
Java Boot Camp• 6 days, 60 hours• Includes 2 test vouchers
• 1Z0-803 : Oracle Certified Associate, Java SE 7 Programmer• 1Z0-804 : Oracle Certified Professional, Java SE 7 Programmer
• Courseware included• In class Instructor-led or Live Online instructor led• Las Vegas Boot Camp is all inclusive
Upcoming Schedules:• October 6th in Las Vegas or Live Online• October 20th in NYC & Live Online
Locations: Attend in-class or Live Online Instructor-led• New York• Las Vegas (All Inclusive Travel Package)• Arlington VA
www.netcomlearning.com
![Page 145: Java 7 Certification JDBC & Related Design Pattern NetCom’s Average Instructor Rating: 8.7/9 Arthur Gober Subject Matter Expert.](https://reader031.fdocuments.in/reader031/viewer/2022032805/56649eeb5503460f94bfc8b1/html5/thumbnails/145.jpg)
Love of Learning
Your class enrollment at NetCom Learning will pay for a child’s education for a year!
Empowering The Disadvantaged
www.netcomlearning.com