Search code examples
sqlms-accessjdbcucanaccess

What is causing the error 'User lacks privilege or object not found: FieldName'


Using a MS Access Database to hold my information, it has been working fine up and till this point i created a new method named CreateLicense() which takes in the license class i made and writes it to the database.

When the code is run it will spit out a error stating 'user lacks privilege or object not found: EXPIRED'

Already tried changing the fields names of Expired incase it was a reserved word or something. Also tried removing the Expired field but then flags the same error but with CONTACT instead.


INSERT INTO Licenses (Name, DateRedeemed, Expired, LicenseLength, Contact) VALUES ('name','25/Jun/2019','02/Jul/2019','2','contact')
user lacks privilege or object not found: EXPIRED

Here is the output of my SQL statement This is the code

public boolean CreateLicense(License newLicense) {
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            File currentDir = new File(""); // Creates a new file
            String newFilePath = "jdbc:ucanaccess://" + currentDir.getAbsolutePath().toString() + "\\store\\data\\database1.accdb";
            Connection conn = DriverManager.getConnection(newFilePath);
            Statement stmt = conn.createStatement();
            stmt.executeUpdate("INSERT INTO Licenses (Name, DateRedeemed, Expired, LicenseLength, Contact) VALUES "
                    //Values for the SQL Statement
                    + "('" + newLicense.getName()//Start Line
                    + "','" + newLicense.getRedeemed()//Middle Lines
                    + "','" + newLicense.getExpired()//Middle Lines
                    + "','" + newLicense.getLicenseLength()//Middle Lines
                    + "','" + newLicense.getContact()+ "')");//End Line

            conn.close();
            return true;
        } catch (Exception ex) {
            String message = ex.getMessage();
            System.out.println(message);
            return false;
        }
    }

Some additional information to help solve The table name is Licenses Field Names and their type

  • ID - Autonumber
  • Name - String
  • DateRedeemed - String
  • Expired - String
  • LicenseLength- int
  • Contact- String

UPDATE I created a query using the query design in access, this supplied the information to the database fine, so it cant be the sql which is the issue.

UPDATE When i rename the table in the database and run the application i get the same error but with the table name instead, i am going to create a new database and see if that solves anything

Solution Found


Solution

  • It was a pure moment of stupidity, i had a created a backup database which was saved just outside the fat jar directory, as when you create a fat jar it wipes the folder. At the some point i have opened that to test something forgotten to close and that is where i added the license table.

    So when i run the application it actually couldn't find the table due to the file path, I figured this out by testing my other methods, i used my createUser() method which showed which database it was being written to.

    For Other users

    If you get this error, try the following

    • Apply Joakim's solution if you are using one large SQL string like i was as it could be a issue with your SQL statement.(And its better programming practise)

    • Look at your Table name and fields and make sure they are the exact same as your SQL

    • Run your application and test another method where it was previously working, if successful check where the application has written the value

    • If all else fails, delete the method, type it from scratch with no copy and paste, create a new database as i had this issue before when i used relationships between tables within my access database

    I hope this solution helps others solve this issue.