Reputation: 697
I have a simple table having id(autoincrement) and name and i am trying to do a very simple insert query
if (connect != null) {
query = new StringBuffer("");
query.append("INSERT INTO ");
query.append("GROUP( ");
query.append("GROUP_NAME ");
query.append(") values (");
query.append("?)");
System.out.println(query.toString());
stmt = connect.prepareStatement(query.toString());
int i = 0;
stmt.setString(1, group.getGroupName());
records = stmt.executeUpdate();
}
but it gives me an exception with stacktrace:
2016-02-09T10:00:44.876+0500|Severe: 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 'GROUP( GROUP_NAME ) values ('Hockey')' at line 1
I have searched about such problems but couldn't find a proper solution.
Upvotes: 1
Views: 6044
Reputation: 4102
GROUP
is a reserved word in MySQL.
Most of the reserved words in the table are forbidden by standard SQL as column or table names (for example, GROUP).
Preferably, you wouldn't use reserved words as identifiers at all, for exactly this reason. If you absolutely must, then use backticks ` to quote reserved words, as described in Section 9.2.1 of the MySQL Reference Manual.
Upvotes: 1
Reputation: 50766
You can't use GROUP
as a name in MySQL unless it's enclosed in backticks:
INSERT INTO `group` (GROUP_NAME) VALUES (?)
Upvotes: 0