I have compiled my JavaFX application and when I try to take the data from the UI and update the database I get this error:
java.sql.SQLSyntaxErrorException: Lexical Error at line 1 , column 105. Encountered "@" (64) after : ""
at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.client.am.ClientStatement.execute(Unknown Source)
at Query.generateOperation(Query.java:45)
at AdminAccount.addAdmin(AdminAccount.java:27)
at Root.lambdaExpression(Root.java:89)
This is the code which is referenced for Query ->
public void generateOperation(String query) {
try {
Statement statement = connection.createStatement();
statement.execute(query) // line raised by compiler
} catch (SQLException e) {
e.printStackTrace();
}
}
This is the code which is referenced for AdminAccount ->
public void addAdmin(String username, String password, String email) {
int id = generateId(); // return random number for id
String values = id + ", " + username + ", " + password + ", " + email;
DB.generateOperation("insert into APP.ADMINDETAILS (ID , USERNAME , PASSWORD , EMAIL) values (" + values + ")"); // DB is the Query object I use to interact with the database . This line is raised by the compiler
}
This is the code which is called by the user interface
submit.setOnAction(e -> {
if (...) {
adminAccount.addAdmin(usernameEntry.getText(), passwordEntry.getText(), emailEntry.getText()) ; // this is the line raised by compiler
}
});
What should I amend to this code so that I don't get the lexical error when parsing the SQL statement. I think there is trouble parsing the email entry as the compiler references the "@" symbol. Should I be using another method for constructing SQL statements?
PreparedStatementApart from being much more secure it also magically solves your problemPreparedStatementsavoids the need to do that. As well as avoiding the gnarly SQL injection problems that arise with SQL query string bashing.)