Home > Database > Mysql Tutorial > Explain what is RowSet object using JDBC program?

Explain what is RowSet object using JDBC program?

WBOY
Release: 2023-09-10 11:21:07
forward
1114 people have browsed it

使用 JDBC 程序解释什么是 RowSet 对象?

RowSet is a wrapper around the ResultSet object. It can connect to and disconnect from the database and can be serialized. It maintains JavaBean components by setting properties. You can pass RowSet objects over the network. By default, RowSet objects are scrollable and updateable, it is used to make ResultSet objects scrollable and updateable.

You can use the

RowSetProvider.newFactory( ).createJdbcRowSet() method.

Example

Suppose we have a table named dataset in the database:

+--------------+-----------+
| mobile_brand | unit_sale |
+--------------+-----------+
| Iphone       |      3000 |
| Samsung      |      4000 |
| Nokia        |      5000 |
| Vivo         |      1500 |
| Oppo         |       900 |
| MI           |      6400 |
| MotoG        |      4360 |
| Lenovo       |      4100 |
| RedMi        |      4000 |
| MotoG        |      4360 |
| OnePlus      |      6334 |
+--------------+-----------+
Copy after login

The following JDBC example creates a RowSet object and uses the object to retrieve a table named dataset Table contents:

import java.sql.DriverManager;
import javax.sql.RowSet;
import javax.sql.rowset.RowSetProvider;
public class RowSetExample {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Creating the RowSet object
      RowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
      //Setting the URL
      String mysqlUrl = "jdbc:mysql://localhost/TestDB";
      rowSet.setUrl(mysqlUrl);
      //Setting the user name
      rowSet.setUsername("root");
      //Setting the password
      rowSet.setPassword("password");
      //Setting the query/command
      rowSet.setCommand("select * from Dataset");
      System.out.println("Contents of the table");
      while(rowSet.next()) {
         System.out.print("Brand: "+rowSet.getString(1)+", ");
         System.out.print("Sale: "+rowSet.getString(2));
         System.out.println("");
      }
   }
}
Copy after login

Output

Contents of the table
Brand: Iphone, Sale: 3000
Brand: Samsung, Sale: 4000
Brand: Nokia, Sale: 5000
Brand: Vivo, Sale: 1500
Brand: Oppo, Sale: 900
Brand: MI, Sale: 6400
Brand: MotoG, Sale: 4360
Brand: Lenovo, Sale: 4100
Brand: RedMi, Sale: 4000
Brand: MotoG, Sale: 4360
Brand: OnePlus, Sale: 6334
Copy after login

The above is the detailed content of Explain what is RowSet object using JDBC program?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template