使用 java 处理 Postgresql 事务

编程入门 行业动态 更新时间:2024-10-24 10:25:04
本文介绍了使用 java 处理 Postgresql 事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个带有preparedStatement的查询块.

I have two blocks of queries with preparedStatement.

这是第一个:

String sql = "update cikan_malzeme set miktar = ? where proje_id = ? and malzeme_id = ?"; PreparedStatement prep = dbConnect.connection.prepareStatement(sql); prep.setFloat(1, toplam); prep.setInt(2, pid); prep.setInt(3, mid); prep.executeUpdate();

这是第二个:

String sql2 = "update malzemeler set miktar = ? where malz_adi = ?"; PreparedStatement prep2 = dbConnect.connection.prepareStatement(sql2); prep2.setFloat(1, fark); prep2.setString(2, malzemeadi); prep2.executeUpdate();

现在我想用事务BEGIN来执行它们;和提交;如何使用 PreparedStatement 处理事务?

Now I want to execute them with the transaction BEGIN; and COMMIT; How can I handle transaction with preparedStatement?

推荐答案

将自动提交设置为 false.

Set auto commit to false.

将您的 PreparedStatements 放在 try 块中.最后提交;在 catch 块中回滚.

Put your PreparedStatements in a try block. Commit at the end; rollback in the catch block.

这就是通常在基本 JDBC 中完成的方式.

That's how it's usually done in bare bones JDBC.

docs.oracle/javase/tutorial/jdbc/basics/transactions.html

如果您使用 EJB3 或 Spring,您可以添加一个事务管理器并以声明方式指定它们.这更加复杂和灵活.

If you use EJB3 or Spring you can add a transaction manager and specify them declaratively. That's more sophisticated and flexible.

更多推荐

使用 java 处理 Postgresql 事务

本文发布于:2023-07-21 11:12:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事务   java   Postgresql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!