Where jdbc Rowsets are used? - java

There are some JDBC Rowsets like CachedRowSet, WebRowSet, FilteredRowSet and JoinRowSet. Does any bode know where they are used?
Ok, may be CachedRowSet is good where I do not want open and connection, may be WebRowSet good when I need insert some XML data ("may be" but I do not sure). But what about others?
Obviously, It is better for performance to write join in SQL query instead of create 2 JoinRowSet, take all data from they and join fields in java. The same about FilteredRowSet - it is more efficient to add where clause to the SQL query instead of grub a lot of the data and filtered it by java.
But somebody "invented" CachedRowSet, WebRowSet, FilteredRowSet and JoinRowSet why? Does anybody has some good experience about they usage?

The CachedRowSet interface defines the basic capabilities available to all disconnected RowSet objects. The other three are extensions of the CachedRowSet interface, which provide more specialized capabilities. The following information shows how they are related:
A CachedRowSet object has all the capabilities of a JdbcRowSet object plus it can also do the following:
Obtain a connection to a data source and execute a query.
Read the data from the resulting ResultSet object and populate itself with
that data.
Manipulate data and make changes to data while it is
disconnected.
Reconnect to the data source to write changes back to
it.
Check for conflicts with the data source and resolve those
conflicts
A WebRowSet object has all the capabilities of a CachedRowSet object plus it can also do the following:
Write itself as an XML document
Read an XML document that describes a WebRowSet object
A JoinRowSet object has all the capabilities of a WebRowSet object (and therefore also those of a CachedRowSet object) plus it can also do the following:
Form the equivalent of a SQL JOIN without having to connect to a data source
A FilteredRowSet object likewise has all the capabilities of a WebRowSet object (and therefore also a CachedRowSet object) plus it can also do the following:
Apply filtering criteria so that only selected data is visible. This is equivalent to executing a query on a RowSet object without having to use a query language or connect to a data source.

RowSet interface, the rows are retrieved from a JDBC data source, but a rowset may be customized so that its data can also be from a spreadsheet, a flat file, or any other data source with a tabular format.
Disconnected (not connected to the data source except when reading data from it or writing data to it)
CachedRowSet
JoinRowSet
FilteredRowSet
WebRowSet
Cached Rowset - being disconnected and able to operate without a driver, is designed to work especially well with a thin client for passing data in a distributed application or for making a result set scrollable and updatable
WebRowSet - the ability to read and write a rowset in XML format.
FilteredRowSet- used to the filtered subset of data from a rowset.
JoinRowSet -used to combine data from two different RowSet objects. This can be especially valuable when related data is stored in different data sources
Documentation

Not sure but this is what I think about FilteredRowSet. One can get the data from database by making connection in one shot. For example data for city, state and country. Later one can further subset the data with out going back to database in Java. Such as all records related to city or state or country or combination of them.

Related

Appropriate way to pass a dataset to Java from Oracle PL/SQL

I need to pass datasets from Oracle to Java through JDBC.
How is it better to organize it so that everything works well and it would be convenient both for Java developers and PL/SQL developers to maintain the code in case of changing, for example, table column types?
I see such variants:
Pass the sys_refcursor via stored procedure, and in Java expect that there will be certain fields with a certain type of data.
Pass a strong ref cursor and in Java do the same, that in item 1, but in the PL/SQL package there is a type description.
Pass SQL "table of" type, described at the schema level. If I understand correctly, in Java apparently it can somehow be applied to the object. The problem is that in these types it is impossible to do fields with the column type - Column_Name%TYPE.
Conduct in the PL/SQL package "table of object / record" type, and using JPublisher to work with it - JPublisher apparently converts it into a SQL type. It is not entirely clear for me how this is implemented, and what needs to be done for the same case when the data type of the column changes.
Using the pipelined function instead of the cursor (does this even make sense for such a task?).
What to choose? Or maybe something else, not from these points?
P.S. Sorry for bad English.
I'm not sure that i've understood your queston right, but i think you confused.
The variants, which you discribed is way to execute Java package on server side (for example, when you have database with application servers and want execute java package on it with data of database).
But if you thinking about JDBC then i guess that you want to make some java-app which could work with database. So that you don't have to user some sys_refcursor of subtupes like table of object / record. The JDBC provides capabilities to work with datasets using simple SQL. You should just connect to database as user (via JDBC) and execute sql query. After that you can get any data from result set.
Examples:
Connection example via JDBC
Execute select after connection
So the answer for your question depends on yours goals.

Insert populated Cachedrowset

I want to transfer data between databases over http and thought to use Cachedrowset since its already serialized.
How is the best way to insert a populated CachedRowset object into another database?
Is it possible at all?

Difference between resultset in java and cursor in mysql

When using java+mysql I came up with two methods to traverse through the datasets
Using cursor and stored procedure in sql and then execute it in java to find whether the user exist or not.
Using "resultSet" in java to find the user exist or not.
Can anyone tell me which is efficient to use and why?
"Cursor" is a SQL concept. ResultSet is a programming type in Java. "Cursor" uses database semantics, and is accessed via SQL. ResultSet uses type/object semantics and is accessed via Java. "Cursor" directly controls how much data is fetched by a SQL query. ResultSet generally does not, but does control how much data is exposed to the Java program. There may or may not be more data fetched by the SQL query than shown in a ResultSet.
SQL structures like "cursor" should only be used in the data context. It is part of the database. It will not stop you from needing a ResultSet when you use JDBC to query the data. ResultSet is how the Java code interacts with the query results.
"Cursor" is used for data operations. It is something that only data manipulation cares about. ResultSet is more evanescent, needed by Java code in the moment of retrieving query data. It is something only your client code cares about. The database has no awareness of ResultSet. Under nearly all circumstances, your Java code has no awareness of cursors.
You should avoid the best you can stored procedures as they are much more complex to maintain than a Java application and they are specific to the target database. You should only use stored procedures for complex use cases that cannot be managed with queries executed thanks to a Statement/PreparedStatement.
In other words 99.9 % of the time, your best choice will be #2.

how to connect multiple databases on a single server with JDBC?

I'm trying to access data in multiple databases on a single running instance. the table structures of these databases are all the same; As far as I know, create a new connnection using jdbc is very expensive. But the connection string of jdbc require format like this
jdbc:mysql://hostname/ databaseName, which needs to specify a specific database.
So I'm wondering is there any way to query data in multiple databases using one connection?
The MySQL documentation is badly written on this topic.
The SELECT Syntax page refers to the JOIN Syntax page for how a table name can be written, even if you don't use JOIN clauses. The JOIN Syntax page simply says tbl_name, without further defining what that is. There is even a comment at the bottom calling this out:
This page needs to make it explicit that a table reference can be of the form schema_name.tbl_name, and that joins between databases are therefore posible.
The Schema Object Names page says nothing about qualifying names, but does have a sub-page called Identifier Qualifiers, which says that a table column can be referred to using the syntax db_name.tbl_name.col_name. The page says nothing about the ability to refer to tables using db_name.tbl_name.
But, if you can refer to a column using db_name.tbl_name.col_name, it would only make sense if you can also refer to a table using db_name.tbl_name, which means that you can access all your databases using a single Connection, if you're ok with having to qualify the table names in the SQL statements.
As mentioned by #MarkRotteveel in a comment, you can also switch database using the Connection.setCatalog(String catalog) method.
This is documented in the MySQL Connector/J 5.1 Developer Guide:
Initial Database for Connection
If the database is not specified, the connection is made with no default database. In this case, either call the setCatalog() method on the Connection instance, or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.
Note: Always use the Connection.setCatalog() method to specify the desired database in JDBC applications, rather than the USE database statement.

Joining multiple result set

I am trying to develop a Java application which merges data from multiple data source basically RDBMS. The scenario is some thing like this.
I have creates a connection to two data sources, lets say a MSSQL database and other Oracle. Also on each connection a user can create a DataObject( a Java object) which contains a SQL query and a connection. The query is executed on the connection and result are displayed.
Now what I want is that my user can join and filter result obtained from multiple DataObject.
Currently I am looking on the following solution:
JDO/Hibernate - I will create a object from the ResultSet obtained from the query execution and will use the multiple objects with filter and joining condition.
Java RowSet - I will create a RowSet object over result sets and user JoinRowSet and FilteredRowSet to join multiple result set.
Please advice me on my choice. Also please can other solution be looked into.
I would suggest the former. To me its as simple as getting the list of entities, and add those in a single list, based on some filter.
Oracle comes with a generic ODBC gateway that allows you to link the oracle database with another database, so you can join tables from both databases etc. with SQL, as if both tables were on Oracle. See this link for details. By doing that, you don't have to replicate database features in your java program.

Categories