2

I have a Java Swing program with database connnection. Up to now, my requirement was to hard code the connection details like below.

public void makeConnection() 
    {
        try
        {
             Class.forName("com.mysql.jdbc.Driver");

             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName","root","");

             //System.out.println("Connection Established");
        }
        catch(Exception e)
        {
            e.printStackTrace();

        }
    }

However the client's IT guy tried to make the connection to the DB via a VPN, and the system rejected to connect, most probably because the hard coded connectivity.

Now, I am looking for ways to store the connectivity data in a way that client can edit. Which means, like most general programs, client decides the db password, port etc. He might need it bcs a VPN is involved. So I am planning to create a small dialog box for the client to enter the DB Password, user, and server location. Once these data are saved, the program will read them and make the connection accordingly.

But the problem is, where do I save these? I know I can save them in a Text file but it is a non-secured method, any one can read it. some kind of hard coding is not an option, because client should be able to edit his password etc etc.

Any advice?

PeakGen
  • 21,894
  • 86
  • 261
  • 463

1 Answers1

3

You can store the connection details using java.util.prefs.Preferences, as suggested here, or a javax.jnlp.PersistenceService, as suggested here. Although platforms typically protect such data from casual snooping, you can store a salted hash using one of the approaches shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Actually this is not working for me.. `java.util.preferences` generates errors like `run: Dec 02, 2014 4:39:14 PM java.util.prefs.WindowsPreferences WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5. ` – PeakGen Dec 02 '14 at 11:11
  • Windows stores the settings on the registry, as discussed [here](http://stackoverflow.com/q/5354838/230513). For reference, a complete example that works on Windows is cited [here](http://stackoverflow.com/a/9481515/230513). – trashgod Dec 02 '14 at 12:20
  • Just for info, will this work without issues in Mac and Linux? – PeakGen Dec 02 '14 at 12:39
  • Each platform uses a different strategy, but I've had no issues on Mac OS X 6/9, Ubuntu 12/14 or Windows XP/7. See also [*Encrypted Preferences in Java*](http://www.drdobbs.com/security/encrypted-preferences-in-java/184416587). – trashgod Dec 03 '14 at 03:07