javams-accessjdbcprivilegesucanaccess

UCanAccess: user lacks privilege or object not found


I am trying to make some Ucanaccess practice to manage some database with java. And I am getting an error that I don't know how to solve.

I have this table called USERS in access:

enter image description here

Autonumber, Short Text, Short Text, Number.

So what I am trying to do is to check if a given User and Password are correct, and if they are, then change "CONEXIONES". But I am getting this error:

Exception in thread "main" net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.1 usuario no tiene privilegios suficientes o objeto no encontrado: Z_19MIKE95

at net.ucanaccess.jdbc.UcanaccessStatement.executeUpdate(UcanaccessStatement.java:221)
at Ej4.Ej4.main(Ej4.java:32)

That in English means:

UCAExc:::4.0.1 user lacks of privileges or object not found Z_19MIKE95

So my code is this:

    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    String url="jdbc:ucanaccess://C:/Users/PC-Mikel/Desktop/Uni/Distribuidos/P4Ejer4.accdb";
    Connection connection = DriverManager.getConnection(url);
    Statement statement = connection.createStatement();
    String sql,user,pass;
    System.out.println("Introduzca Usuario");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    user = br.readLine();
    System.out.println("Introduzca ContraseƱa");
    BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
    pass = br2.readLine();
    sql= "UPDATE USERS SET CONEXIONES = 1 WHERE USUARIO = "+user+" AND CONTRASENA = " +pass;
    System.out.println(sql);
    int nrows = statement.executeUpdate(sql);

The code continues but there are just some conditions and queries.


Solution

  • Your dynamic SQL is creating a query that confuses data values with column names. You should use a PreparedStatement and a parameterized query, e.g.,

    sql= "UPDATE USERS SET CONEXIONES = 1 WHERE USUARIO = ? AND CONTRASENA = ?";
    PreparedStatement pstmt = connection.prepareStatement(sql);
    pstmt.setString(1) = user;
    pstmt.setString(2) = pass;
    int nrows = pstmt.executeUpdate();