我想用这个方法创建一个新的mySQL表。数据库连接工作正常。我做了很多这样的工作语句,但是这个创建表的语句是不起作用的。所以错误一定出在这几行代码中。在按钮上,我添加了错误消息。
public void createNewTable(){
DatabaseConnection connectNow = new DatabaseConnection();
Connection connectDb = connectNow.getConnection();
String tableName = "users";
try {
String createTable = "CREATE TABLE ? (firstname varchar(30), lastname varchar(30), tel integer" +
"email varchar(40), arrive_stamp timestamp, quit_stamp timestamp)";
PreparedStatement myStmt = connectDb.prepareStatement(createTable);
myStmt.setString(1, tableName);
myStmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
e.getCause();
}
}
Illegal operation on empty result set.
java.sql.SQLSyntaxErrorException: 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 ''users' (firstname varchar(30),
lastname varchar(30), tel integer, email varchar' at line 1发布于 2020-12-15 21:33:31
tel和email列之间缺少,。正如Tim在注释中提到的,您应该在sql语句本身中使用表名。
您的sql语句应该是
String createTable = "CREATE TABLE your_table_name (firstname varchar(30), lastname varchar(30), tel integer, " + -- added , at the end
"email varchar(40), arrive_stamp timestamp, quit_stamp timestamp)";https://stackoverflow.com/questions/65306806
复制相似问题