Search code examples
javamysqljdbcmysql-error-1064

JDBC 1064 SQL Syntax Error


I want to handle a MySQL database using JDBC from inside an operation of a Java Web Service. I have :

stmt = conn.createStatement();

String query = "CREATE TABLE Basestations (Network_Id INT,"
                    + "Basestation_Id INT, Signal_Strength DOUBLE,"
                    + "Frequency DOUBLE, Network_Type VARCHAR(40), Max_Bitrate DOUBLE,"
                    + "Guaranteed_Bitrate DOUBLE, Net_Load INT," 
                    + "Provider VARCHAR(45), Range DOUBLE, X INT, Y INT,"
                    + "Port INT, Charging VARCHAR(40), Active BOOLEAN);";

stmt.execute(query);   

to create my table, but this causes:

INFO: SQL Exception: INFO: State : 42000 INFO: Message: 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 'Range DOUBLE, X INT, Y INT,Port INT, Charging VARCHAR(40), Active BOOLEAN)' at line 1 INFO: Error : 1064

What is wrong with my syntax?


Solution

  • RANGE is a reserved keyword is MySQL. Rather surround Range with a ` (backtick).

    String query = "CREATE TABLE Basestations (Network_Id INT,"
                        + "Basestation_Id INT, Signal_Strength DOUBLE,"
                        + "Frequency DOUBLE, Network_Type VARCHAR(40), Max_Bitrate DOUBLE,"
                        + "Guaranteed_Bitrate DOUBLE, Net_Load INT," 
                        + "Provider VARCHAR(45), `Range` DOUBLE, X INT, Y INT,"
                        + "Port INT, Charging VARCHAR(40), Active BOOLEAN);";