我对使用 Java 的 MySQL 还很陌生,但是我已经执行了一些成功的 INSERT 查询,但是似乎无法在没有 MySQLSyntaxErrorException
异常的情况下执行 CREATE TABLE 查询。我的代码如下:
Statement stmt;
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url, "root", "password");
stmt = con.createStatement();
String tblSQL = "CREATE TABLE IF NOT EXISTS \'dev\'.\'testTable\' (\n"
+ " \'id\' int(11) NOT NULL AUTO_INCREMENT,\n"
+ " \'date\' smallint(6) NOT NULL\n"
+ ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
stmt.executeUpdate(tblSQL);
stmt.close();
con.close();
错误如下:
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 ''dev'.'testTable' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'date' smallint(6) N' at line 1
如果有人能发现此查询中的错误,我将不胜感激,因为我已尝试在 phpMyAdmin 中执行此操作并且它可以正常工作。
最佳答案
\n
会让按下回车效果 :) 让它像
String tblSQL = "CREATE TABLE IF NOT EXISTS `dev`.`testTable`"
+ "("
+ "id INTEGER(11) NOT NULL AUTO_INCREMENT primary key,"
+ "date smallint(6) NOT NULL"
+ ")"
+ "ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
关于java - 创建表时出现 MySQLSyntaxErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4646996/