Adding Database Support

Once you import the necessary Database classes, this step is relatively straight forward - all you have to do is replace the method calls that manages the donationList with the respective dbManager calls.

First thing to do is download the necessary database classes in the database archive and add them to a new app.db package in your project.

Take a few moments to investigate the classes and familarise yourself with the methods you’ll be using.

There are a few classes you’ll need to modify to add database support to your project, but initially, you need to create an instance of DBManager in Base.java and both open/close the database when necessary, so refer to the Lecture Material to complete this.

Also, our Donation class needs a slight refactor, so replace the current class with this one.

  1. public class Donation
  2. {
  3. public int id;
  4. public int amount;
  5. public String method;
  6. public Donation (int amount, String method)
  7. {
  8. this.amount = amount;
  9. this.method = method;
  10. }
  11. public Donation ()
  12. {
  13. this.amount = 0;
  14. this.method = "";
  15. }
  16. public String toString()
  17. {
  18. return id + ", " + amount + ", " + method;
  19. }
  20. }

Once you make these changes, commenting out the donationList List, (and save the file) you’ll get a number of errors, which actually indicates which classes you need to now update and add the database calls (and remove the donationList calls).

Each error requires only one line of code to be fixed, so have a go and updating each of the classes (and we’ll have a look at the solution near the end of the Practical Lab).

Once you fix all the errors, and run the app again, you should still be able to make donations - but this time those donations are stored in a database.

And as a final check, if you shut down the app and restart it again, you should still be able to see the donations Report.