16

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();
              }
            }
} 
4

4 に答える 4

50

あなたの問題はここにあります:

rs2 = pstmt.executeQuery(q2);

以前に準備した SQL を実行するのではなくPreparedStatement、 SQL を実行するように指示しています。q2これは次のようになります。

rs2 = pstmt.executeQuery();

これはかなり一般的な間違いであり、主にjava.sql.Statement とそのサブタイプの不適切なクラス設計が原因です。

@RMT が指摘しているように、ここでも同じ間違いを犯します。

rs1 = pstmt.executeQuery(q1);

にはプレースホルダーがないため、これはそれほど重要ではなくq1、SQL はそのまま実行されます。しかし、それはまだ間違っています。

最後に、変数を別の変数に再割り当てする前close()に、最初のを呼び出すことを検討する必要があります。そうしないと漏れる恐れがあります。PreparedStatementpstmt

于 2011-06-24T15:11:49.177 に答える
1

そのようなpstmtのインスタンスを再利用できないことが1つの理由かもしれません。ループの各レベルで個別のPreparedStatementインスタンスを使用する必要があります。

これは1つのステートメントでも実行できることをご存知ですか?

編集:従業員と出席の間に関係があると仮定すると、このようなものは単一のリクエストで合計を返します

select sum( (e_salary / 22) * att_count + e_house_rent + e_conv_allow )
from (
    select emp.e_salary
           emp.e_house_rent,
           emp.e_conv_allow, 
           (select count(att.att_status) from attendance att where att.e_id = mp.e_id) s att_count
    from employee emp
) t 

実際に出席が従業員にリンクされていない場合は、ネストされたselectのwhere句を省略してください。

于 2011-06-24T15:13:17.327 に答える
1
                            pstmt = conn.prepareStatement(q2);
                            pstmt.setInt(1,empId);
                            rs2 = pstmt.executeQuery(q2);

クエリ q2 を使用して準備済みステートメントを既に作成し、変数 empId をそれにバインドしています。ここで pstmt.executeQuery(q2) を呼び出すと、変数のバインドが失われます。pstmt.executeQuery(q2) を実行すると、JDBC ドライバーはバインドされていない sql q2 を解析する可能性があります。

于 2011-06-24T15:15:33.300 に答える