om.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法中有错误;

时间:2017-07-20 07:28:22

标签: java mysql sql jdbc

我试图创建2个表,但我不完全明白我哪里出错了。它显然是一个语法错误,而且我一般都是SQL的新手,有人可以向我解释我在这里做错了吗?

 String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
                "uuid CHAR(36) NOT NULL," +
                " team_name VARCHAR(255) NOT NULL," +
                " BOOLEAN manager NOT NULL," +
                " BOOLEAN team_chat NOT NULL," +
                " PRIMARY KEY (uuid))";

        String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
                "team_name VARCHAR(255) NOT NULL," +
                " team_password VARCHAR(255) NOT NULL" +
                " BOOLEAN friendly_fire NOT NULL," +
                " VARCHAR(255) hq," +
                " VARCHAR(255) rally," +
                " PRIMARY KEY (team_name))";

这是我的堆栈跟踪:

WARN 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 'manager NOT NULL, BOOLEAN team_chat NOT NULL, PRIMARY KEY (uuid))' at line 1

用于执行的代码:

    private void createTables(String tableName, String secondTableName) throws SQLException {
    try {
        checkConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {

        String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
                "uuid CHAR(36) NOT NULL," +
                " team_name VARCHAR(255) NOT NULL," +
                " BOOLEAN manager NOT NULL," +
                " BOOLEAN team_chat NOT NULL," +
                " PRIMARY KEY (uuid))";

        String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
                "team_name VARCHAR(255) NOT NULL," +
                " team_password VARCHAR(255) NOT NULL" +
                " BOOLEAN friendly_fire NOT NULL," +
                " VARCHAR(255) hq," +
                " VARCHAR(255) rally," +
                " PRIMARY KEY (team_name))";

        PreparedStatement preparedStatement = connection.prepareStatement(query);
        PreparedStatement preparedStatement2 = connection.prepareStatement(query2);

        preparedStatement.execute();
        preparedStatement2.execute();
        main.getLogger().log(Level.INFO, "Tables " + tableName + " and " + secondTableName + " were successfully created.");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close();
    }

}

2 个答案:

答案 0 :(得分:2)

问题是表定义中存在MAXVARCHAR期望将一个数字作为参数,因此您需要将MAX定义为变量并将其传递(来自Java),或者只需将MAX替换为数字,例如:

String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
                "uuid CHAR(36) NOT NULL," +
                " team_name VARCHAR( + " + MAX + ") NOT NULL," +
                " BOOLEAN manager NOT NULL," +
                " BOOLEAN team_chat NOT NULL," +
                " PRIMARY KEY (uuid))";

OR

String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
                "uuid CHAR(36) NOT NULL," +
                " team_name VARCHAR(255) NOT NULL," +
                " BOOLEAN manager NOT NULL," +
                " BOOLEAN team_chat NOT NULL," +
                " PRIMARY KEY (uuid))";

如果您不知道最大长度,则可以使用LONGTEXT作为类型。

此外,我们需要修正type和列名称的顺序,它必须是manager BOOLEAN而不是BOOLEAN manager,例如:

    String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
                "uuid CHAR(36) NOT NULL," +
                " team_name VARCHAR(255) NOT NULL," +
                " manager BOOLEAN NOT NULL," +
                " team_chat BOOLEAN NOT NULL," +
                " PRIMARY KEY (uuid))";

    String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
            "team_name VARCHAR(255) NOT NULL," +
            " team_password VARCHAR(255) NOT NULL" +
            " friendly_fire BOOLEAN  NOT NULL," +
            " hq VARCHAR(255)," +
            " rally VARCHAR(255)," +
            " PRIMARY KEY (team_name))";

答案 1 :(得分:0)

列名必须在数据类型之前。所以

BOOLEAN manager NOT NULL,
BOOLEAN team_chat NOT NULL,

应该是

manager BOOLEAN NOT NULL,
team_chat BOOLEAN NOT NULL,