Items it=new Items(); it.setUid(uid);
it.setBid(bid);
it.setCreatetime(time);
it.setCount(count);
it.setPrice(price);
boolean bo=idd.addItems(it);jsp页面这样显示先
<%=it.getUid() %>
<%=it.getBid() %>
<%=it.getCreatetime() %>
<%=it.getCount() %>
<%=it.getPrice() %>
<%=bo %>显示的东西为false 1 1 2011-14-12 21 20 奇怪,几个参数都能显示出来,然后一个添加的方法返回缺是false该方法public boolean addItems(Items i) {
Connection con=null;
PreparedStatement pstmt=null;
try {
con=DBHelper.getConnection();
pstmt=con.prepareStatement("insert into items values(?,?,?,?,?)");
pstmt.setInt(1, i.getUid());
pstmt.setInt(2, i.getBid());
pstmt.setString(3, i.getCreatetime());
pstmt.setInt(4, i.getCount());
pstmt.setString(5, i.getPrice());
int o=pstmt.executeUpdate();
if(o>0)
{
return true;
}
} catch (Exception e) {
// TODO: handle exception
}finally
{
DBHelper.close(con);
}return false;
}
it.setBid(bid);
it.setCreatetime(time);
it.setCount(count);
it.setPrice(price);
boolean bo=idd.addItems(it);jsp页面这样显示先
<%=it.getUid() %>
<%=it.getBid() %>
<%=it.getCreatetime() %>
<%=it.getCount() %>
<%=it.getPrice() %>
<%=bo %>显示的东西为false 1 1 2011-14-12 21 20 奇怪,几个参数都能显示出来,然后一个添加的方法返回缺是false该方法public boolean addItems(Items i) {
Connection con=null;
PreparedStatement pstmt=null;
try {
con=DBHelper.getConnection();
pstmt=con.prepareStatement("insert into items values(?,?,?,?,?)");
pstmt.setInt(1, i.getUid());
pstmt.setInt(2, i.getBid());
pstmt.setString(3, i.getCreatetime());
pstmt.setInt(4, i.getCount());
pstmt.setString(5, i.getPrice());
int o=pstmt.executeUpdate();
if(o>0)
{
return true;
}
} catch (Exception e) {
// TODO: handle exception
}finally
{
DBHelper.close(con);
}return false;
}
你在方法最后执行了return false,那肯定返回false了,或者加到catch块里;
1: if(o>0)的条件没有成立
2:代码出现异常。catch之后进入到finally,然后return false。所以第2种情况最有可能。在catch中打个断点,或输出异常信息,看问题出在哪。
要结合异常信息,考虑很多问题。
例如:
DB连接获得了吗?
如果用工具在DB单独执行那个sql,能执行吗?
参数类型与DB类型匹配吗?
等等
问题么 比如数据库连接问题,或者字段约束违反,id可能有唯一性吧
先在数据库里测一下这句insert对否