HRgiger
HRgiger

Reputation: 2790

What is the benefit of using java.util.properties while connecting to database?

I was reading Oreilly Java Programming With Oracle JDBC edition and I notice that there was one example using java.util.properties object like below;

public class Connector {


    Connection con = null;
    private Properties info = new Properties();

    public void connect(){
        info.put("user", "sys as sysdba");
        info.put("password", "testpass");
        String driverName = "oracle.jdbc.driver.OracleDriver";

        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        String serverName = "localhost";
        String port = "1521";
        String sid = "XE";
        String url = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + sid;



        try {
            con = DriverManager.getConnection(url, info);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void closeConnection(){

        try {
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

I have checked API that I understand main benefit of properties is reading local sources. Google OCI driver examples using java.util.properties more than thin driver examples. My question is;

*Should I use properties for thin driver? *if yes what will be benefit?

*Why I can not cast full details as properties that I have to use String object for connection?

Thank you

Upvotes: 0

Views: 1520

Answers (2)

Josh
Josh

Reputation: 141

"To log on as SYSDBA with the JDBC Thin driver you must configure the server to use the password file. " http://docs.oracle.com/cd/B28359_01/java.111/b31224/dbmgmnt.htm

I'm trying to use a thin driver to connect to Oracle as sysdba but the connections are failing. I tried steps there but my attempts after those changes also fail.

Anyone interested in this, may also be interested in the internal_logon property since it is mentioned in relation to logging on for specific roles like sysdba but again, I had no success using that either.

Upvotes: 0

BalusC
BalusC

Reputation: 1109112

The properties could be loaded from some jdbc.properties file on classpath which allows you for external configuration. Its sole content could look like:

user=sys as sysdba
password=testpass

So, instead of

info.put("user", "sys as sysdba");
info.put("password", "testpass");

you could do

info.load(getClass().getResourceAsStream("/jdbc.properties"));

(note that the filename is free to your choice, it'll work as long as it's in the runtime classpath)

This way you don't need to edit, recompile, rebuild, fuss the class whenever you want to change the connection details. You just have to edit a simple textbased file. This is particularly helpful in distributed applications which needs to be managed by someone without any Java knowledge (e.g. serveradmins).

See also:

Upvotes: 5

Related Questions