问答详情
源自:8-7 商品详细信息显示

在商品逻辑类中的一个方法搞不定~~

这一段是ItemsDAO商品逻辑类中根据ID返回商品信息的方法,,一点一点码上去的不是复制粘贴的,和老师讲的一模一样,它提示我please add return statement,为何?

public Items getItemsById (int id) {

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try {

conn = DBHelper.getConnection();

String sql = "select * from items where id=?";

stmt = conn.prepareStatement(sql);

stmt.setInt(1, id);

rs = stmt.executeQuery();

if (rs.next()) {

Items item = new Items();

item.setCity(rs.getString("city"));

item.setId(rs.getInt("id"));

item.setName(rs.getString("name"));

item.setNumber(rs.getInt("number"));

item.setPicture(rs.getString("picture"));

item.setPrice(rs.getInt("price"));

return item;

}else{

return null;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (stmt != null) {

try {

stmt.close();

stmt = null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (rs != null) {

try {

rs.close();

rs = null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}


提问者:啊梨真神奇 2016-07-08 08:10

个回答

  • VitomyChueng
    2016-07-09 18:00:36
    已采纳

    Connection conn=null;

    PreparedStatement stmt=null;

    ResultSet rs=null;

    try{

    conn=DBhelper.getConnection();

    String sql="select * from items where id=?;";

    stmt=conn.prepareStatement(sql);

    stmt.setInt(1, id);

    rs=stmt.executeQuery();

    if(rs.next()){

    items item=new items();

    item.setId(rs.getInt("id"));

    item.setName(rs.getString("name"));

    item.setCity(rs.getString("city"));

    item.setNumber(rs.getInt("number"));

    item.setPrice(rs.getInt("price"));

    item.setPicture(rs.getString("picture"));

    return item;

    }

    else{

    return null;

    }

    }catch(Exception ex){

    ex.printStackTrace();

       return null;

    }finally{

    if(rs!=null){

    try{

    rs.close();

    rs=null;

    }catch(Exception ex){

    ex.printStackTrace();

    }

    }

    if(stmt!=null){

    try{

    stmt.close();//释放stmt内存

    stmt=null;

    }catch(Exception ex){

    ex.printStackTrace();

    }

    }


  • VitomyChueng
    2016-07-09 17:59:20

    where id=?;";一个分号没写

    Connection conn=null;

    PreparedStatement stmt=null;

    ResultSet rs=null;

    ArrayList<items> list=new ArrayList<items>();//商品集合

    try{

    conn=DBhelper.getConnection();

    String sql="select * from items";

    stmt=conn.prepareStatement(sql);

    rs=stmt.executeQuery();

    while(rs.next()){

    items item=new items();

    item.setId(rs.getInt("id"));

    item.setName(rs.getString("name"));

    item.setCity(rs.getString("city"));

    item.setNumber(rs.getInt("number"));

    item.setPrice(rs.getInt("price"));

    item.setPicture(rs.getString("picture"));

    list.add(item);

    }

    return list;

    }catch(Exception ex){

    ex.printStackTrace();

       return null;

    }finally{

    if(rs!=null){

    try{

    rs.close();

    rs=null;

    }catch(Exception ex){

    ex.printStackTrace();

    }

    }

    if(stmt!=null){

    try{

    stmt.close();//释放stmt内存

    stmt=null;

    }catch(Exception ex){

    ex.printStackTrace();

    }

    }