1. Operation not allowed after ResultSet closed solution
The reason given:
Operation not allowed after ResultSet closed
After translation means, ResultSet
do not allow the operation after closing , which means that in the ResultSet
instance of the call close()
after method, once again using the example.
Solutions:
- View error at
ResultSet
whether the instance has been called close () method to close - Error at
ResultSet
whether other examples andResultSet
examples are from the sameConnection
instance, that aConnection
instance after executing different return after the SQL statement is different from theResultSet
example. See if the Connection instance simultaneously execute different SQL statement returnsResultSet
examples
Solution:
-
First check
ResultSet
whether the instance has been called aclose()
method thatrs.close()
. -
If the
ResultSet
examples do not call theclose()
method, check that generated theResultSet
instanceConnection
instance, thatConnection
if the instance is also executed other SQL statements. If you see thatConnection
if 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 plussynchronized
keywords so thatConnection
instances when executing a SQL statement, letConnection
instance while executing other SQL statements, because a Connection instance can Statement or PreparedStatement instances corresponding to a plurality of instances, but anStatement
instance 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. -
If the
ResultSet
instance and does not call aclose()
method, but do not think thisConnection
instance is not running too many SQL statements,debug
seeStatement
examples orPreparedStatement
whether instances simultaneously into multiple SQL statements, or printed in the consoleStatement
instance orPreparedStatement
SQL statement instance executed.Print Console
Statement
instance orPreparedStatement
instances 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语句不相同
- 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
ResultSet
theclose()
method - If not, then see
ResultSet
whether the instance has only oneStatement
instance orPreparedStatement
instances - 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 Data
After the literal meaning ResultSet
from the update (add, delete, modify the statement). no data. That Result
instance 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 rs
there is no data, so call the next()
method error.
Solutions:
- Check the SQL statement is correct
- Use
execute
andgetResultSet
method - View created
ResultSet
if an instance of the code in question, and a chase inside a reason
Solution:
- Check the SQL statement executed only after the query does not return ResultSet
- Using the execute method and getResultSet
- 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
synchronized
keywords. - 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 found
Xxx 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:
- Check the SQL statement is correct, for example
select name from user;
, in Java it isrs.getString("password");
, orselect nam from user;
, in Javars.getString("name");
carefully check the modifications. debug
EnterPreparedStatement或Statement
, print or consolePreparedStatement
orStatement
compiled 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 PreparedStatement
instances, 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 PreparedStatement
block instance add synchronize
keywords synchronization.
- 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
->ResultSet
place, 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