File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes JDBC Connection Password? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
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: 20273

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
author & internet detective

Joined: May 26, 2003
Posts: 33108

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, 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!
I agree. Here's the link:
subject: JDBC Connection Password?
It's not a secret anymore!