patternjavaMinor
Importing MySQL Database to a .txt File
Viewed 0 times
filedatabasemysqlimportingtxt
Problem
We just finished working on a Database project in Java Netbeans. We added the feature
```
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 FI
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 FI
Solution
Any ideas/opinions about the code I written?
A couple of points on readability:
A couple of points on readability:
- your code does too much. It reads from the database, it prints to a file, it prints to a GUI, and it formats strings. I would create separate methods for each of these activities (or at least for the formatting and the printing to GUI).
- 240 characters are definitely too long for one line. I would try for 80 chars, 120 max.
- your directory location string is duplicated. Define it in a static field and then use
new PrintWriter(location + "\\PasswordList.txt", "UTF-8");
- why are there so many backslashes in your path? I'm not that familiar with Java + Windows, but this seems unnecessary.
- just using a bunch of tabs for alignment is quite messy (the code is messy, and the result will be messy as well). Use either CSV (the code will be cleaner, the output really messy, but at least it will be on purpose), or use some code for proper alignment.
- you have way too many
println. Just use\n.
- your indentation is off, which makes it harder to read.
- try to reduce the length of your try blocks. It's really hard to see what exception might be thrown by what operation if you just catch them all at the very end. You could also use try-with-resources.
Please COPY the FILE ADDRESS above and DELETE the 'PasswordList.txt' file: people are not going to do that. If this is important for security (and it kind of seems like it would be), you should find a different way to handle this.
- a lot of your variables seem to be defined in a large scope, even though it isn't necessary. Eg
entryX(which aren't needed at all, just use the value directly),pstmt,res, etc.
Context
StackExchange Code Review Q#83724, answer score: 7
Revisions (0)
No revisions yet.