无法使用prepareStatement创建表 [英] Cant create table with preparedStatement
本文介绍了无法使用prepareStatement创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法使用preparedStatement
在数据库(mySQL)中创建表,并尝试使用preparedStatement.setInteger()
输入将来的表的名称:
I can't create a table in the database (mySQL), using preparedStatement
and try to enter name of future table with preparedStatement.setInteger()
:
static String queryCreateTable = "CREATE TABLE ?" +
"(ID INTEGER not NULL ," +
"BRAND VARCHAR(40)," +
"MODEL VARCHAR(40)," +
"YEAR INTEGER not NULL," +
"NOVELTY BINARY," +
"PRIMARY KEY ( ID ))";
然后我尝试在用户输入表名之后构造并调用该语句:
And then I try to construct and call the statement after inputing name of table by user:
newNameOfTable = JOptionPane.showInputDialog("Connected for saving data. " +
"Input name of new table:");
pStatement = connection.prepareStatement(queryCreateTable);
pStatement.setString(1, newNameOfTable);
pStatement.executeUpdate();
如果我在不输入名称的情况下尝试执行它(如常量字符串:"CREATE TABLE newtable(...)",但我需要输入名称),效果很好.
It works well if I try to execute it without entering name (like a constant string: "CREATE TABLE newtable (...)" but I need to enter name..
推荐答案
读取表名后,您将不得不格式化字符串,例如:
You will have to format the string after reading the table name, something like:
static String queryCreateTable = "CREATE TABLE {0}" +
"(ID INTEGER not NULL ," +
"BRAND VARCHAR(40)," +
"MODEL VARCHAR(40)," +
"YEAR INTEGER not NULL," +
"NOVELTY BINARY," +
"PRIMARY KEY ( ID ))";
然后创建如下内容:
newNameOfTable = JOptionPane.showInputDialog("Connected for saving data. " +
"Input name of new table:");
statement = connection.createStatement();
statement.execute(MessageFormat.format(queryCreateTable, newNameOfTable));
这篇关于无法使用prepareStatement创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文