如何为java预处理语句插入使用tablename变量 [英] How to use a tablename variable for a java prepared statement insert
问题描述
我正在使用java PreparedStatment对象来构造一系列批量INSERT查询。查询语句的格式为...
I am using a java PreparedStatment object to construct a series of batched INSERT queries. The query statement is of the format...
String strQuery = "INSERT INTO ? (col1, col2, col3, col4, col5) VALUES (?,?,?,?,?,?);";
...所以字段值和表名都是变量(即我有多个表,其中包含相同的列格式,每个插入将指向另一个插入格式。如果我删除?,我可以让执行工作tablename变量和硬代码,但每个预处理语句将被插入到不同的表中,因此需要保留一个变量,我在使用...执行批处理查询之前立即填充...
...so both field values and the tablename are variables (ie. I have multiple tables with the same column format of which each insert will be directed to a different one). I can get the executes to work if I remove the "?" tablename variable and hard code but each prepared statement will be inserted into a different table so needs to remain a variable I populate immediately prior to executing the batch query using...
stmt.setString(1, "tableName1");
我怎么能让它成为动态变量?
How can I let this be a dynamic variable please?
推荐答案
你做不到。您需要使用字符串连接/占位符和String.format构造sql。预准备语句用于列值而非表名。
You can't. You need to contruct the sql with string concatenation/placeholder with String.format. prepared statement is for the column values not for table name.
这篇关于如何为java预处理语句插入使用tablename变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!