Java JdbcTemplate.update() 插入返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21174705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:07:06  来源:igfitidea点击:

JdbcTemplate.update() insert return values

javaspring-mvcdb2spring-jdbcjdbctemplate

提问by Sriram Lns

JdbcTemplate.update()returns number of rows affected - so you not only know that delete/update was sucessfull, you also know how many rows were deleted/updated.

JdbcTemplate.update()返回受影响的行数 - 因此您不仅知道删除/更新成功,还知道删除/更新了多少行。

What will be the return value if I try to insert a row.

如果我尝试插入一行,返回值是什么。

Is it possible to get return value as "0"??

是否可以将返回值设为“0”??

 private static final String insertSql =
 "INSERT INTO employee (" +
 " name, " +
 " surname, " +
 " title, " +
 " created) " +
 "VALUES (John, Smith, Software developer, new Date())";
 int row = template.update(insertSql, params, types);

采纳答案by Turophile

Yes, in theory you should get 0 or 1, but if no row was inserted, it would be due to an error, so a DataAccessExceptionwould be thrown, which is how you would know that something went wrong and no row was created.

是的,理论上你应该得到 0 或 1,但如果没有插入行,那将是由于错误,所以DataAccessException会抛出 a,这就是你如何知道出现问题并且没有创建行的方式。

回答by Prancer

To answer your question, yes, as @Turophile pointed out, you will get a 0 or 1 in response as you can tell from the method signature(which returns an int).

要回答您的问题,是的,正如@Turophile 所指出的,您将得到 0 或 1 作为响应,正如您从方法签名(返回一个 int)中可以看出的那样。

In response to @Turophile comment(sorry I can't add a comment :/ ), to avoid this and avoid partial data being saved to the database, use Springs Transaction Management (tx). This would allow you to rollback transactions based on specific exceptions or all exceptions from the Exception class. Mind you, by default, @Rollback rolls back transactions for runtime, unchecked exceptions only.

为了回应@Turophile评论(抱歉,我无法添加评论 :/ ),为了避免这种情况并避免将部分数据保存到数据库中,请使用 Springs 事务管理 (tx)。这将允许您根据 Exception 类中的特定异常或所有异常回滚事务。请注意,默认情况下,@Rollback 回滚运行时事务,仅限未经检查的异常。

@Transactional(rollbackFor = Exception.class)
public Employee updateEmployee(Employee pEmployee) { ... }

The @Transactional can also be added to the class as well, but I would read up more on itif you are interested in implementing this feature. There is a lot of good documentation on tx.

@Transactional 也可以添加到类中,但如果您有兴趣实现此功能,我会阅读更多相关内容。有很多关于 tx 的好文档。

As a side note, I don't recommend lying to the caller of your method. If you call a method that says "update", run a query that'll update the record, if you want to insert/create a new record, create or call a method that "inserts" a new record into your table -- inserting a duplicate record wouldn't work anyways if the primary key(s) are unique.

作为旁注,我不建议对您的方法的调用者撒谎。如果您调用显示“更新”的方法,请运行将更新记录的查询,如果您想插入/创建新记录,请创建或调用将新记录“插入”到表中的方法——插入如果主键是唯一的,重复的记录无论如何都不会起作用。

回答by Rajesh

jdbctemplate.update will return in integer format as we know. in order to find 0 or 1 just do this below simple code

正如我们所知,jdbctemplate.update 将以整数格式返回。为了找到 0 或 1 只需在下面的简单代码中执行此操作

int i=jdbctemplate.update(.your db call..);

it will return 1 for success and 0 for failure case so, according to it you can process your logic.

成功时返回 1,失败时返回 0,因此,根据它您可以处理您的逻辑。