Reputation: 23352
Its a very strange question but I was thinking if there is any solution for it?
Scenerio is that I want to concatenate a String name
Like there is a String:
public static final String MYSQL_USERNAME = "mysql_user";
public static final String DB2_USERNAME = "db2admin";
public static final String ORACLE_USERNAME = "oracleuser";
...
if(dbType == "mysql"){
username = "MYSQL"+_username;
...
}else if(dbType == "db2"){
username = "DB2"+_username;
...
}else if(dbType == "oracle"){
username = "ORACLE"+_username;
...
}
i.e. at runtime I want to concatenate String based on dbType and fetch username.
Basically I want to make "MYSQL"+_username = MYSQL_USERNAME
I want to avoid these 3 if's
There should be only 1 line
username = dbType+_username;
Upvotes: 0
Views: 92
Reputation: 361565
Set up a map with the user names.
public static final Map<String, String> userNames = new HashMap<>();
{
userNames.put("mysql", "mysql_user");
userNames.put("db2", "db2admin");
userNames.put("oracle", "oracleuser");
}
Then you can look up the desired entry in the map given the database name.
userName = userNames.get(dbType);
Upvotes: 0
Reputation: 308743
I think this is a bad solution.
A better one would use database-specific .properties files with identical key names for each database. Your code need not know or care which database you're connecting to that way.
Upvotes: 1