简体   繁体   中英

How can I execute statements with in order using JDBC and Statement.executeQuery()?

I use JDBC to insert some values into a table. I iterate over the statemets and I wnt to execute them in the order they come:

 for (String statement : statements) {
      System.out.println(statement);
      stmt.executeQuery(statement);
 }

In console the statements are printed:

insert into results (cod_prod,cod_prod1,scor_prod1,cod_prod2,scor_prod2,cod_prod3,scor_prod3,cod_prod4,scor_prod4) values(9100100123256,8692857013184,9.93973798303341,8710908117671,9.653580483609403,6423038000042,9.181317277303606,90005848,9.181317277303606)
insert into results (cod_prod) values(90494741)
...

And I expect they are executed in this order, but when I query the database I get the rows in different order:

img

Why it happend in this way? The stmt.executeQuery(statement) open a process and it executes lower of faster depending on the statement (number of values inserted, etc)? And it should be synchronized?

Could you give me an example of how can I execute statementsin the order the foreach give them?

You are iterating and printing a Java ArrayList or some other structure and that Java data structure has insertion - order maintained.

Your table is a RDBMS table and it doesn't have any such automatic ordering. In your SELECT , you can use SQL . ORDER BY clause on a column to order result set rows in any order that you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM