Search code examples
javajdbcresultsetrowset

How RowSet works java?


I am trying to understand the internal workings of RowSet.

How does it set the properties data and create a connection?

RowSetFactory factory = RowSetProvider.newFactory();

JdbcRowSet jdbcRowSet = factory.createJdbcRowSet(); 

jdbcRowSet.setUrl( … );
jdbcRowSet.setUsername( … );
jdbcRowSet.setPassword( … );
jdbcRowSet.setCommand( … );

jdbcRowSet.execute();

Solution

  • The easiest way is to look at the default implementation com.sun.rowset.JdbcRowSetImpl.

    This class has a method connect() that establishes the connection.

    It does this in one of three ways:

    • if the object was initialized with an SQL connection it returns that connection
    • if the object has a datasource name it uses that name to lookup a datasource
    • if the object has an url set it uses the java.sql.DriverManager to establish a connection to that url

    No magic here, it does the same things that you would do if you were to open a connection yourself.