AvivAvocado
AvivAvocado

Reputation: 21

UCAExc:::3.0.7 user lacks privilege or object not found, when trying to execute specifically update/delete/append queries

I am trying to execute an append, delete and update query in my java code. I use mc access database and I have the queries prepeared in my database and they work just fine when I execute them from access directly. Also, I have a few Select queries that work just fine from the code. The problem occurs only with the update/delete/etc.

The following is my Consts class that establishes the connection to the database and the consts that have the queries saved in them:

package entity;

import java.net.URLDecoder;

///**
 //* http://www.javapractices.com/topic/TopicAction.do?Id=2
 //*/
public final class Consts {
    private Consts() {
        throw new AssertionError();
    }

    protected static final String DB_FILEPATH = getDBPath();
    public static final String CONN_STR = "jdbc:ucanaccess://" + DB_FILEPATH + ";COLUMNORDER=DISPLAY";
    
    
    //----------------------------------------- PARTICIPANT LIST QUERIES -----------------------------------------/
    public static final String SQL_SEL_APPROVED_PARTICIPANT = "{ call qryFindApprovedFriends(?) }";
    public static final String SQL_SEL_WAITING_PARTICIPANT = "{ call qryFindWaitingFriends(?) }";
    public static final String SQL_UPD_TO_APPROVED_PARTICIPANT = "{ call qryUpdParticipantToApproved(?,?) }";// think how to find the friend and add to friendList maybe add find qry
    public static final String SQL_UPD_TO_DENIED_PARTICIPANT = "{ call qryUpdParticipantToDenied(?,?) }";
    public static final String SQL_DEL_FRIEND = "{ call qryDelFriend(?,?) }";
    public static final String SQL_INS_NEW_FRIEND_REQUEST = "{ call qryInsNewFriend(?,?) }";
    

    /**
     * find the correct path of the DB file
     * @return the path of the DB file (from eclipse or with runnable file)
     */
    private static String getDBPath() {
        try {
            String path = Consts.class.getProtectionDomain().getCodeSource().getLocation().getPath();
            String decoded = URLDecoder.decode(path, "UTF-8");
            // System.out.println(decoded) - Can help to check the returned path
            if (decoded.contains(".jar")) {
                decoded = decoded.substring(0, decoded.lastIndexOf('/'));
                return decoded + "/database/HW1_Database_211923158_207975632.accdb";
            } else {
                decoded = decoded.substring(0, decoded.lastIndexOf("bin/"));
                System.out.println(decoded);
                return decoded + "//HW1_Database_211923158_207975632.accdb";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Next, I have ParticipantLogic class that actually handels all the callings to the queries. The problem occurs in removeFriend, addFriend methods inside the try block, when the code tries to connect to the related queries in my database and throws this exception: UCAExc:::3.0.7 user lacks privilege or object not found: QRYINSNEWFRIEND.

package control;
import entity.Participant;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import entity.Consts;

public class ParticipantLogic {
    private static ParticipantLogic _instance;

    private ParticipantLogic() {
    }

    public static ParticipantLogic getInstance() {
        if (_instance == null)
            _instance = new ParticipantLogic();
        return _instance;
    }
    // select all approved participant
    
    public ArrayList<Participant> getApprovedParticipant(long firstPhone) {
        ArrayList<Participant> results = new ArrayList<Participant>();
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            try (Connection conn = DriverManager.getConnection(Consts.CONN_STR);
                    PreparedStatement stmt = conn.prepareStatement(Consts.SQL_SEL_APPROVED_PARTICIPANT))
                            {
                    stmt.setLong(1, firstPhone);
                    
                    
                    ResultSet rs = stmt.executeQuery();
                while (rs.next()) {
                    int i = 1;
                    results.add(new Participant(rs.getLong(i++), rs.getString(i++), rs.getString(i++)));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return results;
    }
    
    // select all waiting participant
    
    public ArrayList<Participant> getWaitingParticipant(Long phone) {
            ArrayList<Participant> results = new ArrayList<Participant>();
            try {
                Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                try (Connection conn = DriverManager.getConnection(Consts.CONN_STR);
                        PreparedStatement stmt = conn.prepareStatement(Consts.SQL_SEL_WAITING_PARTICIPANT))
                                {                       
                        stmt.setLong(1, phone);
                        ResultSet rs = stmt.executeQuery(); 
                    while (rs.next()) {
                        int i = 1;
                        results.add(new Participant(rs.getLong(i++), rs.getString(i++), rs.getString(i++)));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return results;
        }
        //delete friend 
        
        public boolean removeFriend(long firstPhone,long secondphone) {
                    try {
                        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                        try (Connection conn = DriverManager.getConnection(Consts.CONN_STR);
                                CallableStatement stmt = conn.prepareCall(Consts.SQL_DEL_FRIEND)) {
                            
                            stmt.setLong(1, firstPhone);
                            stmt.setLong(2, secondphone);
                        
                            stmt.executeUpdate();
                            return true;
                            
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    return false;
                }
        // new friend request
        public boolean addFriend(long firstPhone,long secondphone) {
                    try {
                        System.out.println("test1");
                        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                        try (Connection conn = DriverManager.getConnection(Consts.CONN_STR);
                                CallableStatement stmt = conn.prepareCall(Consts.SQL_INS_NEW_FRIEND_REQUEST)) {
                            System.out.println("test2");
                            int i = 1;
                            stmt.setLong(i++, firstPhone); // can't be null
                            stmt.setLong(i++, secondphone); // can't be null
                            stmt.executeUpdate();
                            System.out.println("test3");
                            return true;
                            
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    return false;
                }
                
                
                
        //accept friend 
        public boolean acceptFriend(long firstPhone,long secondphone ) {
                    try {
                        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                        try (Connection conn = DriverManager.getConnection(Consts.CONN_STR);
                                CallableStatement stmt = conn.prepareCall(Consts.SQL_UPD_TO_APPROVED_PARTICIPANT)) {
                            
                            int i = 1;
                            stmt.setLong(i++, firstPhone); // can't be null
                            stmt.setLong(i++, secondphone); // can't be null
                            stmt.setString(i++, "APPROVED");
                            stmt.executeUpdate();
                            return true;
                            
                            
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    return false;
                }
        //decline friend 
        public boolean declineFriend(long firstPhone,long secondphone ) {
                    try {
                        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                        try (Connection conn = DriverManager.getConnection(Consts.CONN_STR);
                                CallableStatement stmt = conn.prepareCall(Consts. SQL_UPD_TO_DENIED_PARTICIPANT)) {
                            
                            int i = 1;
                            stmt.setLong(i++, firstPhone); // can't be null
                            stmt.setLong(i++, secondphone); // can't be null
                            stmt.setString(i++, "DENIED");
                            stmt.executeUpdate();
                            return true;
                            
                            
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    return false;
                }   

}

Also, I don't know if this helps but here are the SQL for the queries: qryInsNewFriend:

INSERT INTO TblFriends ( ParticipantPhone, FriendPhone, Status )
SELECT [1] AS Expr1, [2] AS Expr2, "WAITING" AS Expr3;

qryDelFriend:

DELETE TblFriends.ParticipantPhone, TblFriends.FriendPhone
FROM TblFriends
WHERE (((TblFriends.ParticipantPhone)=[1]) AND ((TblFriends.FriendPhone)=[2]));

I tried to check my security options and everything is fine. I tried to repair my database, also I created a new database with the same entries and connected that database to the code to see if the problem occurs again and it did. I tested this on a different computer and this happend again. I tried to change Callable to PrepearedStatement and it didn't work. Moreover, this code snnipet inside ParticipantLogic was given to us by our instructors in uni and other students used this same code with minor changes to variable names and everything worked fine for them. I can't find a reason for this code to not work. Also, I didn't test the other two update queries but I assume they have the same problem as it seems it has something to do with the query type.

Thanks from advance.

Upvotes: 1

Views: 39

Answers (0)

Related Questions