解决warning: CachedRowSetImpl is internal proprietary API and may be removed in a future release
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();
}
}
}
本文共计 3345 字,感谢您的耐心浏览与评论。
0条回应:“解决warning: CachedRowSetImpl is internal proprietary API and may be removed in a future release”