javajavadb

try-with-resources gives unexpected incompatibility errors


Executing javac JdbcRowSet.java on the following excerpt confuses me:

//
import javax.sql.*;
import javax.sql.rowset.*;
import java.sql.*;

class JdbcRowSet
{
    public static void main(String ... args){
        StringBuilder sb = new StringBuilder();

        try(JdbcRowSet jrs = RowSetProvider.newFactory().createJdbcRowSet()){
            //CODE
}
//

According to JavaDoc:

public interface ResultSet extends Wrapper, AutoCloseable

public interface RowSet extends ResultSet

public interface JdbcRowSet extends RowSet, Joinable

I got the following errors:

dbcRowSet.java:8:error: incompatible types: javax.sql.rowset.JdbcRowSet cannot be converted to JdbcRowSet try(//)
JdbcRowSet.java:8:error: incompatible types: try-with-resources not applicable to variable type
JdbcRowSet cannot be converted to AutoCloseable.

The errors are strangely confusing. Can someone help me on this. Thank you.


Solution

  • As @Kayaman correctly pointed out in his comment - you are declaring your own JdbcRowSet class.

    Either change the name of the class or fully qualify the object in the try.

    try(javax.sql.rowset.JdbcRowSet jrs = RowSetProvider.newFactory().createJdbcRowSet())