gpt4 book ai didi

java - Db中正确写入,但Sql时出现 "Illegal operation on empty result set."错误

转载 作者:行者123 更新时间:2023-11-29 13:35:14 28 4
gpt4 key购买 nike

我正在 SQL 数据库上进行INSERT。奇怪的事情发生了:当我运行 downer 代码时,数据库写入正确,但编译器给我一个错误

java.sql.SQLException:对空结果集进行非法操作。

我还没有搜索过它,我发现这可能是由于 JDBC 驱动程序的错误(但我不这么认为)。所以我决定在这里发帖。

首先我给你数据库表:

表格主题

+------------------+----------------+------------+
| subjectCode | SubjectName | teacherCode|
+------------------+----------------+------------+
| 1 | Maths | 10 |
| 2 | English | 20 |
| 3 | Greek | 30 |
+------------------+----------------+------------+

表类

+------------------+----------------+
| ClassCode | ClassName |
+------------------+----------------+
| 1 | Class1 |
| 2 | Class2 |
| 3 | Class3 |
+------------------+----------------+

表格类(class)(行和列是时间表的行(小时)和列(天),明确确定时间表中的类(class);例如:小时 1 是一天的第一个小时)

+------------------+----------------+------------+-----------+-------+-------+
| LessonCode | SubjectCode | teacherCode| ClassCode | Day | Hour |
+------------------+----------------+------------+-----------+-------+-------+
| 1 | James | 10 | 1 | 1 | 1 |
| 2 | John | 20 | 2 | 1 | 2 |
| 3 | Frank | 30 | 3 | 1 | 3 |
+------------------+----------------+------------+-----------+-------+-------+

代码:

private void writeLesson(String subjectName, String className, int column, int row)
{
try{
int subjCode;
String query="SELECT subjectCode from subject WHERE subjectName='"+subjectName+"';";
rs = st.executeQuery(query);
rs.next();
subjCode = rs.getInt("subjectCode");


int teacherCode;
String query1="SELECT teacherCode from subject WHERE subjectName='"+subjectName+"';";
rs = st.executeQuery(query1);
rs.next();
teacherCode = rs.getInt("teacherCode");

int classCode;
String query2="SELECT classCode from class WHERE className='"+className+"';";
rs = st.executeQuery(query2);
rs.next();
ClassCode = rs.getInt("classCode");


String query3 ="INSERT INTO lesson (subjCode, classCode, teacherCode, day, hour) "
+ "VALUES ('" + subjCode + "',"
+ "'" + classCode + "',"
+ "'" + teacherCode + "',"
+ "'" + column + "',"
+ "'" + row + "');";
PreparedStatement statement=con.prepareStatement(query3);
statement.executeUpdate();
statement.close();
}

catch (Exception ex){
System.out.println("Error db: "+ex);

重要:方法writeLesson位于循环中。我已经不在循环中测试了这段代码并且它有效。您认为为什么?

解决方案:问题是在方法 writeLesson 的循环中,我向它传递了“空白”字符串,导致了错误(这就是为什么在 DB 上写入有效,但有错误)。

最佳答案

您是否检查了 select 语句的结果集?其中不是有一个是空的吗?如果 rs 为空,rs.getInt() 将抛出此异常(在这种情况下 rs.next() 返回 false)

关于java - Db中正确写入,但Sql时出现 "Illegal operation on empty result set."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18838409/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com