Search code examples
javams-accessnetbeansjarucanaccess

"No suitable driver found" when running from JAR


I have developed a small game in which the text input by the user needs to be posted in a MS Access Database.

It all works fine in NetBeans but whenever I access it from the JAR file (which is ultimately what I need to hand to my client), it does not post anything to the database. In fact it returns the following error:

java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:\Users\Paul\Desktop\Spelli\RispostiDB.mdb

This is the relevant code:

public void postAnsDB()
    {
       String tableName = "Form"+studentGroup +"_"+studentSurname+"_"+studentName+"";

       answerModifier();

       try{

                String strurl="jdbc:ucanaccess://C:\\Users\\Paul\\Desktop\\Spelli\\RispostiDB.mdb";
                Connection conn=DriverManager.getConnection(strurl, "", "");
                Statement stmt=conn.createStatement();

                //Post Student Details to DB 

                String post = "INSERT INTO "+tableName+"(ID, responses, Correct_Response, Valid_Invalid, Marks) VALUES ('"+ansID+"', '"+answer+"', '"+correct+"', '"+valid+"', '"+marks+"');";

                stmt.executeUpdate(post);
            }
        catch(Exception e)
        {
            System.out.println("Exception found in postAnsDB: "+e);
        }
    }

Solution

  • When you build your project in NetBeans it creates a dist folder in the home folder of your project, puts the JAR file for your code in that folder, and also puts the dependencies (JAR files for required components like UCanAccess, Jackcess, HSQLDB, and Apache Commons bits) into a subfolder named lib. You need to ensure that the lib folder gets copied to the target machine and placed in the same folder as your main JAR file. Look here for details.