【发布时间】:2018-11-19 13:09:53
【问题描述】:
我尝试返回一个 int 变量,但它不起作用。我想在我的函数中返回我的变量 product_price。请帮帮我。
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;
}
【问题讨论】:
-
在
try..catch块外声明int product_price = 0;//or any default value -
我想返回这个 Integer.parseInt(rs.getString("product_sell_price"))。
-
有很多方法可以解决这个问题,只需在try and catch之外声明变量并使用
product_price = Integer.parseInt(rs.getString("product_sell_price"));或者你可以使用return Integer.parseInt(rs.getString("product_sell_price"));并且在你关闭你的方法之前返回一个默认值
标签: java