3

Is it possible to use PreparedStatement for several statements?

E.g., I mean if String sql = "INSERT INTO OR INGNORE ... ; UPDATE ... ; INSERT INTO ..."; this

PreparedStatement pre = conn.prepareStatement(sql);
//...
pre.executeUpdate();

executes only first statement "INSERT INTO OR INGNORE ... (until semicolon). Is it possible to execute all at once?

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • what you are looking for is batch. you insert all statements in a batch and then execute the batch – XtremeBaumer Dec 21 '16 at 08:02
  • there is mysql specific solution: http://stackoverflow.com/questions/10797794/multiple-queries-executed-in-java-in-single-statement – Vladimir Dec 21 '16 at 08:18

2 Answers2

3

Is it possible to execute all at once?

this might depend on the implementation of the JDBC driver by the database vendor but in general I would not expect that to work.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
1
String[] query = { "insert into Gericht (classification,date,name,preisExtern,preisIntern) values (?,?,?,?,?)",
            "test" };
    PreparedStatement stmt;
    for (String str : query) {
        stmt = c.prepareStatement(str);
        stmt.addBatch();
    }
    stmt.executeBatch();

here is an example on how to use batch. if its not what you want, please tell me so.

as wished:

@XtremeBaumer how about when the parameters in prepared statement change? how can you change it dynamically?

Answer:

you can't. if you want several different queries to be executed at once, you can only use fixed statement, otherwise your code will be very large, and then you can do it manually by setting the parameters and adding it to a batch. batches are good if you have 1 query that gets different parameters and you want to add all at once

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • You wanna show me how to execute the same statement several times. Yes? – Vyacheslav Dec 21 '16 at 08:08
  • have a look now. was it really that hard to guess how to use several statements? – XtremeBaumer Dec 21 '16 at 08:12
  • @XtremeBaumer how about when the parameters in prepared statement change? how can you change it dynamically? – msagala25 Dec 21 '16 at 08:16
  • you can't. if you want several **different** queries to be executed at once, you can only use fixed statement, otherwise your code will be very large, and then you can do it manually by setting the parameters and adding it to a batch. batches are good if you have 1 query that gets different parameters and you want to add all at once – XtremeBaumer Dec 21 '16 at 08:19
  • Add your last comment to the answet. I will approve it in this case – Vyacheslav Dec 21 '16 at 09:38
  • This code is not going to work, it will only execute the last statement that was prepared (and if it works, that JDBC driver has a serious bug). – Mark Rotteveel Dec 22 '16 at 12:23
  • @MarkRotteveel why should it only execute the last statement? and why should the driver have a bug? – XtremeBaumer Dec 22 '16 at 14:56
  • Because you are preparing a new statement for each statement, calling add batch, and only calling execute batch on the last statement in the list. If that actually works (executes all those statements), that would mean that the driver collects batches over multiple statement objects, which goes against the JDBC specification (hence: a bug). `addBatch` on a prepared statement is to execute the same statement with multiple sets of parameters for that statement. – Mark Rotteveel Dec 22 '16 at 14:59