2014-03-02 49 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

相關問題