Search code examples
javasqlderby

unusual SQL Exception in java, using derby database


Below is the code which i am using for getting the values form the database:

try {

  Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  connect = DriverManager
      .getConnection("jdbc:derby://localhost:1527/TedW;user=x;password=x;");
  PreparedStatement statement = connect
      .prepareStatement("SELECT * FROM USER");

  resultSet = statement.executeQuery();
  while (resultSet.next()) {
    String user = resultSet.getString("Username");
    String password = resultSet.getString("Password");
    System.out.println("User: " + user);
    System.out.println("ID: " + password);
  }
} catch (Exception e) {
  System.out.println(e);
}

the problem is that when i run it it shows the exception:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "USER" at line 1, column 15.

I see that there is some problem with the table name "USER". I saw it when i created the database its the same. It is amde in Derby. Below is the code for that:

CREATE TABLE "USER"
(
"Id" INT not null primary key generated always as identity (START WITH 1, INCREMENT BY 1),
"Username" VARCHAR(20) NOT NULL,
"Password" VARCHAR(20) NOT NULL

);

Where could i probably be wrong? Am i missing something? Can i get some insights into it?

Thanks.


Solution

  • USER is a reserved keyword and you cannot name your table like that. Check here.