This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes JDBC Connection Password? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC Connection Password?" Watch "JDBC Connection Password?" New topic

JDBC Connection Password?

Paul Brabban
Ranch Hand

Joined: Jan 13, 2008
Posts: 44

This is my first post so apologies if I get anything wrong. I'm Paul from the UK, joined up here after reading about it in one of Kathy Sierra's study guides.

I have a question about good practise in connecting to databases via JDBC. If I have a simple program running that connects to a database, it needs a username/password to do so.

What is the most secure and flexible way to make a username/password combination available to the program? Sorry if it's obvious and I should know it!

Thanks everyone!

"The important thing is not to stop questioning."
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

If it's a command line tool, use one of the methods to read the password:

If it's a graphical user interface, use a JTextField and a JPasswordField:

How To Ask Questions How To Answer Questions
Paul Brabban
Ranch Hand

Joined: Jan 13, 2008
Posts: 44
Thanks for that, but I was meaning more in the case of unattended operation - a server daemon for example. If there isn't a person around to put in a password, what's the best way of storing that password so that I can get to it and use it to connect to my database?

Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30130

You can put it in a property file. For more security, you could encrypt it. You could also put it in a directory with limited read permissions.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Paul Brabban
Ranch Hand

Joined: Jan 13, 2008
Posts: 44
Thank you both for your advice, good to know that there isn't a simple gee-whiz solution that I missed!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: JDBC Connection Password?
Similar Threads
JSP to Access 2000 database
Retrieve DB2 credentials configured on websphere
JDBC Connections Freezing with Multiple Users Accessing DB
Transfer Huge data using Java
Conditional Redirecting using JSP