0

This is my SQL code, a simple transaction. When i run it on phpmyadmin it's work well.

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;

But i want to use this sql code directly in my Java code:

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);

But every time i have this error

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
Luluxe
  • 13
  • 3
  • show us how you defined and set the variable sql – nbk Jul 09 '21 at 18:30
  • Sorry I just added it – Luluxe Jul 09 '21 at 18:35
  • use instead \" a single quote and also loo urgently for prepared statements – nbk Jul 09 '21 at 18:37
  • I have the same error with simple quote – Luluxe Jul 09 '21 at 18:38
  • You should not call `start transaction` and `commit transaction` in JDBC. You need to disable auto-commit mode (`Connection.setAutoCommit(false)`), and commit using `Connection.commit()`. Also JDBC is not designed for executing multiple statements like this, you should execute them individually. – Mark Rotteveel Jul 10 '21 at 07:25

1 Answers1

0

That is a multiple queries.

so you need a connection string that allows that

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

Further remove all \ n and as i said in the comment replace "

String sql = "START TRANSACTION;" +
        "SELECT placed_money FROM bank_accounts WHERE uuid = '" + uuid + "';" +
        "UPDATE bank_accounts SET placed_money = 0 WHERE uuid = '" + uuid + "';" +
        "COMMIT;"; 
nbk
  • 45,398
  • 8
  • 30
  • 47
  • It's work I don't have the same error anymore. But i have "ResultSet is from UPDATE. No Data." Have you an idea ? – Luluxe Jul 09 '21 at 18:57