Operation not allowed after ResultSet closed, ResultSet is from UPDATE. No Data and Column 'xxx' not found the solution being given

1. Operation not allowed after ResultSet closed solution

The reason given:

Operation not allowed after ResultSet closedAfter translation means, ResultSetdo not allow the operation after closing , which means that in the ResultSetinstance of the call close()after method, once again using the example.

Solutions:

  • View error at ResultSetwhether the instance has been called close () method to close
  • Error at ResultSetwhether other examples and ResultSetexamples are from the sameConnection instance, that a Connectioninstance after executing different return after the SQL statement is different from the ResultSetexample. See if the Connection instance simultaneously execute different SQL statement returns ResultSetexamples

Solution:

  1. First check ResultSetwhether the instance has been called a close()method that rs.close().

  2. If the ResultSetexamples do not call the close()method, check that generated the ResultSetinstance Connectioninstance, that Connectionif the instance is also executed other SQL statements. If you see that Connectionif an instance simultaneously execute multiple SQL statements. If simultaneously executed multiple SQL statements, you need to get connected to the database processes to execute SQL statements plus synchronizedkeywords so that Connectioninstances when executing a SQL statement, let Connectioninstance while executing other SQL statements, because a Connection instance can Statement or PreparedStatement instances corresponding to a plurality of instances, but an Statementinstance or instances PreparedStatement ResultSet corresponds to only one example. If the same instance of a Connection to perform different SQL statement Statement instance with the same or PreparedStatement instance, two SQL statements generated a ResultSet instance.

  3. If the ResultSetinstance and does not call a close()method, but do not think this Connectioninstance is not running too many SQL statements, debugsee Statementexamples or PreparedStatementwhether instances simultaneously into multiple SQL statements, or printed in the console Statementinstance or PreparedStatementSQL statement instance executed.

    Print Console Statementinstance or PreparedStatementinstances execute SQL statements code is as follows:

    /***
     * 执行查询的sql语句,并返回结果集
     * @param sql       sql语句
     * @param objects   替代占位符的数组
     * @return ResultSet结果集
     */
    public static ResultSet executeQuery(String sql, Object... objects) {
        System.out.println("sql ->" + sql);
        connection = getConnection();
        System.out.println("connection->" + connection);
        try {
            ppstmt = connection.prepareStatement(sql);
            System.out.println(sql + " ppstm1->" + ppstmt);
    
            if (objects != null && objects.length > 0) {
                for (int i = 0; i < objects.length; i++) {
                    ppstmt.setObject(i + 1, objects[i]);
                }
            }
            rs = ppstmt.executeQuery();
            System.out.println(sql + " ppstm->" + ppstmt);
        } catch (SQLException e) {
            System.out.println("SQL语句错误或参数个数与占位符不一致");
            e.printStackTrace();
            return rs;
        }
        return rs;
    }
    

    Screenshot of error:

   可以看到SQL语句和预编译后的SQ语句不相同
  1. If the global static variables defined in the JDBC tools Connection, PreparedStatement or ResultSet, will have to consider thread safety issues , the two issues may occur, will likely be thread safe place can be synchronized.

to sum up:

  • First see if the manual had to call ResultSetthe close()method
  • If not, then see ResultSetwhether the instance has only oneStatement instance or PreparedStatementinstances
  • If the definition of a global static variables, consider thread safety issues

2. ResultSet is from UPDATE. No Data solutions

The reason given:

ResultSet is from UPDATE. No DataAfter the literal meaning ResultSetfrom the update (add, delete, modify the statement). no data. That Resultinstance may be additions and deletions to the implementation of the SQL statement (the SQL statement is not a query), or a query but ResultSet examples of call next()no data method that while(rs.next())the rsthere is no data, so call the next()method error.

Solutions:

  • Check the SQL statement is correct
  • Use executeand getResultSetmethod
  • View created ResultSetif an instance of the code in question, and a chase inside a reason

Solution:

  1. Check the SQL statement executed only after the query does not return ResultSet
  2. Using the execute method and getResultSet
  3. If the SQL statement is correct, that can execute SQL statements in the database, but Java, but it's not, then catch up or PreparedStatement Statement. debug console to enter or in print incoming SQL statements and compiled SQL statements are the same, if not identical, are at the same time into multiple SQL statements, consider thread safety issues. You need to be synchronized code block add synchronizedkeywords.
  4. If the global static variables defined in the JDBC tools Connection, PreparedStatement or ResultSet, will have to consider the security thread , the thread may appear safe place synchronization can be.

3. Column 'xxx' not found solution

The reason given:

Column 'xxx' not foundXxx literal meaning is not found in this column, that is to say, the results of the query, there is no column for the field.

Solutions:

  • Check the SQL statement is correct
  • After checking whether the compiled SQL statements and SQL statements expected the same

Solution:

  1. Check the SQL statement is correct, for example select name from user;, in Java it is rs.getString("password");, or select nam from user;, in Java rs.getString("name");carefully check the modifications.
  2. debugEnter PreparedStatement或Statement, print or console PreparedStatementor Statementcompiled SQL statements and SQL statements is the same as the incoming

Screenshot error:

The figure shows the different incoming SQL statements and SQL statements compiled

Look at the code given at the

/**
 * 查找所有学生
 *
 * @return 学生集合
 */
@Override
public ArrayList<Student> selectAllStudent() {
    String sql = "SELECT * FROM db_studentinfo";
    ArrayList<Student> studentList = null;
    ResultSet rs = JDBCUtil.executeQuery(sql);
    try {
        studentList = new ArrayList<Student>();
        while (rs.next()) {
            studentList.add(setStudent(rs));
        }
    } catch (SQLException e) {
        e.printStackTrace();
        return studentList;
    } finally {
        JDBCUtil.closeDB();
    }
    return studentList;
}

// 将查询的结果集放入学生对象中
private Student setStudent(ResultSet rs) {
    Student student = null;
    try {
        student = new Student();
        student.setStudentNum(rs.getInt("学生学号"));
        student.setStudentName(rs.getString("学生姓名"));
        student.setGrade(rs.getString("年级"));
        student.setStudentClass(rs.getString("班级"));
        student.setSex(rs.getString("性别"));
        student.setAge(rs.getInt("年龄"));
        student.setAddress(rs.getString("家庭住址"));
        student.setPhone(rs.getString("联系电话"));
    } catch (SQLException e) {
        e.printStackTrace();
        return student;
    }
    return student;
}

Because at this time are two SQL statements simultaneously into the PreparedStatementinstances, so although the incoming SQL statement is correct, but because of other SQL statements also entered, resulting query returns a result set is not what we initially passed in SQL the result set of the statement, it will be reported

Column '学生学号' not found.mistake. Investigation and the reasons from the thread safety , such as the possible cause two SQL statements simultaneously into the PreparedStatementblock instance add synchronizekeywords synchronization.

  1. If the global static variables defined in the JDBC tools Connection, PreparedStatement or ResultSet, will have to consider the security thread , the thread may appear safe place synchronization can be.

to sum up

  • Check the SQL statement is correct
  • Clarify the link 加载JDBC驱动-> Connection-> PreparedStatement-> ResultSetplace, the first thought most likely to go wrong, go break point debug console or print variables that can go wrong
  • If you do not use the database connection pool, JDBC tools if there is a static variable, thread-safety issues to be considered, the database connection pool can make use of the database connection pool

Guess you like

Origin www.cnblogs.com/windowsxpxp/p/12616158.html