您是否直接从JSP使用JDBC?我希望不会......在任何情况下,Spring JDBC都有一些JDBC的好帮手。你想要使用JdbcTemplate和DataSourceTransactionManager。
下面是一个简单的例子(不要指望它来编译,只是为了让你去...)
DataSource dataSource = getDataSourceFromSomewhere();
DataSourceTransactionManager tm = new DataSourceTransactionManager(dataSource);
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
TransactionStatus tx = tm.getTransaction(new DefaultTransactionDefinition());
int update1 = jdbc.update("insert into employee...");
int employeeId = jdbc.queryForInt(select id from employee...");
int update2 = jdbc.update("insert into employee_department...");
if (employeeId > 0 && update2 > 0)
tm.commit(tx);
else
tm.rollback(tx);
如果你不想使用Spring(或不能),平旧的JDBC可以做同样的事情:
Connection conn = getConnectionFromSomewhere()
... do a bunch of work ...
if (work was successful)
conn.commit();
else
conn.rollback();
虽然我认为使用Spring JDBC库比普通的旧JDBC更容易。
你到目前为止做了什么? – diEcho 2014-09-02 05:09:31
我已经完成了3个不同的查询插入部分。现在正在研究验证这些插入 – Sajirupee 2014-09-02 05:10:22
阅读有关交易。例如。您可以编写一个方法并将其标记为@Transactional(如果您使用Spring)来一起执行所有查询。 – StanislavL 2014-09-02 06:09:06