sql Statemente execute 返回值为false

今天用jdbc写了一句sql语句,通过PreparedStatement对象来插入记录,发现一个奇怪的问题,我明明是成功插入记录,可是pstmt.execute()确返回的是false,狂晕中。
Java代码 复制代码  收藏代码
  1. String sqlText "insert into problem(title,  
  2.         "description, input, output, sample_input,  
  3.         "sample_output, hint, source, uploader)  
  4.         "values(?, ?, ?, ?, ?, ?, ?, ?, ?)";  
  5.     PreparedStatement pstmt null;  
  6. ......   
  7. res pstmt.execute();  

 

   呵呵,感到比较困惑。查看sun的API后恍然大悟。

sun API 写道
public boolean execute()
throws SQLExceptionExecutes the SQL statement in thisPreparedStatement object, which may be any kind of SQL statement.Some prepared statements return multiple results; the executemethod handles these complex statements as well as the simpler formof statements handled by the methods executeQuery andexecuteUpdate.
The execute method returns a boolean to indicate the form of thefirst result. You must call either the method getResultSet orgetUpdateCount to retrieve the result; you must call getMoreResultsto move to any subsequent result(s).


Returns:
true if the first result is a ResultSet object; false if the firstresult is an update count or there is no result
Throws:
SQLException - if a database access error occurs or an argument issupplied to this method

    再次发现一个小问题,我将如何判断这条记录是否插入成功呢?我选择了不用execut方法,而改用executeUpdate方法。

Java代码 复制代码  收藏代码
  1. if(pstmt.executeUpdate() == 1 
  2.     return true;  
  3. else  
  4.     return false;  

    executeUpdate()返回数据库中更新记录的条数。那哪位大侠出来说一下execute该在什么情况下使用哇?

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值