[英]Before start of result set running multiple sql
I'm currently working on an application for a restaurant. 我目前正在为餐厅申请。 The idea for the code I'm currently working on is that it gets the name from a combobox and inserts a receipt into the database whith the employee number of the person who is working on it. 我当前正在使用的代码的想法是,它从组合框中获取名称,并将收据插入数据库中,其中包含正在处理该代码的人员的雇员编号。 The code whith which I'm trying to do it is: 我正在尝试的代码是:
RestaurantOrder sqlRestaurantOrder = new RestaurantOrder();
ActionListener printReceiptListner;
printReceiptListner = (ActionEvent) -> {
int ID = Integer.parseInt(input1.getText());
try {
SystemManager manager = new SystemManager();
ResultSet rs = manager.stmt.executeQuery(sqlRestaurantOrder.setIdSql(ID));
while (rs.next()) {
double totalPrice = rs.getDouble("sumprice");
if (ID > 0) {
Receipt receiptSql = new Receipt();
String firstName = (String) cb.getSelectedItem();
String checkoutDate = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
ResultSet rs2 = manager.stmt.executeQuery(receiptSql.getEmployeeId(firstName));
int employeeId = rs2.getInt("id");
while (rs2.next()) {
Receipt receiptSql2 = new Receipt();
ResultSet rs3 = manager.stmt.executeQuery(receiptSql2.SetStatusReceipt(employeeId, checkoutDate, ID, totalPrice));
while (rs3.next()) {
}
}
}
}
} catch (SQLException k) {
JOptionPane.showMessageDialog(null, k.getMessage());
}
};
The statements are: 这些语句是:
public class Receipt {
public String sql;
public String sql2;
public String getEmployeeId(String firstName){
return sql2 = "SELECT id FROM employee WHERE firstName = '" + firstName + "';";
}
public String SetStatusReceipt(int employeeId, String checkoutDate, int id, double totalPrice){
return sql = "INSERT INTO `receipt` (`employeeId`, `price`, `restaurantOrderId`, `checkoutDate`) VALUES (" + employeeId + " , " + totalPrice + ", " + id + ", " + checkoutDate + ");";
};
}
I'm getting the error before start of result set which I looked up what it means but I can't get it fixed at the moment. 我在开始结果集之前就收到了错误消息,我查了一下意思是什么,但目前无法解决。 Help will be appreciated. 帮助将不胜感激。
If I forgot to post more important code let me know I'll update it 如果我忘记发布更重要的代码,请告诉我,我将对其进行更新
You have to call ResultSet.next()
before you are able to access the fields of that ResultSet
. 您必须先调用ResultSet.next()
才能访问该ResultSet
的字段。 So you need to move your assignment of employeeId
into your while
loop: 因此,您需要将对employeeId
的分配移动到while
循环中:
while (rs2.next()) {
int employeeId = rs2.getInt("id");
From the documentation of ResultSet.next()
: 从ResultSet.next()
的文档中:
A
ResultSet
cursor is initially positioned before the first row;ResultSet
游标最初位于第一行之前; the first call to the methodnext
makes the first row the current row; 第一个对方法的调用next
使第一行成为当前行; the second call makes the second row the current row, and so on. 第二个调用使第二行成为当前行,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.