Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT * FROM table throws error in JDBC

Tags:

java

jdbc

I am trying to make a select from a table, which works fine with every other table in my database, but when I try the following I recieve an error:

db.makeQuery("SELECT * FROM References");

Which calls:

public ResultSet makeQuery(String query) throws Exception
{
    preparedStatement = connect.prepareStatement(query);
    resultSet = preparedStatement.executeQuery(query);
    return resultSet;

}

It then throws the following error:

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 'References' at line 1

I seems very strange to me, since this statement works:

db.makeQuery("select * from Products");
like image 622
Medo Avatar asked Mar 05 '26 08:03

Medo


2 Answers

References is a keyword in SQL, so you better avoid it for table names. (See for instance this documentation.)

As suggested by Nishant, you can use reserved words in queries if you escape them with backticks.

Related question:

  • Using MySQL keywords in a query?
like image 100
aioobe Avatar answered Mar 07 '26 21:03

aioobe


use

 db.makeQuery("SELECT * FROM `References`");

if you can, better, avoid having names that are MySQL keywords. As suggested by aioobe

like image 30
Nishant Avatar answered Mar 07 '26 23:03

Nishant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!