简体   繁体   English

如何使用JDBC和Statement.executeQuery()按顺序执行语句?

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

I use JDBC to insert some values into a table. 我使用JDBC将一些值插入表中。 I iterate over the statemets and I wnt to execute them in the order they come: 我遍历了statemets,并按照它们出现的顺序执行它们:

 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)? stmt.executeQuery(statement)打开一个进程,然后根据该语句(插入的值的数量等)以较低的速度执行。 And it should be synchronized? 而且应该同步吗?

Could you give me an example of how can I execute statementsin the order the foreach give them? 您能否举一个例子,说明如何按照foreach给它们的顺序执行语句?

You are iterating and printing a Java ArrayList or some other structure and that Java data structure has insertion - order maintained. 您正在迭代并打印Java ArrayList或其他结构,并且该Java数据结构具有插入顺序

Your table is a RDBMS table and it doesn't have any such automatic ordering. 您的表是RDBMS表,没有任何此类自动排序。 In your SELECT , you can use SQL . SELECT ,可以使用SQL。 ORDER BY clause on a column to order result set rows in any order that you want. 列上的ORDER BY子句以所需的任何顺序对结果集行进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Statement.executeQuery的ResultSet不能为null - The ResultSet can not be null for Statement.executeQuery 使用statement.executeQuery编写的Select语句无法选择具有-(破折号)的值 - Select statement written using statement.executeQuery can't select values having - (dash) Statement.executeQuery() 花费太多时间。 有没有办法优化这个? 我正在使用 Athena DB - Statement.executeQuery() taking too much time. Is there any way to optimise this? I am using Athena DB Statement.executeQuery() 和 SQL 注入 - Statement.executeQuery() and SQL injection statement.executeQuery () 不起作用 - statement.executeQuery () does not working 执行Statement.executeQuery()后如何处理Java冻结? - How to handle Java freezing upon executing Statement.executeQuery()? oracle中的statement.executeQuery非常慢且不一致 - statement.executeQuery in oracle is very slow & inconsistent ResultSet是一个接口。 Statement.executeQuery(String)如何创建其对象。 - ResultSet is a interface. How does Statement.executeQuery(String) create its object.? Statement.executeQuery()为SELECT语句返回什么 - What does Statement.executeQuery() return for a SELECT statement Statement.executeQuery 为每个 SQL 查询花费相同的时间 - Statement.executeQuery taking same amount of time for every SQL query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM