Question
HCLTech
NL
Last activity: 18 Nov 2024 6:16 EST
Retrieve password defined on DB instance
Hi,
We have developed Java code to fulfill our business needs and in it, we have created a database connection using JNDI name and ran a SQL query. Because of the latest changes, all Database rules now point to the Database URL instead of the JNDI name. Currently, we are seeking a resolution to fetch the DB password that was set in the Database. Our attempts with the code provided have consistently resulted in an empty password field.
Previous Code:
String databaseName = clDef.getTable().getDBName(); String dbHandle = "DATA-ADMIN-DB-NAME " + databaseName.toUpperCase(); String jndiName = db.open(dbHandle,true).getProperty("pyDataSourceJNDIName").getStringValue();
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup(jndiName);
Connection con=ds.getConnection();
New Code changes:
String connectionUrl = db.open(dbHandle,true).getProperty("pyDatabaseURL").getStringValue(); String userName = db.open(dbHandle,true).getProperty("pyDBUser").getStringValue(); String pwd = db.open(dbHandle,true).getProperty("pyDBAdminPassword").getStringValue(); oLog.infoForced(pwd); Connection con=DriverManager.getConnection(connectionUrl,userName,pwd);
Hi,
We have developed Java code to fulfill our business needs and in it, we have created a database connection using JNDI name and ran a SQL query. Because of the latest changes, all Database rules now point to the Database URL instead of the JNDI name. Currently, we are seeking a resolution to fetch the DB password that was set in the Database. Our attempts with the code provided have consistently resulted in an empty password field.
Previous Code:
String databaseName = clDef.getTable().getDBName(); String dbHandle = "DATA-ADMIN-DB-NAME " + databaseName.toUpperCase(); String jndiName = db.open(dbHandle,true).getProperty("pyDataSourceJNDIName").getStringValue();
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup(jndiName);
Connection con=ds.getConnection();
New Code changes:
String connectionUrl = db.open(dbHandle,true).getProperty("pyDatabaseURL").getStringValue(); String userName = db.open(dbHandle,true).getProperty("pyDBUser").getStringValue(); String pwd = db.open(dbHandle,true).getProperty("pyDBAdminPassword").getStringValue(); oLog.infoForced(pwd); Connection con=DriverManager.getConnection(connectionUrl,userName,pwd);
To establish a DB connection we must need to send the user name and password along with the URL, It is not advisable to hardcode user credentials for security reasons and it is not considered a best practice.