我有以下使用JDBC从数据库获取数据的方法:

public List<QueryHolder> getData(String Name) throws SQLException {
    OracleDataSource ds;
    ds = new OracleDataSource();

    String url="jdbc:oracle:thin:@192.***.*.**:1521/dbg";
    ds.setURL(url);
    Connection conn = null;
    conn=ds.getConnection("name","password");

    SQL_Str = "SELECT ASM4EVLENMEEHLIYETBELGESI.DAMATSOYAD,ASM4EVLENMEEHLIYETBELGESI.DAMATBABAADI,ASM4EVLENMEEHLIYETBELGESI.DAMATANNEADI FROM ASM4EVLENMEEHLIYETBELGESI where DAMATSOYAD like ('%"
            + Name + "%')";
    Statement ps = conn.createStatement();
    if (ps == null)
        throw new SQLException("Can't get data source");
    rs = ps.executeQuery(SQL_Str);
    ps.close();
    if (con != null) {
        con.close();
    }

    if (rs == null)
        throw new SQLException("Can't get data source");
    List<QueryHolder> list = new ArrayList<QueryHolder>();
    while (rs.next()) {                                 // <-- This is line 89.
        QueryHolder que = new QueryHolder();

        que.setSoyad(rs.getString("DAMATSOYAD"));
        que.setBabaadi(rs.getString("DAMATANNEADI"));
        que.setAnneadi(rs.getString("DAMATBABAADI"));

        list.add(que);
    }
    return list;
}


但是,当我运行此方法时,它将引发以下异常:

java.sql.SQLException: Closed statement: next
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
    oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:187)
    Que.Query.getData(Query.java:89)
    ...


这是怎么引起的,我该如何解决?

最佳答案

正确的结单顺序:

应该执行以下步骤(按顺序)


结果集
准备声明
连接。



  编辑代码


Statement ps = conn.createStatement();
...
rs = ps.executeQuery(SQL_Str);
...

while (rs.next()) {                                 // <-- This is line 89.
    ...
}

ps.close();
con.close();
return list;

关于java - java.sql.SQLException:闭合语句:next,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24693153/

10-12 06:02