简体   繁体   English

使用Java的PreparedStatement将数组传递给SQL查询

[英]Passing an Array to a SQL query using Java's PreparedStatement

I've been having a little trouble passing an array into a SQL query using Java's prepared statements. 使用Java的准备好的语句将数组传递到SQL查询中时,我一直遇到一些麻烦。 I had first tried the sourceforge driver, however I would get the AbstractMethodError when I call setArray method. 我首先尝试了sourceforge驱动程序,但是当我调用setArray方法时会收到AbstractMethodError。 Not knowing the solution to that I swapped to the Microsoft sqlserver driver, but now I get a different error entirely, which is "java.sql.SQLFeatureNotSupportedException: This operation is not supported." 我不知道我换成了Microsoft sqlserver驱动程序的解决方案,但是现在我完全得到了另一个错误,即“ java.sql.SQLFeatureNotSupportedException:不支持此操作”。 Tried a whole bunch of things to try and resolve this but nothing appears to work. 尝试了很多方法来尝试解决此问题,但似乎没有任何效果。

My Java code looks similar to examples I've seen here and on the internet, and is as follows, 我的Java代码类似于在这里和互联网上看到的示例,如下所示,

PreparedStatement ps = connection.prepareStatement(query);
String[] suppliers = {"21","2774","120563","3714","59"};
ps.setArray(1, connection.createArrayOf("text", suppliers));
ResultSet rs = ps.executeQuery();

Example of my SQL queries. 我的SQL查询示例。 The only line of real interest is the line before last where I have added the '?' 真正感兴趣的唯一一行是我在最后一行添加“?”的那一行 character, which as I understand it is how I pass a parameter to a SQL Query. 字符,据我了解,这就是我如何将参数传递给SQL查询。

productsPrices.query = select contract.supplierid as 'hotelid' \
  , round((rate.s1/money.buy)*euro.sell,0) as "single" \
  , round((rate.s2/money.buy)*euro.sell,0) as "double" \
  ,service.Name as 'roomtype' \
  ,stock.alloc - stock.taken as 'stock.available' \
  , contract.notes as 'boardType' \
  , object.name as 'occupancy' \
  ,object.cap as 'capacity' \
  ,object.mincap as 'min capacity' \
  ,stock.date as 'date' \
from stock stock \
  inner join rate rate on stock.rateid = rate.id \
  inner join contract contract on rate.contractid = contract.id \
  inner join service service on contract.serviceid = service.ID \
  inner join object object on service.objectid = object.ID \
  inner join band band on contract.termsid = band.ID \
  inner join Money money on band.moneyid = money.id \
  inner join Money euro on euro.Name  = 'Euros' \
where stock.date > getdate() \
and stock.closed = 0 \
and (stock.alloc - stock.taken) > 0 \
and stock.date > getdate() \
      and contract.supplierid in (?) \
      and contract.Finish > GETDATE()

I solved same issue by dynamically generating string with required number of question marks. 我通过动态生成带有所需数量的问号的字符串来解决了同一问题。 Here's a snippet:- 这是一个片段:

String param = "(";
for(int i=0;i<suppliers.length;i++){
param = param+"?,";
}
param = param.substring(0,param.length()-1);
param=param+")";

query = query + param;

PreparedStatement ps = connection.prepareStatement(query);

for(int i=0;i<suppliers.length;i++){
ps.setString(i+1,suppliers[i]);
}

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

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