Java 7 JDBC Improvements

Java 7 JDBC Improvements

JDBC (Java Database Connectivity) provides universal data access from the Java programming language. You can access any data from database, spreadsheets or flat files by using JDBC.

In Java 7, Java has introduced the following features:

1) It provides the ability to use a try-with-resources statement to automatically close resources of type Connection, ResultSet, and Statement.

2) RowSet 1.1: The introduction of the RowSetFactory interface and the RowSetProvider class, which enable you to create all types of row sets supported by your JDBC driver.

RowSetFactory Interface

It defines the implementation of a factory that is used to obtain different types of RowSet.


RowSetFactory Interface Methods

Methods Description
CachedRowSet createCachedRowSet() throws SQLException It creates a new instance of a FilteredRowSet. It throws SQLException, if a CachedRowSet cannot be created.
FilteredRowSet createFilteredRowSet() throws SQLException It creates a new instance of a FilteredRowSet. It throws SQLException, if a FilteredRowSet cannot be created.
JdbcRowSet createJdbcRowSet() throws SQLException It creates a new instance of a JdbcRowSet. It throws SQLException, if a JdbcRowSet cannot be created.
JoinRowSet createJoinRowSet() throws SQLException It creates a new instance of a JoinRowSet. It throws SQLException, if a JoinRowSet cannot be created.
WebRowSet createWebRowSet() throws SQLException It creates a new instance of a WebRowSet. It throws SQLException, if a WebRowSet cannot be created.

Java RowSetProvider Class

It is a factory API that helps to applications to get a RowSetFactory implementation that can be used to create different types of RowSet.

Methods Description
public static RowSetFactory newFactory() throws SQLException It creates a new instance of a RowSetFactory implementation. It throws SQLException, if the default factory class cannot be loaded or instantiated.
public static RowSetFactory newFactory(String factoryClassName, ClassLoader cl) throws SQLException It creates a new instance of a RowSetFactory from the specified factory class name. This function is useful when there are multiple providers in the classpath. It gives more control to the application as it can specify which provider should be loaded. It throws SQLException, if factoryClassName is null, or the factory class cannot be loaded.

JDBC Example: Mysql Connection by using Try-With-Resources

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. class JdbcExample{
  6.     public static void main(String args[]){
  7.         try(// ————–try-with-resources begin————-//
  8.             // Creating connection
  9.             Connection con = DriverManager.getConnection( “jdbc:mysql://localhost:3306/student”,“root”,“mysql”);
  10.             // Creating statement
  11.             Statement stmt=con.createStatement();
  12.             // Executing Sql query
  13.             ResultSet rs=stmt.executeQuery(“select * from user”);
  14.            )// ————–try-with-resources end————–//
  15.         { // —————-try block begin———————//
  16.             // Iterating ResultSet elements
  17.             while(rs.next()){
  18.                 System.out.println(rs.getInt(1)+“”+rs.getString(2)+“”+rs.getString(3));
  19.             }
  20.         } // —————-try block end———————-//
  21.         catch(Exception e){ // Exception handler 
  22.             System.out.println(e.getMessage());
  23.         }
  24.     }
  25. }

In the above example, we have used try-with-resources. It is used to close resources after completing try block. Now, you don’t need to close database connection explicitly.

Make sure you are using JDBC version 4.0 or higher and Java version 1.6 or higher.


RowSet 1.1

In earlier versions of Java, you have created instances of JdbcRowSet, CachedRowSet, FilteredRowSet etc by using JdbcRowSetImpl class.

Now, Java 7 has added a new RowSet 1.1. So, you can create instance of JdbcRowSet by using RowSetFactory interface.


Java CachedRowSet

Itstores (caches) data into memory so that is can perform operations on its own data rather than data stored in the database. It can operate without being connected to its data source, that why, it is also known as disconnectedRowSet.

Java JDBC Example: CachedRowSet

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import javax.sql.rowset.CachedRowSet;
  6. import javax.sql.rowset.RowSetProvider;
  7. class JdbcExample{
  8.     public static void main(String args[]) throws Exception{
  9.         try(// ————–try-with-resources begin————-//
  10.             // Creating connection
  11.             Connection con = DriverManager.getConnection( “jdbc:mysql://localhost:3306/student”,“root”,“mysql”);
  12.             // Creating statement
  13.             Statement stmt=con.createStatement();
  14.             // Executing query
  15.             ResultSet rs=stmt.executeQuery(“select * from user”);
  16.            )// ————–try-with-resources end————–//
  17.         { // —————-try block begin———————//
  18.             // Creating CachedRowSet
  19.             CachedRowSet cRS = RowSetProvider.newFactory().createCachedRowSet();
  20.             // Populating ResultSet data into CachedRowSet
  21.             cRS.populate(rs);
  22.             while(cRS.next()){
  23.                 System.out.println(cRS.getInt(1)+“”+cRS.getString(2)+“”+cRS.getString(3));
  24.             }
  25.         } // —————-try block end———————-//
  26.         catch(Exception e){ // Exception handler 
  27.             System.out.println(e);
  28.         }
  29.     }
  30. }

Java JdbcRowSet

It is an improvedResultSet object which is used to maintain connection to a data source. It is similar to ResultSet, but the big difference is that it provides set of properties and listener like a JavaBeans.The main purpose of JdbcRowSet is to make a ResultSet scrollable and updatable.

In the following example, we are creating instance of JdbcRowSet by using new approach.

Java JdbcRowSet Example 1

  1. import javax.sql.rowset.JdbcRowSet;
  2. import javax.sql.rowset.RowSetProvider;
  3. class JdbcExample{
  4.     public static void main(String args[]) throws Exception{
  5.         try(// ————–try-with-resources begin————-//
  6.             // Creating connection
  7.             JdbcRowSet jRS = RowSetProvider.newFactory().createJdbcRowSet();
  8.            )// ————–try-with-resources end————–//
  9.         { // —————-try block begin———————//
  10.             // Set database connection
  11.             jRS.setUrl(“jdbc:mysql://localhost:3306/student”);
  12.             // Set database username
  13.             jRS.setUsername(“root”);
  14.             // Set database password
  15.             jRS.setPassword(“mysql”);
  16.             // Set sql query to execute 
  17.             jRS.setCommand(“select * from user”);
  18.             // Execute query
  19.             jRS.execute();
  20.             while(jRS.next()){
  21.                 System.out.println(jRS.getInt(1)+“”+jRS.getString(2)+“”+jRS.getString(3));
  22.             }
  23.         } // —————-try block end———————-//
  24.         catch(Exception e){ // Exception handler 
  25.             System.out.println(e);
  26.         }
  27.     }
  28. }

Java JdbcRowSet Example: Updating Row

  1. import javax.sql.rowset.JdbcRowSet;
  2. import javax.sql.rowset.RowSetProvider;
  3. class JdbcExample{
  4.     public staticvoid main(String args[]) throws Exception{
  5.         try(// ————–try-with-resources begin————-//
  6.             // Creating connection
  7.             JdbcRowSet jRS = RowSetProvider.newFactory().createJdbcRowSet();
  8.            )// ————–try-with-resources end————–//
  9.         { // —————-try block begin———————//
  10.             // Set database connection
  11.             jRS.setUrl(“jdbc:mysql://localhost:3306/student”);
  12.             // Set database username
  13.             jRS.setUsername(“root”);
  14.             // Set database password
  15.             jRS.setPassword(“mysql”);
  16.             // Set sql query to execute 
  17.             jRS.setCommand(“select * from user”);
  18.             // Execute query
  19.             jRS.execute();
  20.             // Getting 3rd row because it is scrollable by default
  21.             jRS.absolute(3);
  22.             System.out.println(jRS.getInt(1)+“”+jRS.getString(2)+“”+jRS.getString(3));
  23.             // Updating 3rd row
  24.             jRS.updateString(“name”“Neraj Kumar Singh”);
  25.             jRS.updateRow();
  26.             // Fetching 3rd row again
  27.             System.out.println(jRS.getInt(1)+“”+jRS.getString(2)+“”+jRS.getString(3));
  28.         } // —————-try block end———————-//
  29.         catch(Exception e){ // Exception handler 
  30.             System.out.println(e);
  31.         }
  32.     }
  33. }

Output:

3  Neraj kumar  8562697858
3  Neraj Kumar Singh  8562697858
Next TopicAssertion in Java
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java 7 JDBC Improvements JDBC (Java Database Connectivity) provides universal data access from the Java programming language. You can access any data from database, spreadsheets or flat files by using JDBC. In Java 7, Java has introduced the following features: 1) It provides the ability to use a try-with-resources statement to automatically close resources of […]

Tags:

....
...