2014-03-02 29 views
0

如何在准备好的语句中设置列表(statement.setString(1,productCode);)。 看下面我的代码片段。如何在准备好的语句中设置列表

感谢

public static List<Message> listAllWonBids(List<Message> productCode, 
    Connection connection) { 

    List<Message> winners = new ArrayList<Message>(); 

    String sql = "SELECT b.`id`, b.`msisdn` as msisdn ,b.`productname` as productname, b.`productcode` as productcode, max(b.`amount`) as amount FROM " 
     + TableNames.SAVEDBIDSTABLE 
     + " b where productcode = ? " 
     + " group by amount order by productcode, amount desc limit 1"; 

    PreparedStatement statement = null; 
    ResultSet resultSet = null; 

    try { 
     LOGGER.info(sql); 

     if (connection == null || connection.isClosed()) 
      connection = DBConnection.getConnection(); 

     statement = connection.prepareStatement(sql); 

     **statement.setString(1, productCode);** 

     resultSet = statement.executeQuery(); 

注:PRODUCTCODE从下面

public static List<Message> allProductCode(Connection connection) { 
    List<Message> productcodes = new ArrayList<Message>(); 

    PreparedStatement statement = null; 
    ResultSet resultSet = null; 

    String sql = "SELECT `productCode` FROM " + TableNames.AUCTIONTABLE1 
     + " WHERE date(`endDate`) = curdate() order by `id` desc"; 
+0

这个问题的答案可能会帮助:http://stackoverflow.com/questions/178479/preparedstatement-in-clause-alternatives –

回答

1

你可以结合这两个查询。喜欢的东西:

String sql = "SELECT b.`id`, b.`msisdn` as msisdn ,b.`productname` as productname, b.`productcode` as productcode, max(b.`amount`) as amount FROM " 
+ TableNames.SAVEDBIDSTABLE + " b where productcode in (SELECT `productCode` FROM " 
+ TableNames.AUCTIONTABLE1 + " WHERE date(`endDate`) = curdate() order by `id` desc) group by amount order by productcode, amount desc limit 1"; 

那么你就不需要任何参数

+0

谢谢先生,上述查询的作品,但列出了所有的产品代码。我想获得每个产品代码的最大金额,这就是为什么我限制1(意思是每个产品代码的最大值)。也许你有不同的方式来实现这一点。非常感谢 – pmaingi

2

这是不可能出另一个列表来了,要么你生成一个where子句以编程方式使用IN

"where productcode in (" + listToStringEncoding + ")" 

或者您遍历列表并多次调用该语句。

另一种可能是加入了2个语句...

+0

谢谢gerritCap,让我试试如上。 – pmaingi

相关问题