此ProductDAO类返回用户的产品列表,但Netbeans中的编译器显示“缺少返回语句”。任何进展?缺少return语句编译器警告
public List<Product> doSelectAvailableProducts(String userid){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
List<Product> cl = new ArrayList<Product>();
try{
con = datasource.getConnection();
}
catch(SQLException e){
e.printStackTrace();
}
try{
stmt = con.createStatement();
String sql = "select * from Product where id not in " +"(select cid from registration where uid ='" + userid + "')";
rs = stmt.executeQuery(sql);
while(rs.next()){
Product product = new Product();
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setDescription(rs.getString("description"));
product.setPrice(Double.parseDouble(rs.getString("price"))); cl.add(product); }
return cl;
}
catch(SQLException e){
e.printStackTrace();
}
finally {
if(stmt != null) {
try { stmt.close();}
catch (Exception e) { e.printStackTrace(); }
}
if(con != null) {
try { con.close();}
catch (Exception e) { e.printStackTrace(); }
}} }
如果在上次try-catch中发生异常,在此块或catch/finally子句中没有'return'语句。您需要为每个可能的执行都有一个'return'语句。 – AxelH
[在非void方法编译时缺少return语句](https:// stackoverflow。com/questions/16789832/missing-return-statement-in-a-non-void-method-compiles) – Sturzl
如果你学习java,你需要学习https://docs.oracle.com/javase/tutorial/java/ javaOO/returnvalue.html在发布这种类型的问题之前堆栈溢出https://stackoverflow.com/help/how-to-ask,其中Netbeans IDE已经知道你关于这个编译时间**缺少返回语句** – 2017-10-06 12:28:46