I just wrote my first javaJava database program for the purpose of getting feedback on the implementation and coding. It has 1 table and 2 buttons and prompts the user to select a folder, lists the contents of the folder in the table and lists the hash of the files in the table and writes it to a database. It works fine, but I have no idea if I coded it cleanly and split the program into the proper packages and classes. Its definitely a beginner level project so there is nothing too complex about it. I'm also using the h2 database because I was told its the most efficient for small databases. Could you all please give me feedback on this?
It works fine, but I have no idea if I coded it cleanly and split the program into the proper packages and classes. It's definitely a beginner level project so there is nothing too complex about it. I'm also using the h2 database because I was told its the most efficient for small databases. Could you all please give me feedback on this?
I used NetBeans 7.3 and uploaded the full project here: http://www.mediafire.com/?huujeed8oiwonr8here This. This is the class I'm using for the database. It contains most of the code I have in question, but there are other parts of the project as a whole I'm concerned if they were organized correctly:
public class CDatabaseLayer {
private static ArrayList<CFileObject> fileList = new ArrayList();
private static Server server;
private static JdbcDataSource ds = new JdbcDataSource();
private static Connection conn;
private static int lastId = 0;
private static Statement stat;
private static ResultSet rs;
private static String query;
static public ArrayList<CFileObject> getFileList() {
connectDatabase();
return fileList;
}
static public boolean connectDatabase()
{
System.out.println("Attempting to connect to database.");
if(server == null) {
try {
server = Server.createTcpServer();
server = server.start();
} catch (SQLException ex) {
System.out.println("connectDatabase createTcpServer() exception: "+ex);
return false;
}
}
//return false if connected
if(conn != null) {
System.out.println("Already established DB connection.");
return false;
} else {
try {
//connect to database
ds.setURL("jdbc:h2:test");
ds.setUser("sa");
ds.setPassword("");
static public ArrayList<CFileObject> getFileList() {
connectDatabase();
return fileList;
} conn = ds.getConnection();
if(conn.isClosed()) {
System.out.println("Connection not established.");
} else {
System.out.println("Connected.");
createFileListTable();
loadFileListTable();
}
} catch (SQLException ex) {
System.out.println("connectDatabase getConnection() exception: "+ex);
}
}
return true;
}
static public boolean connectDatabase()
{
static private void System.out.printlnloadFileListTable("Attempting to connect to database.");
if(server == null) {
try {
serverstat = ServernewStatement();
rs = stat.createTcpServerexecuteQuery("select * from fileList");
fileList.clear();
server
while(rs.next()) {
CFileObject objF = servernew CFileObject();
objF.startfileName = rs.getString("fileName");
objF.filePath = rs.getString("filePath");
objF.fileHash = rs.getString("fileHash");
fileList.add(objF);
}
} catch (SQLException ex) {
System.out.println("connectDatabase createTcpServer()"loadFileListTable exception: "+ex);
return false;
}
}
//return false if connected
if(conn != null) {
static public void System.out.printlnmanipulateFiles("Already established DB connection.");
throws SQLException { return false;
} else {
try {
//connect to database
ds.setURL("jdbc:h2:test");
ds.setUser("sa");
ds.setPassword("");
conn = ds.getConnection();
if(conn.isClosed()) {
System.out.println("Connection not established.");
} else {
"manipulateFiles: Connection is System.out.println("Connectedclosed.");
createFileListTable();
loadFileListTable();
}
} catch (SQLException ex) {
System.out.println("connectDatabase"manipulateFiles getConnection()connection exception: "+ex);
}
for(CFileObject f : fileList) {
String hash = new String();
try {
hash = hashFile.getMD5Checksum(f.filePath);
} catch (Exception ex) {
System.out.println("manipulateFiles hash exception: "+ex);
}
f.fileHash = hash;
query = "update filelist set filehash = '"+hash+"' where filepath = '"+f.filePath+"'";
stat = newStatement();
//QUESTION: Why does this return false??
stat.execute(query);
}
}
return
true; static public void updateDatabaseWithFilesFromPath(File path)
} {
ArrayList<CFileObject> list;
list = CFileObject.getListFromPath(path);
static private void loadFileListTable()
{
try {
stat = newStatement();
rs = stat.executeQuery("select * from fileList");
fileList.clear();
whilefor(rs.nextCFileObject file : list) {
if(!entryExists("filelist", "filepath", file.filePath)) {
CFileObject objF = new CFileObjecttry {
lastId = nextUnusedId();
objF addFileEntry(lastId, file.fileName =, rsfile.getString("fileName"filePath);
objF.filePath = rs fileList.getStringadd("filePath"file);
objF.fileHash = rs.getString } catch ("fileHash"SQLException ex); {
fileList System.addout.println(objF"updateDatabaseWithFilesFromPath exception: "+ex);
}
}
}
} catch (SQLException ex) {
System.out.println("loadFileListTable exception: "+ex);
}
}
static public void manipulateFiles() throws SQLException {
try {
if(conn.isClosed()) {
System.out.println("manipulateFiles: Connection is closed.");
}
} catch (SQLException ex) {
System.out.println("manipulateFiles connection exception: "+ex);
}
for(CFileObject f : fileList) {/*
String hashstatic =public newvoid StringupdateDatabase();
try {
hash =throws hashFile.getMD5Checksum(f.filePath);SQLException
} catch (Exception ex) {
lastId = System.out.printlnnextUnusedId("manipulateFiles hash exception: "+ex);
}
f.fileHash = hash;
query = "update filelist set filehash = '"+hash+"' where filepath = '"+f.filePath+"'";
stat =for newStatement();
final CFileObject file //QUESTION: Why does this return false??
stat.execute(queryfileList);
}
}
static public void updateDatabaseWithFilesFromPath(File path)
{
ArrayList<CFileObject> list;
list = CFileObject.getListFromPath(path);
for(CFileObject file : list) {
if(!entryExists("filelist", "filepath", file.filePath)) {
try {
lastId = nextUnusedId();
addFileEntry(lastId, file.fileName, file.filePath);
fileList.add(file);
} catch (SQLException ex) {
System.out.println("updateDatabaseWithFilesFromPath exception: "+ex);
}
}
}
}
/*
static public void updateDatabase() throws SQLException
{
lastId = nextUnusedId();*/
for
(final CFileObject file :private fileList)static {Statement newStatement()
{
if(!entryExists("filelist", "filepath", file.filePath)) try {
lastIdstat = nextUnusedIdconn.createStatement();
addFileEntryreturn stat;
} catch (lastId,SQLException file.fileName,ex) file{
System.filePathout.println("newStatement exception: "+ex);
}
return null;
}
}
* /
/QUESTION: Is there a more efficient way to do this?
private static Statementvoid newStatementcreateFileListTable()
{
try {
//see if fileList table exists. if not, catch error and create it
try {
stat = conn.createStatementnewStatement();
return stat; stat.executeQuery("select * from fileList");
} catch(SQLException e) {
if(e.toString().contains("Table \"FILELIST\" not found")) {
System.out.println("Creating filelist table.\n");
stat.execute("create table filelist(id int primary key, fileName varchar(255), filePath varchar(512), fileHash varchar(32))");
} else {
System.out.println(e);
}
}
} catch(SQLException ex) {
System.out.println("newStatement"createFileListTable exception: "+ex);
}
}
return null;
}
//QUESTION: Is there a more efficient way to do this?
private static voidint createFileListTablenextUnusedId()
{
try {
//see if fileList table exists. if not, catch error and create it
try {
stat = newStatement();
rs = stat.executeQuery("select *id from fileList");
} catch(SQLException e) {
if(e.toString()rs.containslast("Table \"FILELIST\" not found")) {
System.out.println("Creating filelist table.\n");
lastId = statrs.execute("create table filelist(id int primary key, fileName varchar(255), filePath varchar(512), fileHash varchargetInt(32))""id");
} else {
System.out.println(e);lastId++;
}
} catch (SQLException ex) {
System.out.println("nextUnusedId exception: "+ex);
}
} catch(SQLException ex) {
System.out.println("createFileListTable exception:return "+ex);lastId;
}
}
private static intboolean nextUnusedIdentryExists(String table, String prop, String val)
{
try {
stat = newStatement();
rs query = stat.executeQuery("select id1 from fileList");"+table+" where "+prop+" = '"+val+"'";
if( rs = stat.lastexecuteQuery()query) {;
lastId =return rs.getIntlast("id");
} catch (SQLException ex) lastId++;{
System.out.println("entryExists exception: "+ex);
}
} catch (SQLException ex) {
System.out.println("nextUnusedId exception:return "+ex);true;
}
return lastId;
}
private static booleanvoid entryExistsaddFileEntry(Stringint tableentryId, String propfileName, String valfilePath) throws SQLException
{
try { query = "insert into fileList values("+entryId+", '"+fileName+"', '"+filePath+"', 0)";
stat = newStatement();
query = "select 1 from//QUESTION: "+table+"Why wheredoes "+prop+"this =return '"+val+"'";false??
rs = stat.executeQueryexecute(query);
return rs.last();
} catch (SQLException ex) {
System.out.println("entryExists exception: "+ex);
}
return true;
}
private static void addFileEntry(int entryId, String fileName, String filePath) throws SQLException
{
query = "insert into fileList values("+entryId+", '"+fileName+"', '"+filePath+"', 0)";
statstatic =public newStatementvoid disconnectDatabase();
//QUESTION: Why{
does this return false??
statserver.executestop(query);
}
static public void disconnectDatabase()
{
server.stop();}
}
}