4
I wonder if it is possible to make one INSERT
and a UPDATE
in the same query, that is, in the same operation.
I’m using the following to do both operations.
public Connection conn = null;
...
conn = DriverManager.getConnection (url, userName,password);
...
String Query_SQL_UPDATE = "UPDATE users SET name = 'exemplo';";
String Query_SQL_INSERT = "INSERT INTO `users_logs` (user_id, name) VALUES ('1','demo');";
PreparedStatement Update_SQL = conn.prepareStatement(Query_SQL_UPDATE);
Update_SQL.execute();
PreparedStatement Insert_SQL = conn.prepareStatement(Query_SQL_INSERT);
Insert_SQL.execute();
Is there any way to do this, or even simplify these commands? If so, what are.
Since you are using free query. I imagine that if you put a ";" and the second instruction, it works.
– mau humor
It seems there’s nothing in the JDBC documentation that says you can’t repurpose, I’ve even done something similar (Insert followed by select) and had no problems. In the case of Statement, you can reuse without problems, but if you need a return, the result is not reusable.
– user28595
@mauhumor had already tried, however unsuccessfully, the reason I do not know. Let’s expect some response and see if the certain diegofm
– Florida
@Florida the way I did was passing the first query, having it executed, then passing the second query to the same statement and running again. Send the two together at once I don’t know if it’s possible(nor recommendable).
– user28595
So I guess you can’t do that, because they’re different operations. I don’t see any problem with the way you’re doing it. Perhaps, to maintain integrity, in case one of the operations fails, it would be good to use transaction.
– mau humor