Accessing Hyphenated Column Names in MySQL with Java
When working with MySQL databases, you may encounter situations where column names contain hyphens (-) characters. However, when accessing these columns from Java code, the hyphens can break the column names and result in errors.
To overcome this issue, enclose the hyphenated column names within back-ticks ( ). Back-ticks serve as delimiters that prevent the database from breaking the column name at the hyphen.
For example, if you have a column named "air_port", instead of accessing it as "air_port" in your Java code, you should enclose it within back-ticks, like this:
ResultSet rs = statement.executeQuery("SELECT * FROM `air_port`");
By enclosing the column name within back-ticks, MySQL will interpret it as a single entity and not split it at the hyphen. This allows you to successfully access and retrieve data from the column.
Note that the character set used in the database does not affect this issue. The back-ticks method is a universal approach for escaping hyphens in column names in MySQL regardless of the character set.
The above is the detailed content of How to Access Hyphenated Column Names in MySQL Using Java?. For more information, please follow other related articles on the PHP Chinese website!