如何为java预处理语句插入使用tablename变量 [英] How to use a tablename variable for a java prepared statement insert

查看:243
本文介绍了如何为java预处理语句插入使用tablename变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆