This is on top of @tim's excellent review. (Please follow everything he said.)
Paths in Java
Here's a tip about typing paths:
just use /
as the path separator, Java will figure out the right thing to use for the underlying operating system, for example:
File basedir = new File("C:/Users/mandu/Documents/NetBeansProjects/PasswordVault/src");
Then you can rewrite your writer
and location
in terms of that:
PrintWriter writer = new PrintWriter(new File(basedir, "PasswordList.txt"), "UTF-8");
String location = basedir.getPath();
By the way you used two very similar paths,
one starting with C:/
and another starting with D:/
.
Looks like a bug.
This tends to happen when duplicating stuff in code rather than reusing common logic.
Order of operations
I suggest to think through the order in which you perform operations.
In particular,
it's pointless and potentially dangerous to open an output file for writing before you have anything to write.
You should create the writer
after you could create the database connection and execute queries and ready to write results.
Your questions
Using getString
to get column values is fine like that, when you work with a ResultSet
object. There are other ways, for example using frameworks that can map rows to Java objects, such as Hibernate and Spring and probably many others.
As for error handling, in a serious application there is no place for printing stack traces on the console. The recommended practice is to write to a log file instead. But the bigger problem is having a large try
block with many catch statements. As @tim already suggested, you should split that up to smaller functions, and then you will have smaller try-catch blocks naturally.