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
# 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
# 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.
# 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.
# 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