In standard Java Database Connectivity (JDBC), working with standard ResultSet objects often requires manually opening connections, managing statements, and tracking resource life cycles. A JDBCRowSet wraps a standard ResultSet into a JavaBean component, making database handling flexible, easy to configure, and scrollable by default.
Key Steps to Create and Use JDBCRowSet
To implement a JdbcRowSet in your Java application, you follow these straightforward steps:
Obtain the RowSetProvider: Call RowSetProvider.newFactory() to obtain an instance of RowSetFactory.
Create JdbcRowSet: Use rowSetFactory.createJdbcRowSet() to get your row set instance.
Configure Connection Parameters: Set the database connection URL, username, and password using dedicated setter methods.
Define Query & Parameters: Set the SQL statement via setCommand() and bind query placeholders with setters like setString().
Execute and Process: Trigger execute() to load the records, iterate through the row set, and access column values smoothly.
Complete Java Code Example
import java.sql.SQLException;
import java.util.Scanner;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
public class JdbcRowSetDemo {
public static void main(String[] args) {
JdbcRowSetDemo demo = new JdbcRowSetDemo();
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter Country Code (e.g., IND, USA): ");
String countryCode = scanner.next();
demo.getCityInformation(countryCode);
} catch (Exception e) {
e.printStackTrace();
}
}
public void getCityInformation(String countryCode) {
try {
// 1. Create RowSetFactory
RowSetFactory factory = RowSetProvider.newFactory();
// 2. Create JdbcRowSet instance
try (JdbcRowSet jdbcRowSet = factory.createJdbcRowSet()) {
// 3. Set connection properties
jdbcRowSet.setUrl("jdbc:mysql://localhost:3306/world");
jdbcRowSet.setUsername("root");
jdbcRowSet.setPassword("password");
// 4. Set SQL query command and bind parameters
jdbcRowSet.setCommand("SELECT ID, Name, CountryCode, District, Population FROM city WHERE CountryCode = ?");
jdbcRowSet.setString(1, countryCode);
// 5. Execute query
jdbcRowSet.execute();
// 6. Iterate through the records
while (jdbcRowSet.next()) {
System.out.println("ID: " + jdbcRowSet.getInt("ID"));
System.out.println("Name: " + jdbcRowSet.getString("Name"));
System.out.println("CountryCode: " + jdbcRowSet.getString("CountryCode"));
System.out.println("District: " + jdbcRowSet.getString("District"));
System.out.println("Population: " + jdbcRowSet.getInt("Population"));
System.out.println("------------------------------------------");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.SQLException;
import java.util.Scanner;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
public class JDBCRowSetDemo
{
// JDBC driver name and database URL
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL="jdbc:mysql://localhost:3306/world";
// Database credentials
static final String USERNAME="root";
static final String PASSWORD="root";
public static void main( String[] args )
{
JDBCRowSetDemo jdbcRowSetDemo =new JDBCRowSetDemo();
Scanner scanner =new Scanner(System.in);
while( true )
{
System.out.print("Enter City Country Code :");
String cityCountrycode = scanner.nextLine();
if( cityCountrycode.equals("exit") )
{
break;
}
jdbcRowSetDemo.getCityInformation(cityCountrycode);
}
scanner.close();
}
private void getCityInformation( String cityCountrycode )
{
JdbcRowSet jdbcRowSet =null;
try
{
/*
* Using RowSetFactory create the JdbcRowSet object.
*/
RowSetFactory rowSetFactory = RowSetProvider.newFactory();
jdbcRowSet = rowSetFactory.createJdbcRowSet();
/*
* Set the JdbcRowSet properties [URL,username,password,command]
*/
jdbcRowSet.setUrl(DB_URL);
jdbcRowSet.setUsername(USERNAME);
jdbcRowSet.setPassword(PASSWORD);
/*
* Sets the command property with a query that produces a ResultSet
* object containing all the data in the table
*/
jdbcRowSet.setCommand("select * from city where countrycode=?");
jdbcRowSet.setString(1, cityCountrycode);
/*
* The execute method does many things for you in the background:
*
* 1.It makes a connection to the database using the values you assigned to the url,
* username, and password properties.
*
* 2.It executes the query you set in the command property.
*
* 3.It reads the data from the resulting ResultSet object into the jdbcRs object.
*
*/
jdbcRowSet.execute();
/*
* Iterate the jdbcRowSet and get each row information
* of city table.
*/while( jdbcRowSet.next() )
{
int id = jdbcRowSet.getInt(1);
String name = jdbcRowSet.getString(2);
String countryCode = jdbcRowSet.getString(3);
String district = jdbcRowSet.getString(4);
int population = jdbcRowSet.getInt(5);
/*
* Display values
*/
System.out.print("ID: "+ id);
System.out.print(", Name: "+ name);
System.out.print(", CountryCode: "+ countryCode);
System.out.print(", District: "+ district);
System.out.println(", Population: "+ population);
}
}
catch( SQLException se )
{
se.printStackTrace();
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
if(jdbcRowSet!=null)
{
try
{
jdbcRowSet.close();
}
catch( SQLException e )
{
e.printStackTrace();
}
}
}
}
}
Output
Enter City Country Code :ind
ID:1024, Name: Mumbai (Bombay), CountryCode:IND, District: Maharashtra, Population:10500000ID:1025, Name: Delhi, CountryCode:IND, District: Delhi, Population:7206704ID:1026, Name: Calcutta [Kolkata], CountryCode:IND, District: West Bengali, Population:4399819ID:1027, Name: Chennai (Madras), CountryCode:IND, District: Tamil Nadu, Population:3841396------
Enter City Country Code :pak
ID:2822, Name: Karachi, CountryCode:PAK, District: Sindh, Population:9269265ID:2823, Name: Lahore, CountryCode:PAK, District: Punjab, Population:5063499ID:2824, Name: Faisalabad, CountryCode:PAK, District: Punjab, Population:1977246ID:2825, Name: Rawalpindi, CountryCode:PAK, District: Punjab, Population:1406214------
Enter City Country Code :exit
Environment Used
JDK version : 1.7.0_51
Mysql Server version : 5.6.19
To Download /JDBCRowSet_Read_DemoApp Project Click the below link
The Type 1 JDBC Driver, commonly known as the JDBC-ODBC Bridge Driver, acts as a translator between Java's JDBC calls and Open Database Connectivity (ODBC) function calls.
During the early days of Java, native JDBC drivers were not available for every database. The Type 1 driver bridged this gap by translating JDBC API calls into native ODBC calls, allowing Java applications to communicate with any database that provided an ODBC driver.
How the Architecture Works
The execution flow follows a sequential bridge path across layers:
Java Application: Issues standard JDBC calls via java.sql interfaces.
JDBC-ODBC Bridge: Translates standard JDBC method calls into corresponding ODBC function calls using native methods.
ODBC Driver & Database Client: The client-side ODBC driver processes the queries and routes them to the database server.
Database: Executes SQL statements and returns result sets back through the bridge layers.
Advantages and Limitations
Broad Database Support (Historic): Allowed Java applications to connect easily to legacy databases that only supported ODBC (e.g., MS Access).
Platform Dependency: Requires ODBC configuration and native client binaries installed on every client machine, breaking pure Java portability.
Performance Overhead: Dual-layer translation (JDBC to ODBC and ODBC to database format) degrades execution speed.
Deprecation: The JDBC-ODBC Bridge was deprecated in Java 7 and completely removed starting from Java 8 in favor of pure Java Type 4 drivers.
The PreparedStatement interface extends Statement and represents a precompiled SQL statement. Instead of constructing SQL strings by concatenating values directly, PreparedStatement uses placeholders (?) for parameterized queries.
Because the database compiles the query structure once and reuses the execution plan with different parameters, it offers superior performance for repeated queries and provides strong defense against SQL injection attacks.
Complete Java Code Implementation
Here is a practical example demonstrating how to insert records using PreparedStatement: