我们可以在一种方法中使用准备好的语句来使用两个查询吗

发布于 2024-11-08 07:24:13 字数 1686 浏览 0 评论 0原文

我们可以在使用准备好的语句时在一种方法中使用两个查询吗?我已经尝试使用这个,但即将出现无效列名异常。

我的代码片段如下。

    public double getPayroll(){
            ResultSet rs = null;
            ResultSet rs2 = null;
            Connection conn = null;
            PreparedStatement pstmt = null;
            try {
              conn = getDBConnection();
              int employeeId;
              String q1 = "select e_salary,e_house_rent,e_conv_allow,e_id 
                            from employee";
              pstmt = conn.prepareStatement(q1);
              rs = pstmt.executeQuery();
              double dailyPay=0,basicPay=0,payroll2=0;
              int houseRent=0,convAllow=0;
              while (rs.next()) {
                dailyPay = rs.getInt(1)*.03;
                houseRent=rs.getInt(2);
                convAllow=rs.getInt(3);
                employeeId=rs.getInt(4);
              }
              String q2="select att_status from attendance where 
                            e_id=employeeId";
              pstmt = conn.prepareStatement(q2);
              rs2 = pstmt.executeQuery();
              int noOfPresents = 0;
              while(rs2.next()){
                  noOfPresents+=1;
             }    
             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();
              }
            }
}

Can we use two queries in one method while using prepared statement, I have tried using this but invalid column name exception is coming.

My code snippets is as follows.

    public double getPayroll(){
            ResultSet rs = null;
            ResultSet rs2 = null;
            Connection conn = null;
            PreparedStatement pstmt = null;
            try {
              conn = getDBConnection();
              int employeeId;
              String q1 = "select e_salary,e_house_rent,e_conv_allow,e_id 
                            from employee";
              pstmt = conn.prepareStatement(q1);
              rs = pstmt.executeQuery();
              double dailyPay=0,basicPay=0,payroll2=0;
              int houseRent=0,convAllow=0;
              while (rs.next()) {
                dailyPay = rs.getInt(1)*.03;
                houseRent=rs.getInt(2);
                convAllow=rs.getInt(3);
                employeeId=rs.getInt(4);
              }
              String q2="select att_status from attendance where 
                            e_id=employeeId";
              pstmt = conn.prepareStatement(q2);
              rs2 = pstmt.executeQuery();
              int noOfPresents = 0;
              while(rs2.next()){
                  noOfPresents+=1;
             }    
             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();
              }
            }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2024-11-15 07:24:13

你的问题是q2中的sql假设有一个名为employeeId的列,但我怀疑你想插入变量employeeId的值。

将其更改为

select att_status from attendance where e_id=?

然后

pstmt.setString(1, employeeId);

在执行 pstmt.executeQuery() 之前执行;

Your problem is that the sql in q2 assumes that there is a column named employeeId, but I suspect you want to insert the value of the variable employeeId.

Change it to

select att_status from attendance where e_id=?

Then execute

pstmt.setString(1, employeeId);

before executing pstmt.executeQuery();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文