patternjavaMinor
Program to provide CRUD operations for a Boxers(Fighter) table in a database
Viewed 0 times
operationsprovidetableprogramdatabaseforboxersfightercrud
Problem
I have been through a few SQLite tutorials and wrote this code on my own to reinforce the principles. The tutorials I went through varied widely in a few areas so this is what I came up with as a combination of everything.
Other than the two classes I have listed below, all I have is one activity that adds, remove, inserts, update and display data from the database.
I have a few specific questions:
Helper Class
```
public class BoxScoresHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "boxing_scores.db";
private static final int VERSION = 1;
private static BoxScoresHelper instance = null;
public static BoxScoresHelper getInstance(Context context){
if(instance == null){
instance = new BoxScoresHelper(context);
}
return instance;
}
private BoxScoresHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createBoxerSQLString());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop Table If Exists " + BoxerDAO.TABLE_NAME);
onCreate(db);
}
private String createBoxerSQLString(){
String boxerCreateString = "create table " + BoxerDAO.TABLE_NAME +
"(" + BoxerDAO._ID + " Integer Primary Key AutoIncrement, " +
BoxerDAO.BOXER_NAME + " Text Not Null, " +
BoxerDAO.WEIGHT_CLASS + " Text Not Null, " +
BoxerDAO.WINS + " Integer Not Null, " +
BoxerDAO.LOSSES + " Integer Not Null);";
return boxerCreat
Other than the two classes I have listed below, all I have is one activity that adds, remove, inserts, update and display data from the database.
I have a few specific questions:
- When should I close the helper class or does garbage collection deal with it automatically?
- Should I create a Boxer POJO (Plain Old Java Object) to pass boxer data to and from the DAO?
- Is the DAO implementation efficient?
- Does the code deviate from Java and Android best practices in any way?
Helper Class
```
public class BoxScoresHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "boxing_scores.db";
private static final int VERSION = 1;
private static BoxScoresHelper instance = null;
public static BoxScoresHelper getInstance(Context context){
if(instance == null){
instance = new BoxScoresHelper(context);
}
return instance;
}
private BoxScoresHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createBoxerSQLString());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop Table If Exists " + BoxerDAO.TABLE_NAME);
onCreate(db);
}
private String createBoxerSQLString(){
String boxerCreateString = "create table " + BoxerDAO.TABLE_NAME +
"(" + BoxerDAO._ID + " Integer Primary Key AutoIncrement, " +
BoxerDAO.BOXER_NAME + " Text Not Null, " +
BoxerDAO.WEIGHT_CLASS + " Text Not Null, " +
BoxerDAO.WINS + " Integer Not Null, " +
BoxerDAO.LOSSES + " Integer Not Null);";
return boxerCreat
Solution
Convert member field to local variable when you can
In
Each utility method gets an
The class needs only an
Refer to types by interfaces
In
It doesn't use any features of
Class design
It would be better if it wasn't aware of
You could pass to the constructor an
Naming
Prefixing member fields with "my" is not a common convention.
You could simply drop that.
Coding style
There are too many blank lines in the code.
This is unnecessary, it would be good to trim the code a little.
In
BoxerDAO, the myBoxerDB member can be converted to a local variable.Each utility method gets an
SQLiteDatabase instance from the helper.The class needs only an
SQLiteOpenHelper as a member field.Refer to types by interfaces
In
BoxerDAO,myScoresHelper can be declared as an instance of SQLiteOpenHelper.It doesn't use any features of
BoxScoresHelper.Class design
BoxerDAO is only aware of Context to get an instance of BoxScoresHelper.It would be better if it wasn't aware of
Context at all.You could pass to the constructor an
SQLiteOpenHelper instead.Naming
Prefixing member fields with "my" is not a common convention.
You could simply drop that.
Coding style
There are too many blank lines in the code.
This is unnecessary, it would be good to trim the code a little.
Context
StackExchange Code Review Q#95820, answer score: 2
Revisions (0)
No revisions yet.