0

I'm using java with postresql driver and I want to run multiply queries in one statement (if possible)

I'm aware of running batch queries like this:

pstmt = connection.createStatement();
pstmt.addBatch("query1 here");
pstmt.addBatch("query2 here");
pstmt.executeBatch();

but is there a way to execute multiply queries with parameters?

Something like this:

pstmt = connection.prepareStatement(
           "UPDATE TABLE Example SET name=? WHERE id = ?;\n" +
           "UPDATE TABLE Other SET name=? WHERE id = ?;"
        );
pstmt.setInt(1, "name");
pstmt.setInt(2, id1);
pstmt.setInt(3, "kuku");
pstmt.setInt(4, id2);
pstmt.execute();

--

UPDATE

The link in the comment (this) answers half of my question.

In addition, I want to know if there is a way to run two different queries.

Lets say the queries above were:

  UPDATE table1 SET col=? WHERE id=?;
  INSERT INTO table2 VALUES (?, ?, ?);

As it seems there is not way to do that... :/ Thanks.

jurl
  • 2,504
  • 1
  • 17
  • 20
  • 1
    Using a `PreparedStatement` demonstrated [here](https://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/) – MadProgrammer Dec 10 '17 at 22:20
  • Thanks! I didn't found this link at the question you mention. Still this is about one type of query (that will be run with different parameters in each time), can I do this with two types of queries? (I updated the question with explanation) – jurl Dec 11 '17 at 09:40
  • 1
    You could try, but I have the feeling that the answer is no – MadProgrammer Dec 11 '17 at 10:16
  • I found this question: [multiple-queries-executed-in-java-in-single-statement](https://stackoverflow.com/questions/10797794/multiple-queries-executed-in-java-in-single-statement) seems to have the answer :) Thanks – jurl Dec 11 '17 at 11:34
  • Just got to remember, that answer is for MySQL – MadProgrammer Dec 11 '17 at 18:58

0 Answers0