1

I have tried to return an int variable but it's not working. I want to return my variable product_price in my function. Please help me.

  public static int getProductSellPriceById(int productid) {

        try {
            currentCon = ConnectionManager.getConnection();
            ps=currentCon.prepareStatement("select product_sell_price from products where product_id=?");

            ps.setInt(1, productid);
            ResultSet rs = ps.executeQuery(); 

            if (rs.next()) {
                int product_price = Integer.parseInt(rs.getString("product_sell_price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return product_price;
   }
Star
  • 103
  • 9
  • 4
    declare `int product_price = 0;//or any default value` outside `try..catch` block – Youcef LAIDANI Jun 09 '18 at 20:16
  • i want to return this Integer.parseInt(rs.getString("product_sell_price")). – shafiq ruslan Jun 09 '18 at 20:23
  • 2
    Possible duplicate of [Why does a Try/Catch block create new variable scope?](https://stackoverflow.com/questions/11655020/why-does-a-try-catch-block-create-new-variable-scope) – Logan Jun 09 '18 at 20:25
  • There are many ways to solve this issue, just declare the variable outside the try and catch and just use `product_price = Integer.parseInt(rs.getString("product_sell_price"));` or you can use `return Integer.parseInt(rs.getString("product_sell_price"));` and before you close your method return a default value – Youcef LAIDANI Jun 09 '18 at 20:26

2 Answers2

1

You can return directly from within the if condition:

public static int getProductSellPriceById(int productid) {

    try {
        currentCon = ConnectionManager.getConnection();
        ps=currentCon.prepareStatement("select product_sell_price from products where product_id=?");

        ps.setInt(1, productid);
        ResultSet rs = ps.executeQuery(); 

        if (rs.next()) {
            return Integer.parseInt(rs.getString("product_sell_price"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } 

    return 0; // or any other default value
}

You can also change the return type of your function to Integer and return null as the default value to denote that no value was found.

luthrad
  • 67
  • 4
1

Don't assign the value to a variable. If your code doesn't find any appropriate product it will execute without a problem and return 0, just like it will when it hits a database problem, such as the table having different columns.

Also be aware that you need to clean up any resources you are using from the database, otherwise you'll run out of connections real soon. Luckily try-with-resource makes that very easy.

I've adapted your code to what it should be.

public int getProductSellPriceById(int productId) throws SQLException, NoSuchElementException {
    try (Connection currentCon = ConnectionManager.getConnection();
         PreparedStatement ps = currentCon.prepareStatement("select product_sell_price from products where product_id=?")) {

        ps.setInt(1, productId);

        try (ResultSet rs = ps.executeQuery()) { 
            if (rs.next()) {
                return Integer.parseInt(rs.getString("product_sell_price"));
            }
        }
    }

    throw new NoSuchElementException(String.format("No product with id %d found", productId));
}   

If the product-sellprice is not mandatory you could also use OptionalInt as a return type, replace the return with return OptionalInt.of(...), and the throw with return OptionalInt.empty(). If it is a throw is better as it indicates a model error which you should not lightly ignore.

Although I wonder why you'd use a product_sell_price column that is a String column type and parse it into an integer. That is a risk. Better define the column to be a number type.

M. le Rutte
  • 3,525
  • 3
  • 18
  • 31