Java Source Code
There are four Java classes implemented in this application:
-
TestDatabase.java
-
Two major functions of this class are
- Display the database contents when the activity starts.
- Handle the event of clicking one of the two buttons, “Add New” and “Delete First,” on the application interface.
-
CommentsDataSource.java
-
It is the DAO (Data Access Object) of this application.
It is used to maintain the database connection and supports adding new comments and fetching all comments.
Business applications need access to data from databases and Java Database Connectivity (JDBC) API provides the capability to execute SQL queries against a database and then fetch the results, one column at a time.
The DAO design pattern provides a technique for separating object persistence and data access logic from any particular persistence mechanism.
It provides flexibility to change an application’s persistence mechanism over time without the need to re-engineer application logic that interacts with the DAO tier.
-
MySQLiteHelper.java
-
It is responsible for creating, opening, closing, and using an SQLite database.
A good practice for dealing with databases is to create a helper class to encapsulate all the complexities of accessing the database so that it is transparent to the calling code.
-
Comment.java
-
It is the model containing the data that will be saved in the database and shown in the user interface.