-1

I have no idea what wrong is with my sample code, I've followed all instructions on multiple YouTube videos and other online sources. I just can't fix it. Could someone explain what is the problem and how to solve it? All the required lib's are already added.

I have made sure all the libraries are correctly installed, Yet it still does not find the driver.

This is my code

public boolean checkLogin(String username, String password) {       
    try {
        Connection myconObj;

        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        URL databaseLocation = this.getClass().getResource("/com/vanstryp/database/MainUserData.accdb");

        myconObj = DriverManager.getConnection("jdbc:ucanaccess:/" + databaseLocation);
        ResultSet result;
        Statement stmt = conn.createStatement();
        String query = "select * from MainUserData";
        result = stmt.executeQuery(query);

        while (result.next()) {
            String dbUsername = result.getString("Username");
            String dbPassword = result.getString("Password");
            System.out.println();
            if (username.equalsIgnoreCase(dbUsername) && password.equals(dbPassword)) {

                PrintWriter activeUser = new PrintWriter(new FileWriter("activeUser.db"));
                activeUser.println(dbUsername);
                activeUser.close();
                return true;

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;

}

This is the error message:

java.sql.SQLException: No suitable driver found for jdbc:ucanaccess:/file:/C:/Users/Jaco%20van%20Stryp/Dropbox/Fitness%20Perfect/build/classes/com/vanstryp/database/MainUserData.accdb
Erik A
  • 28,352
  • 10
  • 37
  • 55
  • 2
    *"without giving me an error"* - What is the error? – MadProgrammer Feb 24 '18 at 20:30
  • Please see my edit – Esme van Stryp Feb 24 '18 at 20:33
  • What do you believe value of `databaseLocation` is? It is not what you think. – Andreas Feb 24 '18 at 20:34
  • 4
    *"No suitable driver found"* would suggest that either you've not downloaded the driver Jar or not included it within the class path when you compile or run the program – MadProgrammer Feb 24 '18 at 20:34
  • Possible duplicate of [No suitable driver found for jdbc:ucanaccess://C:\Users\Asim Iqbal\Documents\PersonInfo.accdb](https://stackoverflow.com/questions/31741879/no-suitable-driver-found-for-jdbcucanaccess-c-users-asim-iqbal-documents-per) – Stefan Freitag Feb 24 '18 at 20:35
  • I'd also be wary of `URL databaseLocation = this.getClass().getResource("/com/vanstryp/database/MainUserData.accdb");` - as this will likely make the database read only, The URL which is generated may also not be valid for the driver – MadProgrammer Feb 24 '18 at 20:35
  • https://gyazo.com/414d1fa89737b174ecc5d6be189a3571 These are all the libraries that i have (Youtube suggested) Also how would i fix this problem? (to make it not read only) – Esme van Stryp Feb 24 '18 at 20:38
  • 1
    Possible duplicate [Manipulating an Access database from Java without ODBC](https://stackoverflow.com/questions/21955256/manipulating-an-access-database-from-java-without-odbc) - as it gives detailed instructions on how to include the Jars into a number of different IDEs – MadProgrammer Feb 24 '18 at 20:40
  • @EsmevanStryp *"Also how would i fix this problem? (to make it not read only)"* Place the database on the file system, outside of the project/jar context – MadProgrammer Feb 24 '18 at 20:42
  • I followed the link you gave me, and I already have everything it says, still its not working, I also need this to be a portable project, therefor it needs to be in the jar context. – Esme van Stryp Feb 24 '18 at 20:46
  • myconObj = DriverManager.getConnection("jdbc:ucanaccess:/" + databaseLocation); This is the line that it thinks the problem is at – Esme van Stryp Feb 24 '18 at 20:48

1 Answers1

0

java.sql.SQLException: No suitable driver found for jdbc:ucanaccess:/file:/C:/Users/Jaco%20van%20Stryp/Dropbox/Fitness%20Perfect/build/classes/com/vanstryp/database/MainUserData.accdb

A UCanAccess connection URL must begin with jdbc:ucanaccess:// followed by the path to the database file. Your connection URL begins with jdbc:ucanaccess:/ (only one slash) and it also includes the spurious file: designator. It should look more like this:

jdbc:ucanaccess://C:/path/to/database/file.accdb
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
  • "A UCanAccess connection URL must begin with jdbc:ucanaccess://" When it's like this it can't find the database at all, when not, it can only not find a suitable driver. – Esme van Stryp Feb 25 '18 at 06:50
  • The URL takes me to the exact same location as where the database is, i made sure of that. It needs to be a portable project, that is why it always searches for it inside the .jar – Esme van Stryp Feb 25 '18 at 06:53