tenten
tenten

Reputation: 1276

Getting return value in JSP

Below is the code for auto generate purchase order number from the MySQL database in JSP. I want to return "POno" String but It shows error because It is outside the if condition.

How can I get this String as a return?

<%!
    public String autoPONo()throws SQLException{

        rs=pst.executeQuery();

        if(rs.next()){
            String po= rs.getString("max(PONo)");
            int intNo = Integer.parseInt(po);
            intNo+=1;

            String POno = Integer.toString(intNo);  
        }

           return POno; 
     }
%>

Upvotes: 1

Views: 726

Answers (2)

Dongqing
Dongqing

Reputation: 686

You must declare the POno out of the if or return inside if, so that the POno is accessible for return statement.

    rs=pst.executeQuery();

     String POno = "";
        if(rs.next()) {
           String po= rs.getString("max(PONo)");
           int intNo = Integer.parseInt(po);
           intNo+=1;
               POno = Integer.toString(intNo);  
        }

    return POno; 

or

    rs=pst.executeQuery();

    if(rs.next()) {
         String po= rs.getString("max(PONo)");
         int intNo = Integer.parseInt(po);
         intNo+=1;

         String POno = Integer.toString(intNo); 
         return POno; 
     }

    return ""; 

Upvotes: 1

Jens
Jens

Reputation: 69440

Declare the variable otsido of your loop:

<%!
    public String autoPONo()throws SQLException{
               String POno = null;
                rs=pst.executeQuery();

                if(rs.next()){
                   String po= rs.getString("max(PONo)");
                   int intNo = Integer.parseInt(po);
                   intNo+=1;

                   POno = Integer.toString(intNo);  
                }

           return POno; 
        }
    }
%>

Upvotes: 4

Related Questions