We just finished working on a Database project in Java Netbeans. We added the feature Import
which will import the database record of one user to a text file.
String IMPORT_QUERY =
"SELECT pe.entry_id ID, pe.entry_account ACCOUNT, pe.entry_username USERNAME, pe.entry_password PASSWORD, pe.entry_category CATEGORY FROM password_entries pe JOIN accounts a ON a.user_id = pe.user_id WHERE username = ? AND password = ?";
try {
PrintWriter writer = new PrintWriter("D:\\Users\\mandu\\Documents\\NetBeansProjects\\PasswordVault\\src\\PasswordList.txt", "UTF-8");
String location = "C:\\\\Users\\\\mandu\\\\Documents\\\\NetBeansProjects\\\\PasswordVault\\\\src";
pstmt = conn.prepareStatement(IMPORT_QUERY);
pstmt.setString(1, LogInFrame.username.getText());
pstmt.setString(2, LogInFrame.password.getText());
res = pstmt.executeQuery();
writer.println("ACCOUNT" + "\t\t|\t" + "USERNAME" + "\t\t\t|\t" + "PASSWORD" + "\t\t\t|\t" + "CATEGORY" + "\t\t|\t\n");
writer.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
while(res.next()){
entryAccount = res.getString("ACCOUNT");
entryUsername = res.getString("USERNAME");
entryPassword= res.getString("PASSWORD");
entryCategory = res.getString("CATEGORY");
writer.println(entryAccount + "\t\t\t|\t" + entryUsername + "\t\t\t|\t" + entryPassword + "\t\t\t|\t" + entryCategory + "\t\t\t|\t\n");
}
writer.println();
writer.println();
writer.println("++++++++++++++++++++++WARNING!++++++++++++++++++++++");
writer.println();
writer.println("FILE ADDRESS: " + location);
writer.println();
writer.println("Please COPY the FILE ADDRESS above and DELETE the 'PasswordList.txt' file AFTER you COPY you RECORDS.");
writer.println();
writer.println("TO AVOID SOMEONE FROM COPYING YOUR RECORDS.");
writer.close();
JOptionPane.showMessageDialog(null,"\nData Imported!","Prompt:", JOptionPane.INFORMATION_MESSAGE);
OpenFile();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,"\nFile not found. Please check the file location.","Error x203", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
} catch (UnsupportedEncodingException ex) {
JOptionPane.showMessageDialog(null,"\nUnsupported Encoding. Please check the Target file.","Error x204:", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
} catch(SQLException | HeadlessException ex){
JOptionPane.showMessageDialog(null,"\nError occured while communicating with the server.","Error x205", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"\nError occured in getting input.\nPlease try again.\n","Error x206:", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
Any ideas/opinions about the code, or is there any way to get the columns aside from getString()
? Did I violate some rules with regards to error checking?