IN子句中參數(shù)列表的PreparedStatement如何在執(zhí)行查詢(xún)時(shí)在JDBC中的prearedStatement中為in子句設(shè)置值。例子:connection.prepareStatement("Select * from test where field in (?)");如果這個(gè)in-子句可以保存多個(gè)值,我如何才能做到這一點(diǎn)。有時(shí)我事先知道參數(shù)列表,有時(shí)我不知道。如何處理這個(gè)案子?
3 回答

www說(shuō)
TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
List possibleValues = ... StringBuilder builder = new StringBuilder();for( int i = 0 ; i < possibleValue.size(); i++ ) { builder.append("?,");}String stmt = "select * from test where field in (" + builder.deleteCharAt( builder.length() -1 ).toString() + ")";PreparedStatement pstmt = ...
int index = 1;for( Object o : possibleValue ) { pstmt.setObject( index++, o ); // or whatever it applies }
添加回答
舉報(bào)
0/150
提交
取消