String sql2 = "create table ? ( id int not null auto_increment, fullname
varchar(30) not null, primary key (id) )";
PreparedStatement stmt2 = conn.prepareStatement(sql2);
stmt2.setString(1, username);
stmt2.execute();
stmt2.close();
从上面的语句中,我收到错误消息(“john”作为表名):
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error
in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near ''john' ( id int not null
auto_increment, fullname varchar(30) not null, primary ' at line 1
eclipse 说错误在这一行:
stmt2.execute();
请帮助大家...tq
最佳答案
不能在 CREATE TABLE 语句中使用表名参数。
而是简单地使用变量构建 SQL 字符串:
String sql2 = "create table " + username + " ( id int not null auto_increment, fullname
varchar(30) not null, primary key (id) )";
Statement stmt2 = conn.createStatement();
stmt2.executeUpdate(sql2);
关于java - 如何在preparedstatement中使用JDBC通配符创建mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37885777/