如何在JDBC查询中传递参数以创建表? [英] How to pass parameter in JDBC query for creating table?
问题描述
我正在尝试编写一个SQL查询,我想在运行时传递表名.查询:
I am trying to write a sql query where i want to pass the table name at run-time. Query:
create table mytable (time datetime, price double)
因此,我创建了一个通用查询:
So, i have created a generic query :
create table ? (time datetime, price double)
然后我在运行时将表名传递为:
and then i am passing the table name at run-time as:
PreparedStatement preparedStatement = connection.prepareStatement(CREATE_TABLE_QUERY);
preparedStatement.setString(1, tableName);
preparedStatement.executeUpdate(CREATE_TABLE_QUERY);
在运行此代码时,我得到了例外,我的查询语法不正确.我们是否使用其他方法传递用于创建表的参数?
On running this code i am getting exception that my query syntax is not correct. Do we pass the parameter for creating table using some other technique?
推荐答案
据我所知,准备好的语句不允许设置表名.通常它仅支持? DML语句(插入,更新,删除,选择)中的值,而不是DDL.
As far as I know, prepared statements does not allow setting a table name. Mostly it only supports ? in values in DML statements (insert, update, delete, select), not DDL.
要创建表,必须创建一个SQL字符串,例如
To create table, you must create a SQL string, such as
String s = "create table " + TABLE_NAME + " (col int) ";
stmt.execute(s);
这篇关于如何在JDBC查询中传递参数以创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!