5

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);
        }
    }
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
  • 2
    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](https://netbeans.org/kb/articles/javase-deploy.html) for details. – Gord Thompson Oct 01 '16 at 15:52
  • THANK YOU! In addition to your suggestions, I have also added all the Jar files in NetBeans, under Libraries. My program is now fully functional, so thanks a lot!! – Paul Formosa Oct 03 '16 at 12:28

2 Answers2

0

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.

Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
0

You could go for the fat-jar approach. You can see it here if you use ant.

If have a maven project you could have this plugin to create a fat-jar

The benefit of a fat-jar application is that you could have everything inside one jar and don't need to worry with the lib dir like it was mentioned in another answer.

Also be careful with hard-coded paths like:

String strurl="jdbc:ucanaccess://C:\\Users\\Paul\\Desktop\\Spelli\\RispostiDB.mdb";

that you have in your code.

Community
  • 1
  • 1
liponcio
  • 254
  • 1
  • 9