Re: String Vs StringBuffer
Usually you'd have a number of static queries I believe, so if I was you I'd create some static variables holding the constants for your queries.
Code :
private static final String SELECT_ALL_USERS = "select username,password,email from userstable";
And then you just pass that into your queryManager.
// Json
Re: String Vs StringBuffer
Thanks,
But i am having different querys in my action class,each time i calling queryManager,i am jst passing the query,....
eg :
Code :
query ="select userName,userPass from userTable";
rs = queryManager(query);
query = "select price from priceTable;
rs = queryManager(query);
Some time May have a 4 more also,So in this kind of situation what to use String or StringBuffer?.....
Re: String Vs StringBuffer
I meant you should have a list of queries.
Code :
private static final String QUERY1 = "select username,password,email from userstable";
private static final String QUERY2 = "select * from someothertable";
private static final String QUERY3 = "select * from userstable WHERE username=?";
And just add all your queries like this. How does your queryManager actually call these?
// Json
Re: String Vs StringBuffer
Yes!
I am doing web application banking project,in struts frame work,
in my action class i am getting query result from different tables,so my QueryManager will return
the query result set,So my doubt is,
Code :
private static final String QUERY1 = "select username,password,email from userstable";
private static final String QUERY2 = "select * from someothertable";
private static final String QUERY3 = "select * from userstable WHERE username=?";
if i create the query with different string,
will it be create more object then it takes more memory right?
So if i use StringBuffer then it will use the same memory right?.....
What is the benefit of creating different string obj for different query?.....
Re: String Vs StringBuffer
When you declare all your queries as statics they will of course take up memory like that but you will have a reference to the same strings so it will be dead quick.
When you use the StringBuffer you will create a lot of strings anyways and they all get stored into the string pool anyways so in effect they will take up memory.
Using a stringbuilder or even stringbuffer which is slower will take more time in the long end. In my opinion there's no reason to use a StringBuilder if you know that the strings will be the exactly same every time.
Otherwise you can create a properties file in your web-inf/classes folder called databaseQueries.properties and load that into memory and just get the query you need from it.
The amount of memory taken up by your queries will be nothing compared to the objects you will be putting on the session anyways I believe.
// Json