我正在使用以下方法通过使用 jdbc 计算工资单,但“ORA-01008:并非所有变量都绑定”错误未删除。
有什么想法吗?
我正在使用以下代码
public double getPayroll(){
ResultSet rs = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getDBConnection();
double dailyPay=0,basicPay=0,payroll2=0;
int houseRent=0,convAllow=0,noOfPresents=0,empId=0;
String q = "select e_id from employee";
pstmt = conn.prepareStatement(q);
rs = pstmt.executeQuery();
while (rs.next()) {
empId=rs.getInt(1);
String q1 = "select count(att_status) from attendance where att_status='p'";
pstmt = conn.prepareStatement(q1);
rs1 = pstmt.executeQuery(q1);
while(rs1.next()){
noOfPresents=rs1.getInt(1);
String q2 = "select e_salary,e_house_rent,e_conv_allow from employee where e_id=?";
pstmt = conn.prepareStatement(q2);
pstmt.setInt(1,empId);
rs2 = pstmt.executeQuery(q2);
while(rs2.next()){
dailyPay=rs2.getInt(1)/22;
houseRent=rs2.getInt(2);
convAllow=rs2.getInt(3);
basicPay=dailyPay*noOfPresents;
payroll2+=basicPay+houseRent+convAllow;
}
}
}
return payroll2;
}catch (Exception e) {
e.printStackTrace();
return 0.0;
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
你的问题在这里:
rs2 = pstmt.executeQuery(q2);
你正在告诉PreparedStatement
执行 SQLq2
,而不是执行之前准备的SQL。这应该只是:
rs2 = pstmt.executeQuery();
这是一个相当常见的错误,主要是由于糟糕的类设计造成的java.sql.Statement
及其亚型。
正如@RMT 指出的,你在这里犯了同样的错误:
rs1 = pstmt.executeQuery(q1);
这并不重要,因为没有占位符q1
,因此 SQL 按原样执行。但它仍然是错误的。
最后,您应该考虑致电close()
在第一个PreparedStatement
,在重新分配之前pstmt
变量到另一个。如果不这样做,您就有泄漏的风险。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)