与第二个查询Faceing问题,它是依赖于第一查询
问题描述:
在我下面的代码IST查询成功完成,但由IST查询结果需要使用2号查询其不工作与第二个查询Faceing问题,它是依赖于第一查询
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
公共无效断开(){
//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();
}
答
在第二份声明,您使用的是字面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准备语句,以及如何将它们与结合使用的更详细的解释一读。
(注意:我还没有试过这个代码,但我希望这个想法有帮助)。
为了帮助我们调试您的问题,可以向我们展示错误或堆栈跟踪?你知道结果集的价值吗?你是否直接在数据库上测试你的查询?做了那个工作 – Thomas
你的意思是*它不工作*? – Guy
实际上它运行良好,ist查询但第二个查询结果不显示 – Ruma