Search code examples
sqljdbcactivejdbcjavaliteactiveweb

Table not found exception using javalite


So I have a very simple table I made in SQL using h2

CREATE TABLE USERS(
    username varchar(255) NOT NULL,
    password varchar(255),

); 

I'm trying to use javalite to add an entry to it so I made this following the instructions on the site.

package DBTEST;

import org.javalite.activejdbc.Base;

public class makeDB {
    public static void main(String[] args) {
        Base.open("org.h2.Driver", "jdbc:h2:./test", "sa", "");

        User e = new User();
        e.set("username", "John");
        e.set("password", "Doe");
        e.saveIt();


        User.findAll().dump();
        Base.close();
    }
}

I have a class Users for this table

package DBTEST;

import org.javalite.activejdbc.Model;
import org.javalite.activejdbc.annotations.Table;

@Table("USERS")
public class User extends Model {
}

I keep getting this exception

Exception in thread "main" org.javalite.activejdbc.DBException: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "USERS" not found; SQL statement:

Can anyone help? I have no idea why this is happening


Solution

    • First, your SQL has an extra comma in "CREATE USERS" statement. The errors says: "able "USERS" not found" - this mean you simply do not have a table!

    • Second, the table definition is missing an id, please see https://javalite.io/surrogate_primary_keys

    • Third, I created a simple example project and added your code there. It is working as expected. The project can be found here: https://github.com/javalite/h2-example

    The output from running this program looks like this:

    Model: activejdbc.examples.simple.User, table: 'users', attributes: {ID=1, PASSWORD=Doe, USERNAME=John}
    

    which is exactly as expected.

    Additionally, the @Table annotation is not necessary: https://javalite.io/english_inflections