侧边栏壁纸
博主头像
侯秀荣

贪婪和恐惧是人性的两大弱点,
人类几万年,人性也没进步1厘米。

  • 累计撰写 172 篇文章
  • 累计收到 3 条评论

解决warning: CachedRowSetImpl is internal proprietary API and may be removed in a future release

2021-7-20 / 0 评论 / 2058 阅读

warning: CachedRowSetImpl is internal proprietary API and may be removed in a future release

            CachedRowSetImpl cachedRowSet = new CachedRowSetImpl()



Is this behavior wrong?

No, its a warning telling us that ChachedRowSetImpl does not belong to the public interface, therefore compatibility is not guaranteed.

Workaround

The following code snippet creates a CachedRowSet object by using a RowSetFactory which is created by the RowSetProvider:

RowSetFactory factory = RowSetProvider.newFactory(); CachedRowSet rowset = factory.createCachedRowSet();

This creates a CachedRowSet object from the implementation class com.sun.rowset.CachedRowSetImpl. It’s equivalent to the following statement:

CachedRowSet rowset = new com.sun.rowset.CachedRowSetImpl();

However, it’s recommended to create a CachedRowSet object from a RowSetFactory because the reference implementation may be changed in future.

本地测试运行结果:

import lombok.extern.slf4j.Slf4j;

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.sun.rowset.CachedRowSetImpl;

@Slf4j
public class TestCachedRowSet {

    private static Connection connection;

    private static String user = "root";

    private static String password = "root";

    private static String className = "com.mysql.jdbc.Driver";

    private static String url = "jdbc:mysql://localhost:3306/student";

    public static void main(String[] args) {
        try {
            Class.forName(className);
            connection = DriverManager.getConnection(url, user, password);
            String sql = "select * from patient";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.execute();
            ResultSet resultSet = preparedStatement.getResultSet();

            RowSetFactory rowSetFactory = RowSetProvider.newFactory();
            CachedRowSet cachedRowSet = rowSetFactory.createCachedRowSet();
            //CachedRowSetImpl cachedRowSet = new CachedRowSetImpl();
            cachedRowSet.populate(resultSet);

            resultSet.close();
            preparedStatement.close();
            connection.close();
            while (cachedRowSet.next()) {
                log.info("id:" + cachedRowSet.getString("id") + ",name:" + cachedRowSet.getString("name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 


评论一下?

OωO
取消