2017-04-05 95 views
1

我想使一些Ucanaccess實踐來管理一些數據庫與Java。而且我收到了一個我不知道如何解決的錯誤。UCanAccess:用戶缺少特權或對象未找到

我有這樣的表稱爲用戶訪問:

enter image description here

自動編號,短文本,短文本,數字。

所以我想要做的是檢查給定的用戶和密碼是否正確,如果他們是,然後更改「CONEXIONES」。但我收到此錯誤:

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)

,在英文的意思是:

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

所以我的代碼是這樣的:

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); 

代碼繼續,但目前只有一些條件和查詢。

回答

1

您的動態SQL正在創建一個將數據值與列名混淆的查詢。您應該使用PreparedStatement參數化查詢,例如,

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(); 
相關問題