如何将表名变量用于 Java 准备语句插入 [英] How to use a tablename variable for a java prepared statement insert
问题描述
我正在使用 java PreparedStatment 对象来构造一系列批量插入查询.查询语句格式为...
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 (?,?,?,?,?,?);";
...所以字段值和表名都是变量(即,我有多个具有相同列格式的表,其中每个插入都将定向到不同的表).如果我删除?",我可以让执行工作表名变量和硬代码,但每个准备好的语句都将插入到不同的表中,因此需要保留一个变量,我在执行批处理查询之前立即使用...
...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 准备语句插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!