Go Back   Java Programming Forums > Java Standard Edition Programming Help > Java SE APIs


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-11-2009, 10:46 AM
Junior Member
 

Join Date: Sep 2009
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
kalees is on a distinguished road
Lightbulb String Vs StringBuffer

Hi!

I am working in web application project,
I have a doubt,when should use String and String Buffer, I know the concept String in immutable and StringBuffer is muttable,So every time i am assign the value into the String its taking separate memory,....

My doubt is I have a executeQuerymanager Class,...So when i want to execute the query so i jst put the query in String and call the executeQuerymanager Class,So in my Class if i want to execute two the String So i am Using Same String variable and Calling the Class,.....

So shall i make it StringBuffer instead of String?.....

Hope u underStand my doubt,...

eg:

Java Code
String query = "";
ResultSet rs = null;

query = "select * from XXX";

rs = executeQueryManager(query);

query = "select * from YYY";

rs = = executeQueryManager(query);
Hope U understand my example....

Thanks,

kalees



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 04-11-2009, 11:34 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default 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.

Java Code
private static final String SELECT_ALL_USERS = "select username,password,email from userstable";
And then you just pass that into your queryManager.

// Json
Reply With Quote
  #3 (permalink)  
Old 04-11-2009, 12:28 PM
Junior Member
 

Join Date: Sep 2009
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
kalees is on a distinguished road
Exclamation 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 :

Java 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?.....
Reply With Quote
  #4 (permalink)  
Old 05-11-2009, 07:25 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: String Vs StringBuffer

I meant you should have a list of queries.

Java 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
Reply With Quote
  #5 (permalink)  
Old 06-11-2009, 04:59 AM
Junior Member
 

Join Date: Sep 2009
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
kalees is on a distinguished road
Lightbulb 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,

Java 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?.....
Reply With Quote
  #6 (permalink)  
Old 06-11-2009, 07:27 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default 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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
error cannot find symbol-constructor Person(java.lang.String,java.lang.String,Date) ss7 What's Wrong With My Code? 2 03-11-2009 10:28 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:04 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.