我正在为大学做作业使用netbeans中的一个查询到一个ms访问的数据库,这让我花了两天时间才抓到目前为止。问题是,只要打开包装并运行它,它就会给我一个可爱的结果。在我收到此错误之后:
net.ucanaccess.jdbc.UcanaccessSQLException:游标状态无效:标识符游标未定位在UPDATE,DELETE,SET或GET语句中的行上:; ResultSet为空。
我很感激你能给予的任何帮助。 非常感谢。
private void FindFlghtBtnActionPerformed(java.awt.event.ActionEvent evt) {
try{
rs = stmt.executeQuery("SELECT DepAirportTable.AirportName, DepAirportTable.AirportDesignaton, FlightTable.DepDate, ArrAirportTable.ArrAirportName, ArrAirportTable.ArrAirportDesignaton\n" +
"FROM PlaneTable INNER JOIN (ArrAirportTable INNER JOIN (DepAirportTable INNER JOIN FlightTable ON DepAirportTable.AirportDesignaton = FlightTable.FromAirDes) ON ArrAirportTable.ArrAirportDesignaton = FlightTable.ToAirDes) ON PlaneTable.FlightDesignationCode = FlightTable.[FlightDesignationCode]\n" +
"WHERE (((DepAirportTable.AirportName)=\""+DepCombo.getSelectedItem()+"\") AND ((FlightTable.DepDate)=#"+((JTextField)DateDep.getDateEditor().getUiComponent()).getText()+"#) AND ((ArrAirportTable.ArrAirportName)=\""+ArrComboBox.getSelectedItem()+"\"));");
rs.next();
{
departure = rs.getString("AirportName");
depDate = rs.getString("DepDate");
arrival = rs.getString("ArrAirportName");
BookingPrevTa.setText("Your flight details are:\nDeparting from "+departure+" arriving at: "+arrival+" departing on: "+depDate);
rs.close();
}
}
catch (SQLException ee)
{
ee.printStackTrace();
}
}
答案 0 :(得分:1)
您的问题是,您没有检查next()
的返回值,当它返回false
时,您会收到类似的异常。
以下是代码的清理版本,它假定您将数据库连接作为名为conn
的字段。根据需要调整日期格式。
改进是:
next()
返回值的检查(原始问题的修复) PreparedStatement
来防止SQL Injection个漏洞PreparedStatement
和ResultSet
)关闭,永不留下悬空JOIN
条款以便于使用this.
添加到字段引用中,以提高清晰度并防止隐藏String sql = "SELECT DepAirportTable.AirportName" +
", DepAirportTable.AirportDesignaton" +
", FlightTable.DepDate" +
", ArrAirportTable.ArrAirportName" +
", ArrAirportTable.ArrAirportDesignaton" +
" FROM FlightTable" +
" JOIN PlaneTable ON PlaneTable.FlightDesignationCode = FlightTable.FlightDesignationCode" +
" JOIN DepAirportTable ON DepAirportTable.AirportDesignaton = FlightTable.FromAirDes" +
" JOIN ArrAirportTable ON ArrAirportTable.ArrAirportDesignaton = FlightTable.ToAirDes" +
" WHERE DepAirportTable.AirportName = ?" +
" AND FlightTable.DepDate = ?" +
" AND ArrAirportTable.ArrAirportName = ?";
try (PreparedStatement stmt = this.conn.prepareStatement(sql)) {
String dateStr = ((JTextField)DateDep.getDateEditor().getUiComponent()).getText();
java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
stmt.setString(1, DepCombo.getSelectedItem());
stmt.setDate (2, new java.sql.Date(date.getTime()));
stmt.setString(3, ArrComboBox.getSelectedItem());
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String departure = rs.getString("AirportName");
String depDate = rs.getString("DepDate");
String arrival = rs.getString("ArrAirportName");
BookingPrevTa.setText("Your flight details are:\n" +
"Departing from " + departure +
" arriving at: " + arrival +
" departing on: " + depDate);
}
}
}
答案 1 :(得分:0)
我不确定{}的功能,如果它们正在循环,那么好像你把rs.close()
置于错误的位置。我认为你应该把它放在{}之外,如果{}正在做我认为他们正在做的事情。