Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with my database code?

Tags:

java

I'm trying to check if my String key is in the NETFLIX column.

public boolean checkSerial(String key){
    boolean isValid = false;
    sql = "Select * from KEYS WHERE NETFLIX=?";
    try{
        ps = con.prepareStatement(sql);
        ps.setString(1, key);
        rs = ps.executeQuery();
        if(rs.next())
            isValid = true;
    }catch(SQLException e){
        System.out.println(e);
    }
    return isValid;
}

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'KEYS WHERE NETFLIX='IPMAN'' at line 1

like image 600
NgoCuong Avatar asked Jan 09 '23 06:01

NgoCuong


1 Answers

KEYS is a MySQL reserved keyword. So possibly you'll get an error even if there's nothing wrong with the query.

Either you've to avoid using MySQL reserved keywords, which can be found at

https://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

or

Use appropriate quotes for the keywords if you don't want to change your existing table. Like,

Select * from `KEYS` WHERE NETFLIX=?

Note: It's not a single quote, it's a symbol which is present along with the tilde symbol below the escape button (It's called backtick according to comment).

like image 156
The Coder Avatar answered Jan 18 '23 06:01

The Coder