Java d有一个空列表,不知道任何异常(日志中除外)。(或null但我最近尝试使用exception而不是null) public List<Product> doSelectAvailableProducts( String userid){
d有一个空列表,不知道任何异常(日志中除外)。(或Java d有一个空列表,不知道任何异常(日志中除外)。(或null但我最近尝试使用exception而不是null) public List<Product> doSelectAvailableProducts( String userid){ ,java,compiler-errors,return,Java,Compiler Errors,Return,d有一个空列表,不知道任何异常(日志中除外)。(或null但我最近尝试使用exception而不是null) public List<Product> doSelectAvailableProducts( String userid){ Connection con = null; Statement stmt = null; ResultSet rs = null; List<Product> cl = new ArrayList&
null
但我最近尝试使用exception而不是null)
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(); }
}} }
catch( SQLException e ){
e.printStackTrace();
return new ArrayList<Product>(); // <--
}
finally {
if(stmt != null) {
try { stmt.close();}
catch (Exception e) { e.printStackTrace(); }
}
if(con != null) {
try { con.close();}
catch (Exception e) { e.printStackTrace(); }
}
}
return null;
}
}
List<Product> doSelectAvailableProducts result=object.doSelectAvailableProducts(aStringId);
if(result!=null){
// Do you next work flow here...
}
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();
return cl;
}
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();
return cl;
}
finally {
if(stmt != null) {
try { stmt.close();}
catch (Exception e) { e.printStackTrace(); }
}
if(con != null) {
try { con.close();}
catch (Exception e) { e.printStackTrace(); }
}} return cl;}
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){
//throw this exception or add a return here?
e.printStackTrace();
}
try{
stmt = con.createStatement();
String sql = "select * from Product where id not in " +"(select cid from registration where uid ='" + userid + "')";
//Exception might be thrown here, so the return statment below isn't reached
//then all the code in the catch and finally run, but nothing is returned
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); }
// Last return statement, never reached
return cl;
}
catch( SQLException e ){
//throw this exception or add a return here?
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();
}
}
//add a return here?
}
//add a return here?
}