try catch方法中的返回值

时间:2016-04-03 02:38:29

标签: java

 public double calRoomCharges(int cusId)


{

    try
    {


        //Get Rates
        String sql="SELECT nights FROM reservation where cus_id ='"+cusId+"'";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        int roomRates = 0;
            if(!rs.next())
            {
                //handle no result
            } 
            else 
            {
                roomRates = rs.getInt("nights") ;

            }
            double tot1 =roomRates;
            String totRoom = String.valueOf(tot1);
            rc.setText(totRoom);

    }
    catch(Exception e){}




 }

我想从这个方法中获取totRoom。我该如何返回它?如果我将return语句放在try catch块之外,它就不会获取块内计算的值。

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。如果有效,请告诉我。

public double calRoomCharges(int cusId)
{
    String totRoom = "";
    double totalCharge = 0.0;

    try
    {
        //Get Rates
        String sql="SELECT nights FROM reservation where cus_id ='"+cusId+"'";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        int roomRates = 0;
            if(!rs.next())
            {
                //handle no result
            } 
            else 
            {
                roomRates = rs.getInt("nights") ;
                totalCharge = roomRates; // calculate total charge
                String totRoom = String.valueOf(totalCharge);
                rc.setText(totRoom);

            }

    }
    catch(Exception e){
        System.out.prtintln(e.getMessage());
    }

    return totalCharge;
 }
相关问题