【发布时间】:2018-03-18 05:24:07
【问题描述】:
此 ProductDAO 类返回用户的产品列表,但 Netbeans 中的编译器显示“缺少返回语句”。有预支吗?
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语句来执行所有可能的操作。 -
如果您学习 java,您需要先向 docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html 学习,然后再将此类问题发布到 stack-overflow stackoverflow.com/help/how-to-ask,其中 Netbeans IDE 已经知道您有关此编译时间的情况 缺少返回语句
标签: java compiler-errors return