Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java equivalent for MFC's CString::Format()

Status
Not open for further replies.

zooxmusic

Programmer
Nov 24, 2004
96
US
Hi all, For the life of me I can't find a way to do this. Forgive my ignorance. Here is my question:

I need a way to replace a ? or many ?'s in a string with a value. How can I accomplish this?

in MFC you could do

resultCString = someCString.format("you can't enter %s for %s", "value1", "value2");

and you would get
resultCString = "you can't enter value1 for value2"


Is there a way to do this in java?

Thanks in advance
Brian


Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Got it. java.util.Formatter

Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
I'd go

resultString = "You can't enter" + value1 + "for" + value2.

Cheers,

Dian
 
Yes, but using something like
Code:
"you can't enter %s for %s"
means it can be stored in some kind of resource (such as the language resource bundles).
 
From his post it's difficult to tell what he's trying to do. Using the formatter will take more time and resources.

Cheers,

Dian
 
Granted. I'm simply assuming that Brian knows about string concatenation and that his needs may go beyond that, hence the post.
 
You are correct timw,
Basically I need a generic sql statement that have a lot of common language except for the table name and some field names

i.e

sql = "select * from %s where condition etc..."

and I have like 15 tables to go to so I figure it would be better to have this as a string and then just

String.format(sql, "TABLE_1", etc...);
String.format(sql, "TABLE_2", etc...);
etc...

it all worked out as per JDK 5.0 because they added this functionality into the String class. Thanks for the replies though.

Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
#1 - Don't use arbitrary SQL statements - they are dangerous & inefficent. Use stored procedures (CallableStatement) where possible, and if not, at least use a PreparedStatement. There is really no need to use JDBC the way you are doing ...

--------------------------------------------------
Free Database Connection Pooling Software
 
I understand that sedj. I plan on using PreparedStatement's in the future but I am under the gun and not a great OO designer yet. I have parts of my app being converted into an OO fashion but I have to cut some corners because I don't understand it fully and I will have to read so I am using a String in my SqlFetcher and this works out ok.


Dian you keep saying concat but do you understand what i am doing?

All I can see in concat'ing the string would be very uninviting if statements. Maybe you can post what you believe will work.

If there are no parameters then the string will be something like:

sql = "select * from TABLE"

if I have one parameter it would be something to the effect:
sql += "where " + " Field = " + value

and if I have multiple conditions it would be

sql " AND " + "Field = " + value + " maybe another AND or maybe not

and then again if they are strings or not it would have to have the appropriate "'" around the field.

So please if you see a way to do this please share.



Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
If you had started your post with this instead of the MFC String thingie, all would have been more clear.

Anyway, I'm offering you an alternative solution, take the one you like more. Is it possible to do it with StringBuffers? It is and in fact that's my way to construct SQL queries.

Cheers,

Dian
 
Actually I just found an elegant way to do this with the Interpreter pattern [GoF]. Actually it is the QueryObject pattern in Martin Fowler's Patterns of Enterprise Application Architecture. If anyone is interested please look to either of these books although the Fowler one is exaclty what I was asking here.

Brian


Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top