HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Storing .h files in SQLite using SQLJet

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sqlitefilesusingsqljetstoring

Problem

I’m trying to learn as much as I can on my own by reading lots of examples, documentations, and asking here. I would like to improve my style to write efficient code and adhere to Java standards.

In this small sample of code I would like to get feedback on a few things:

  • Exception throwing



  • Opening/closing database connection



  • Any other comments in general style



There are two classes, Database and Main.

My Database class:

public class Database {

    private String dbName = "";
    private SqlJetDb db = null;

    public Database(String dbName) {
        this.dbName = dbName;
    }

    public void CreateDatabase() throws SqlJetException {...}

    public void OpenDatabaseConnection() throws SqlJetException {...}

    public void CloseDatabaseConnection() throws SqlJetException {...}

    private void InsertRecord(String file) throws SqlJetException {...}

    public void GetDirectoryContent(String dir) {
        File directory = new File(dir);
        if (directory.isDirectory()) {
            String[] content = directory.list();
            for (String s : content) {
                GetDirectoryContent(dir + "\\" + s);
            }
        } else {
            String file = directory.toString();
            String extension = file.substring(file.lastIndexOf("."));
            if (extension.equals(".h")) {
                try {
                    InsertRecord(file);
                } catch (SqlJetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


Call in main:

Database db = new Database("test.db");
    try {
        db.CreateDatabase();
        db.OpenDatabaseConnection();
        db.GetDirectoryContent("C:\\test");         
    } catch (SqlJetException e) {
        e.printStackTrace();
    } finally {
        try {
            db.CloseDatabaseConnection();
        } catch (SqlJetException e) {
            e.printStackTrace();
        }           
    }
}

Solution

Consider in the following:

public Database(String dbName) {
    this.dbName = dbName;
}


  • I would check for a null dbName being passed in. Note that you pass this reference and then simply store it. Consequently, if it's null, you won't find out until later (perhaps, much later). You then have to work out at what point that was set to null.



  • Will you change dbName ? If not, make it final. It'll stop you changing it inadvertently later on. Immutability is often a good idea. It makes the class more robust and thread-safety easier to achieve. Perhaps not a requirement here, but who knows ? It's easier to relax a restriction rather than apply it after-the-fact.

Code Snippets

public Database(String dbName) {
    this.dbName = dbName;
}

Context

StackExchange Code Review Q#14203, answer score: 6

Revisions (0)

No revisions yet.