今天用jdbc写了一句sql语句,通过PreparedStatement对象来插入记录,发现一个奇怪的问题,我明明是成功插入记录,可是pstmt.execute()确返回的是false,狂晕中。
String sqlText = "insert into problem(title, " +
"description, input, output, sample_input, " +
"sample_output, hint, source, uploader) " +
"values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = null;
......
res = pstmt.execute();
呵呵,感到比较困惑。查看sun的API后恍然大悟。
sun API 写道
public boolean execute()
throws SQLExceptionExecutes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement. Some prepared statements return multiple results; the execute method handles these complex statements as well as the simpler form of statements handled by the methods executeQuery and executeUpdate.
The execute method returns a boolean to indicate the form of the first result. You must call either the method getResultSet or getUpdateCount to retrieve the result; you must call getMoreResults to move to any subsequent result(s).
Returns:
true if the first result is a ResultSet object; false if the first result is an update count or there is no result
Throws:
SQLException - if a database access error occurs or an argument is supplied to this method
再次发现一个小问题,我将如何判断这条记录是否插入成功呢?我选择了不用execut方法,而改用executeUpdate方法。
if(pstmt.executeUpdate() == 1)
return true;
else
return false;
executeUpdate()返回数据库中更新记录的条数。那哪位大侠出来说一下execute该在什么情况下使用哇?
分享到:
相关推荐
JDBC中PreparedStatement接口提供的execute、executeQuery和executeUpdate之间的区别及用法
jdbc2.0版 PreparedStatement接口的用法
PreparedStatement详细用法
关于PreparedStatement插入Date类型值的方法.txt
JDBC基础教程之PreparedStatement.doc )
NULL 博文链接:https://huiminchen.iteye.com/blog/1097332
Statement和PreparedStatement之间的区别
练习3:使用PreparedStatement插入宠物信息.zip
在java中向访问数据库的sql语句一般放到类PreparedStatement类构造函数中,这里遇到的问题就是向sql语句中用setDate()插入时间时不能用java.util.date,而应用java.sql.date,这里就讲述了解决方法,一种是插入...
NULL 博文链接:https://chaoyi.iteye.com/blog/2088080
java中PreparedStatement和Statement的区别
MySql练习3:使用PreparedStatement插入宠物信息.zip MySql练习3:使用PreparedStatement插入宠物信息.zip MySql练习3:使用PreparedStatement插入宠物信息.zip
NULL 博文链接:https://stevenjohn.iteye.com/blog/968877
可以了解PreparedStatemen的具体用法,使用与JSP初学者
此实例意在解决预处理命令PreparedStatement的setString()方法,在sql2008数据库中写入数据时,会自动补足空格的问题, 同时此实例也解决了当存在自动补足空格的问题时,使用nvarchar可以使查找出来的数据与原输入...
什么时候用statement,什么时候用preparedstatement
java.sql.PreparedStatement接口。PrepareStatement接口继承Statement接口。 PrepatredStatement实例包含已编译的SQL语句,由于PreparedStatement对象已预编译过哦哦,所以执行速度快于Statement对象。 包含于...
主要介绍了JSP中的PreparedStatement对象操作数据库的使用教程,文中举了一些使用PreparedStatement预处理语句对象进行MySQL增删查改的例子,需要的朋友可以参考下
jdbc中preparedStatement比Statement的好处