JAVA ERROR : package com.sun.rowset is not visible : com.sun.rowset is declared in module java.sql.rowset, which does not export it

JAVA ERROR : package com.sun.rowset is not visible : com.sun.rowset is declared in module java.sql.rowset, which does not export it

我只是尝试 运行 这个代码:

import com.sun.rowset.CachedRowSetImpl;

public class Test {
    public static void main(String[] args) throws Exception{
        CachedRowSetImpl crs = new CachedRowSetImpl();
    }
}

当我 运行 它时,我得到:

Error:(1, 15) java: package com.sun.rowset is not visible (package com.sun.rowset is declared in module java.sql.rowset, which does not export it)

我正在使用 IntelliJ,我尝试导入 rs2xml.jar,但仍然无济于事。

有了 Java 9,您将无法再访问此 class。并且以理想的方式你不应该那样做。那是因为这个class的包没有在模块javax.sql.rowset中导出。 Java-9 中正确的做法是:

import javax.sql.rowset.*; 

public class Test {
    public static void main(String[] args) throws Exception {

        CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();
    }
}

要了解我们可以转到模块描述 (module-info.java) 并找到导出包的列表:

exports javax.sql.rowset;
exports javax.sql.rowset.serial;
exports javax.sql.rowset.spi;

这应该与 Java 10

一起使用

而不是

CachedRowSet crs = new CachedRowSetImpl();

使用

CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();

除了这里的答案,重要的是要注意你应该永远不要使用com.sun.rowset.CachedRowSetImpl,即使在Java8.

Are there any good CachedRowSet implementations other than the Sun one? 所述,RowSetProvider 是获得 CachedRowSet.

的标准方法

来自 sun 的包是 internal and subject to change。除了 JDK 开发人员外,绝不能使用它们。