这个问题在这里已有答案:
在最近几天我无法弄清楚如何解决这个问题,我在使用Java EE的同一指令中遇到了多个查询的问题,
static public void transfer(String sender, String receiver, double amount) {
try (Connection connection = DriverManager.getConnection(dbURL, dbLogin, dbPassword)) {
String query = " insert into T_Transactions (sender, receiver, amount, date) values (?, ?, ?, ?) ; update T_Users set balance =? WHERE email = ?";
Date d = new Date();
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, sender);
statement.setString(2, receiver);
statement.setDouble(3, amount);
statement.setString(4, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSS").format(d));
statement.setDouble(5, amount);
statement.setString(6, sender);
try {
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
我在mysql中尝试了查询,它工作正常,但每次我在我的代码中使用它时都面临以下错误
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update T_Users
我在这里错过了什么?谢谢
1 回答
我不确定为什么一起尝试 . 请单独使用插入和更新 . 否则顺其自然 . 我的理解是在Mysql和java中执行多个查询是不同的