简体   繁体   English

尝试将Java变量传递到sql字符串中

[英]Trying to pass a Java variable into a sql string

I've had a look around on the web but can't seem to find a definite answer to my question. 我在网上浏览了一下,但似乎找不到确切的答案。

Basically, I have a database and table that are successfully working. 基本上,我有一个数据库和表可以正常工作。 Now I want to read each line from my table one by one and store the result into a array and I am trying to use a for loop to be more professional rather then using repetition. 现在,我想一张一张地读取表中的每一行,并将结果存储到数组中,我正在尝试使用for循环使其更加专业,而不是使用重复。

I have this code 我有这个代码

for (int i=1; i<=8; i++)
{
    String query = "Select * FROM Table1 WHERE ID = i";
    Rs = St.executeQuery(query);
    COL1Title[i] = Rs.getString("CO1Name");
    COL2Age[i] = Rs.getString("CO2Rating");
}

The for loop is in a try catch statement and it's complaining with the error "Unknown column 'i' in 'where clause'" for循环在try catch语句中,并且出现错误“在'where子句'中的未知列'i'”

Im guessing there's a certain way for how variable i is to be inserted in the the query. 我猜想有某种方法可以将i插入查询中。

I should point out ID is a column that has the auto increment feature added on and is primary key if that helps 我应该指出ID是添加了自动递增功能的列,如果有帮助,它是主键

Could anyone help me out here? 有人可以帮我吗?

First, we can simplify the task be executing a single query. 首先,我们可以简化执行单个查询的任务。 Note the addition of the range limit and the ORDER BY - without an ORDER BY the results have an unspecified order! 请注意,范围限制和ORDER BY的加法-不带ORDER BY的结果具有不确定的顺序!

PreparedStatement stmt = "Select ID, CO1Name, CO2Rating"
    + " FROM Table1"
    + " WHERE ID >= ? AND ID <= ?"
    + " ORDER BY ID";

And bind in placeholders (unless there is good reason otherwise, always use placeholders when injecting data into a query). 并绑定占位符(除非有充分的理由,否则在将数据注入查询时始终使用占位符)。 The values could have been hard-coded above in this case, just as they are hard-coded in the for-loop, but the binding is shown here for future reference: 在这种情况下,这些值可能已经在上面进行了硬编码,就像它们在for循环中被硬编码一样,但是此处显示了绑定以供将来参考:

stmt.setInt(1, 1);
stmt.setInt(2, 8);

Then execute the query: 然后执行查询:

ResultSet rs = stmt.executeQuery();

And iterate the results. 并迭代结果。 Note that rs.next() must be invoke once before any column is read (the cursor starts before any records) and, in this case, it makes it easy to handle a bunch of results. 请注意,在读取任何列之前(光标任何记录之前开始rs.next()必须先调用一次rs.next() ,在这种情况下,它可以轻松处理大量结果。

while (rs.next()) {
   int id = rs.getInt("ID");
   String title = rs.getString("CO1Name");
   String name = rs.getString("CO2Rating");
   // do stuff with this record
}

Note that even though the ORDER BY guarantees that the results are iterated in order of ID, assuming a database cardinality rule ensures each result has a unique ID, there may be 0 to 8 records returned - that is, non-existent records may need to be detected/handled separately. 请注意,即使ORDER BY保证结果按照ID的顺序进行迭代,但假设数据库基数规则确保每个结果都有唯一的ID,则可能会返回0到8条记录-也就是说,不存在的记录可能需要分别检测/处理。

Also (but not shown), make sure to cleanup ( close ) the ResultSet when done: use a try/finally or try-with-resources construct. 同样(但未显示),请确保完成后清理( close )ResultSet:使用try / finally或try-with-resources构造。

您需要以整数形式将i传递给字符串,并替换为:

String query = String.format("Select * FROM Table1 WHERE ID = %d",i);

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

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