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();
}
}
}
GetDirectoryContent
method and let that exception bubble up into yourMain
class. Take this as a general guideline—catch as late as possible. But, even before you start entering such details, you must first study the Java Naming Conventions and adhere to them religiously. \$\endgroup\$