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

I'm pretty dumbfounded with this error. Basicly had this base code for every of my project with database but somehow it's just dead.

public ArrayList<Character> display()
    {
        ArrayList<Character> collection = new ArrayList<>();
        try
        {
            stat = (Statement) connect.createStatement();

            result = stat.executeQuery("SELECT * FROM character");

            while(result.next())
            {
                Character a = new Character(
                            result.getString("user"),
                            result.getInt("win"),
                            result.getInt("lose"),
                            result.getInt("draw")
                            );
                collection.add(a);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return collection;
    }
        chara = new Character();

        for(int i = 0; i< chara.display().size();i++)
        {
            System.out.println("asd");
            String message = chara.display().get(i).getUser();
            System.out.println(message);
        }
2
  • For more info the bug got triggered by chara.display().size() in which it redirected tot the long post. And yes i have triple checked all of the spelling for the database Commented Dec 11, 2019 at 13:20
  • The problem is 'character' is a reserved word, either double quote it or change it. Commented Dec 11, 2019 at 13:25

1 Answer 1

1

In MariaDB, CHARACTER is a reserved keyword Therefore you cannot use it as such in your query. You need to put it between backthick ( ` )

/* ... */
stat = (Statement) connect.createStatement();

result = stat.executeQuery("SELECT * FROM `character`;");

while(result.next())
{
     Character a = new Character(
                            result.getString("user"),
                            result.getInt("win"),
                            result.getInt("lose"),
                            result.getInt("draw")
                            );
     collection.add(a);
}
/* ... */
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah it worked! Never realized that character is a keyword thanks for your help there!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.