how to hide username/password in JDBC program.

hi All,

The support tat I get here is simply gr8.

Can someone suggest or send me a link as to how I hide the username /password ....i.e database connection in the jdbc program so as to avoid changing the program everywhere in case username and passwd changes ?

I simply want to change the following code

String driverName = "oracle.jdbc.driver.OracleDriver";

Class.forName(driverName).newInstance();

// Create a connection to the database

Sring serverName = "127.0.0.1";

String portNumber = "1521";

String sid = "oratest";

String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;

String username = "scott";

String password = "tiger";

connection = DriverManager.getConnection(url, username, password);

Ta,

sunny

[837 byte] By [GloomyProgrammera] at [2007-11-27 5:45:46]
# 1
Store it in an external file. For example a simple propertiesfile. Check the java.util.Properties API.
BalusCa at 2007-7-12 15:27:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Why dont u use some simple bean, where you dont have to do any change in ur programe, simple you have to change your bean class and it will reflect in ur program ,more over it will remain hidden from an external user of ur programmer.

If you need code let me know, i will send u the same.

Cheers

Sandy

sandysharma2000a at 2007-7-12 15:27:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

hi

u can set the driver name, userid and password in the admin settings of ur server, http://localhost:8080/admin

click on datasource and set the properties.

In the code u create a context and use datasource to get the connection with that database.

Connection conn = null;

Context initContext = new InitialContext();

Context envContext = (Context) initContext.lookup("java:/comp/env");

DataSource ds = (DataSource) envContext.lookup("JNDIRef");

conn = ds.getConnection();

i think this will serve ur purpose.

Neelima.Sridhara at 2007-7-12 15:27:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

> Why dont u use some simple bean, where you dont have

> to do any change in ur programe, simple you have to

> change your bean class and it will reflect in ur

> program ,more over it will remain hidden from an

> external user of ur programmer.

This is not a solution, but a movement of the problem.

BalusCa at 2007-7-12 15:27:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Using Property file is the best way.
javamonka at 2007-7-12 15:27:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
use the ResourceBundle Class to access the property file and then get all the properties u require from the file while establishing the connection
javaj2eeguya at 2007-7-12 15:27:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
Don't use ResourceBundle. Those are intented for different kind of propertiesfiles (language-specific bundles in favour of internationalization).Just use the java.util.Properties API.
BalusCa at 2007-7-12 15:27:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

well i kind of support what my fellow poster neeelima.srtidhar said..

Use of Connection Pooling via JNDI which is either provided by appln server or using custom built frameworks is the best method which i can think of instead of reinventing the wheel by creating a Properties file and then writing a Utility Class which ultimately endup in creating connection Object without any sort of optimal usage.

checkout the below articles for reference of how to do it.

http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

http://www.informit.com/articles/article.asp?p=352320&rl=1

http://www.codeproject.com/useritems/pool_client_jboss.asp

http://edocs.bea.com/wls/docs81/jdbc/programming.html

http://www.castor.org/jdo-pooling.html

http://homepages.nildram.co.uk/~slink/java/DBPool/

and if you are thinking of reinventing those that could be possible using the below article

http://www.javaworld.com/javaworld/jw-05-2002/jw-0517-jdbcdriver.html?page=1

Hope that might help:)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 15:27:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...