[Solved] java.sql.SQLSyntaxErrorException: unexpected token: XYZ
java.sql.SQLSyntaxErrorException with error message Unexpected token exception occurred when progress query has violated SQL Syntax or in technical terms SQLState class value is ’42’, or under vendor-specified conditions.
java.sql.SQLSyntaxErrorException is sub class of SQLNonTransientException which is also sub class of SQLException.
Constructors
- SQLSyntaxErrorException() : Constructs a SQLSyntaxErrorException object.
- SQLSyntaxErrorException(String reason) : Constructs a SQLSyntaxErrorException object with a given reason.
- SQLSyntaxErrorException(String reason, String SQLState) :Constructs a SQLSyntaxErrorException object with a given reason and SQLState.
- SQLSyntaxErrorException(String reason, String SQLState, int vendorCode) : Constructs a SQLSyntaxErrorException object with a given reason, SQLState and vendorCode.
- SQLSyntaxErrorException(String reason, String SQLState, int vendorCode, Throwable cause) : Constructs a SQLSyntaxErrorException object with a given reason, SQLState, vendorCode and cause.
- SQLSyntaxErrorException(String reason, String SQLState, Throwable cause) : Constructs a SQLSyntaxErrorException object with a given reason, SQLState and cause.
- SQLSyntaxErrorException(String reason, Throwable cause) : Constructs a SQLSyntaxErrorException object with a given reason and cause.
- SQLSyntaxErrorException(Throwable cause): Constructs a SQLSyntaxErrorException object with a given cause.
java.sql.SQLSyntaxErrorException Example
In this below example try to execute below query while JDBC or upload default schema by Spring boot then it will throw exception “java.sql.SQLSyntaxErrorException: unexpected token: EMPLOYEE” because this query is using EMPLOYEE as required keyword TABLE in first line. That is violation of SQL syntax rule.
DROP EMPLOYEE IF EXISTS; CREATE TABLE EMPLOYEE ( ID int NOT NULL PRIMARY KEY, FIRST_NAME varchar(255), LAST_NAME varchar(255), ADDRESS varchar(255), );
java.sql.SQLSyntaxErrorException Stacktrace
Caused by: java.sql.SQLSyntaxErrorException: unexpected token: EMPLOYEE at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:472) ~[spring-jdbc-4.3.23.RELEASE.jar:4.3.23.RELEASE] . 73 common frames omitted Caused by: org.hsqldb.HsqlException: unexpected token: EMPLOYEE at org.hsqldb.error.Error.parseError(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.ParserBase.unexpectedToken(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.ParserDDL.compileDrop(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.ParserCommand.compilePart(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.ParserCommand.compileStatements(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.Session.executeDirectStatement(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] at org.hsqldb.Session.execute(Unknown Source) ~[hsqldb-2.3.6.jar:2.3.6] . 76 common frames omitted
Solutions
Below is correct query to resolve this issue .
DROP TABLE IF EXISTS EMPLOYEE;
CREATE TABLE EMPLOYEE
(
ID int NOT NULL PRIMARY KEY,
FIRST_NAME varchar(255),
LAST_NAME varchar(255),
ADDRESS varchar(255),
);
Unexpected token
posted 6 years ago
Hey guys, what i’m trying to do is compare 2 strings (username and password which are set in the variables) and then take the input typed into a jtextfield and jpasswordfield and then compare them. I’ve got most of it working except when I try to make the code go
JTextField ——-> Checks String 1 and JPasswordField ——-> Checks String 2
I end up with a error which says token expected.
My understanding is that the «||» checks that both conditions are true and if it is then the frame gets disposed and a new frame pops up. Can anyone guide me on this one?
Bartender
posted 6 years ago
posted 6 years ago
The OR operator (||) returns TRUE if either of the boolean expressions it is used with are true and it returns false if both of the boolean expressions are false.
The AND operator (&&) returns TRUE if both of the boolean expressions are true and false if either or both are false.
Do you want both tests to be true (use AND) or does only one (use OR) need to be true?
Marshal
posted 6 years ago
«Token expected» is a compiler error which says (very obscurely, I agree) that something is missing in your code. It looks like Ganesh’s post is right, your brackets aren’t matching up correctly.
posted 6 years ago
Thanks for that guys, it was a long day and my head was all over the place so I managed to miss it! here is the revised code
The only issue now is that when I enter a string into the username and password fields it doesn’t do as I wish
Bartender
posted 6 years ago
posted 6 years ago
Hey I just had a look at what you said and i’ll be completely honest with you, it doesn’t make too much sense to me. Could you break it down abit more without actually answering my question by posting code because I want to figure this out by myself. I’ll post what I have but I feel like what is getting me is getPassword returns a char[] but what to do next. I’ll post my code and if you guys can help me out that would be greatly appreciated.
Bartender
posted 6 years ago
Marshal
posted 6 years ago
Paul Clapham wrote: «Token expected» is a compiler error which says (very obscurely, I agree) that something is missing in your code. It looks like Ganesh’s post is right, your brackets aren’t matching up correctly.
Which compiler are you using? Eclipse?
You should use an editor which supports bracket matching; hover your mouse over the first ( and its paired ) should change colour. I think that works on IDEs too.
Saloon Keeper
posted 6 years ago
There’s a reason that passwords are kept in char arrays. Sensitive data shouldn’t be stored as strings. For now, what Ganesh suggested will work, but applications that are more than just practice projects should never convert passwords to strings.
Marshal
posted 6 years ago
You should find out about the security problems with passwords, but databases maintain passwords as a hash of some sort. You should use this sort of method to compare equality of the char[]s.
posted 6 years ago
Would I use toString method? that’s the only thing I can think of right now and when you say
If you know String class has a constructor which takes char[] as parameter using that you can have a String value of all those characters present in char[].
In your example here
Have I given it the right constructor?
posted 6 years ago
Which compiler are you using? Eclipse?
Marshal
posted 6 years ago
Doesn’t IntelliJ use ordinary javac behind the scenes? I would have thought you probably get bracket highlighting on IntilliJ.
I have given you a suggestion about how to check equality of two char[]s. You can use new String(myCharArray) or the toCharArray method of the String class. As I said, I think much production code uses an SHA512 or something similar to store a hash of your password, but I am not certain. You can find that out in a security book or maybe Wikipedia.
posted 6 years ago
I would have thought you probably get bracket highlighting on IntilliJ
It does but because of the theme that i’m using it makes it difficult
You can use new String(myCharArray) or the toCharArray method of the String class
It’s getting late where I am but when I get a chance tomorrow i’ll check it out and see if I can put it together
As I said, I think much production code uses an SHA512 or something similar to store a hash of your password
I’ll read about it when I can because i’m always curious about java and programming
The support on this forum is amazing and just wanted to extended a big thanks to everyone who participates in keeping this forum going
Bartender
posted 6 years ago
mitchell bat wrote: Have I given it the right constructor?
Yes you used correct constructor but I would go with advices of Stephan van Hulst and Campbell Ritchie and use char[]. You can use for practice project as Stephan van Hulst mentioned. If you are interested in knowing why prefer char[] over String for password I’ll post the link of thread here which I’m going to create for that issue.
posted 6 years ago
I’ve had a go and can’t work it out. I’ll explain my logic and what i’ve done
Here what i’ve done is created a string for the username to be used, i’ve created an char array and a string that holds the password
and now here what I think iv’e done is get the input from the password field and used the get password method to change it into a char and then used the toString method to convert the char to a string and compared it back to the string that I have by using the euqals method
But still not working, please tell me that i’m close guys!
Marshal
posted 6 years ago
Have you validated your assumption about what
Bartender
posted 6 years ago
to know what is happening in your code, whenever necessary you better print the value of variables or whatever you get to keep track of it. If I don’t know what returns then As Paul already mentioned, I need to know what it returns but how? print it’s returned value.
posted 6 years ago
Have you validated your assumption about what
?
1
passwordField.getPassword().toString()
I’ve tried to google java validation but not much comes up, can you post a tutorial so I can have a read through it and hopefully come up with a solution.
But just out of curiosity why am I not getting this to work? Is it not working because of the validation or what?
Marshal
posted 6 years ago
You seem to have assumed that you know what the result of
would be. I’m suggesting that your assumption is wrong. To test the assumption all you need is to display the result and see if it matches your expectation. To do that:
You don’t need a tutorial to find out about System.out.println, the simplest form of debugging.
Bartender
posted 6 years ago
he meant to say that did you check what you get by doing thisDo one thing print the value like this and see what it prints
Does that print contents of char[] array as String? or something else ?
posted 6 years ago
So I ran this code and the output of the validation was
and then I did it again and I got this
Which means i’m doing something wrong, which by the looks of things it’s not being converted into a string. Am I right in saying that?
Marshal
posted 6 years ago
You aren’t quite right. The toString() method always produces a String, it can’t do anything else. However you have assumed that if you have an array of chars, then its toString() method will produce a String which concatenates all the chars in the array. But as you can see now, that isn’t the case. So yes, it produces a String. It just doesn’t produce the String you need.
If you want to convert an array of chars into a String according to the blindingly obvious rule, then calling the array’s toString() method isn’t the way to do it. There’s a String constructor which does what you want, so check the API documentation for String to find it.