我正在使用预准备语句对我的数据库执行某些操作。我的表格如下:
+--------------------------------------------------------+
|Field | Type | Size | Key | Text |
+--------------------------------------------------------+
|X1DEL | Char | 1 | 2 | Status |
|X1CMP | Zoned | 3 | 1 | Company No. |
|X1ORD | Zoned | 9 | 2 | Order Number |
|X1TYPE | Char | 1 | 1 | Transaction Type |
|X1ORDT | Zoned | 8 | 0 | Order Date |
|X1CUST | Char | 10 | 0 | Customer Number |
|X1PO | Char | 20 | 0 | PO Number |
|X1OTOT | Zoned | 11 | 2 | Order Total |
+--------------------------------------------------------+
我正在为单个参数执行一个删除查询delete from my_table where field=?
。
我创建了2个参数的查询delete from my_table where field=? and type=?
现在我再次想要执行4到5个参数的查询。我正在编写一个delete from my_table where field=?,type=?,size=? and key=?
的查询
我试过这个查询但没有得到结果。
答案 0 :(得分:3)
按照设计,use prepared statements使用任意数量的参数构建查询。
final String sql = "delete from my_table where field=? and type=? and size=? and key=?";
如果您的参数位于collection
,则可以使用链接教程和"make coding easier by using a for loop or a while loop to set values for input parameters"
的建议。根据您提供的type
和参数数量,您需要为每个参数调用相应的.set
方法。
//assuming con (DB connection) is properly setup
final PreparedStatment statement = con.prepareStatement(sql);
statement.setString(1, "X1DEL");
statement.setString(2, "CHAR");
statement.setInt(3,1);
statement.setInt(4, 2);
//... etc
您必须记住准备好的语句是1-based
索引。
答案 1 :(得分:1)
为什么在2参数示例显示正确的事情时,您是这样做的?
delete from my_table where field = ? and type = ? and size = ? and key = ?