如何使用JavaJDBC运行sql事务

bvk5enib  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(303)

此问题已在此处找到答案

如何在jdbc中启动事务((8个答案)
如何使用jdbc驱动程序编写事务((1个答案)
7小时前关门了。
这是我的sql代码,一个简单的事务。当我在phpmyadmin上运行它时,它工作得很好。

START TRANSACTION;
SELECT received_money FROM bank_accounts WHERE `uuid` = "2eaef3e4-eeb6-41f7-9ecf-5d503465baba";
UPDATE bank_accounts SET received_money = 0 WHERE `uuid` = "2eaef3e4-eeb6-41f7-9ecf-5d503465baba";
COMMIT;

但我想在java代码中直接使用此sql代码:

String sql = "START TRANSACTION;\n" +
            "SELECT placed_money FROM bank_accounts WHERE uuid = \"" + uuid + "\";\n" +
            "UPDATE bank_accounts SET placed_money = 0 WHERE uuid = \"" + uuid + "\";\n" +
            "COMMIT;";
Connection connection = connect();
Statement stmt = connection.createStatement();
ResultSet resultSet = stmt.executeQuery(sql);

但每次我都有这个错误

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT received_money FROM bank_accounts WHERE `uuid` = "2eaef3e4-eeb6-41f7-9ecf' at line 2
q9yhzks0

q9yhzks01#

这是一个多查询的问题。
所以你需要一个连接字符串来允许

String dbUrl = "jdbc:mysql:///test?allowMultiQueries=true";

进一步删除所有\n如我在评论中所说的“替换”

String sql = "START TRANSACTION;" +
        "SELECT placed_money FROM bank_accounts WHERE uuid = '" + uuid + "';" +
        "UPDATE bank_accounts SET placed_money = 0 WHERE uuid = '" + uuid + "';" +
        "COMMIT;";

相关问题