面对第二个查询的问题,这取决于第一个查询

时间:2017-06-12 07:51:53

标签: mysql jdbc connection

在我的下面的代码中,ist查询已成功完成但是从ist查询结果需要用于第二个查询它不能正常工作

        try{

            pstmt=conn.prepareStatement("SELECT * FROM loanrequest where loan_status_id='1' and loan_sub_status_id is NULL and business_id!=0 and white_label_id=1 order by loanId desc limit 1;;");


            ResultSet rs=pstmt.executeQuery();



            while(rs.next()){  //next result will check until the result 

                String dbbusinessname=rs.getString("loan_ref_id");
                xls.setCellData("Data","loan_ref_id",35,dbbusinessname);
                System.out.println(dbbusinessname);

                String dbloanId=rs.getString("loanId");
                xls.setCellData("Data","loanId",35,dbloanId);


                //Need to use loan id in my 2nd query

//这部分依赖于ist查询。 //以下查询其无效

                pstmt=conn.prepareStatement("SELECT * FROM loan_bank_mapping where loan_id='dbloanId';;");

                ResultSet rs=pstmt.executeQuery();



            while(rs.next()){  //next result will check until the result 

                String dbloan_bank_mapping_id=rs.getString("loan_bank_mapping_id");
                xls.setCellData("Data","loan_bank_mapping_id",35,dbloan_bank_mapping_id);
                System.out.println(dbbusinessname);




            }
            }catch (Exception e) {


                System.out.println("Error in fireing query");
                // TODO Auto-generated catch block
                e.printStackTrace();
            Assert.fail("Exception in query"+e.getMessage());

                //throw new SkipException("Could not establish connection");
            }//Create the object of driver class

public void disconnect(){

    //when doing testing after testing  need to close orelse trouble will come


    try {


        if(rs!=null){
            rs.close();
        }


        if(pstmt!=null){
            pstmt.close();
        }

        if((conn!=null) && (!conn.isClosed())){
            conn.close();
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

在第二个语句中,您使用了一个文字作为loan_id的值。您需要在字符串中放置占位符,然后将值绑定到它。

pstmt=conn.prepareStatement("SELECT * FROM loan_bank_mapping where loan_id=?");
pstmt.setString(1, dbloanId);

阅读http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html以获取有关预准备陈述的更详细说明以及如何将其与捆绑一起使用。

(注意:我还没有尝试过这段代码,但我希望这个想法有所帮助)。