声明了返回值却报错没有返回
本帖最后由 QQ1184318016 于 2013-03-04 12:54:24 编辑
public User getUser(int id){
Connection conn =null;
Statement stmt = null;
ResultSet set = null;
try{
//得到连接
conn = JdbcUtils.getConnection();
//得到发送对象
stmt = conn.createStatement();
//发送sql语句
String sql = "select * from User where id = "+id;
set = stmt.executeQuery(sql);
if (set.next()){
User user = new User();
String name = set.getString("name");
String password = set.getString("password");
String email = set.getString ("email");
String birthday = set.getString("birthday");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(birthday);
BeanUtils.setProperty(user, "id", id);
BeanUtils.setProperty(user, "name", name);
BeanUtils.setProperty(user, "password", password);
BeanUtils.setProperty(user, "email", email);
BeanUtils.setProperty(user, "date", date);
return user;
}
return null ;
}catch (Exception e){
new DaoException(e);
}finally{
JdbcUtils.release(conn, stmt, set);
}
}
------解决方案--------------------首先你的java样式没用上。
其次不抛异常的时候有返回值,抛异常的时候呢?
要么就把return 语句放到最后finally里面
------解决方案--------------------User user = new User();定义在if (set.next()){外部,不然每次都创建新的user了