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.

5
  • 1
    Using a PreparedStatement demonstrated here Commented Dec 10, 2017 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) Commented Dec 11, 2017 at 9:40
  • 1
    You could try, but I have the feeling that the answer is no Commented Dec 11, 2017 at 10:16
  • I found this question: multiple-queries-executed-in-java-in-single-statement seems to have the answer :) Thanks Commented Dec 11, 2017 at 11:34
  • Just got to remember, that answer is for MySQL Commented Dec 11, 2017 at 18:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.