Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Statement, PreparedStatement,

 
Zoram Paul
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Can anyone tell me when to use Statement and when PrepareStatement while handling databases?.. I know its always better to use PrepareStatement but Statement has its own charm, but when should we use it and why?

Please advice. I am confused.

 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Statement is used for one shot or single hit to database. Like when you have a query which will always execute the same statement, you can prefer Statement.

HtH.
 
Zoram Paul
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mr. Raghavan,
Thanks, Your reply was good.
Can you give me an example,
Say while inserting data into a database, sometime we use Statement and sometime the PrepareStatement. Whats the difference?

Please advice.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mizoram Java,

I am sure that Java is not your last name and Mizoram (a state in NE , India) is not your first name.Please check with the naming policy of Javaranch. ;)

Going forward to what Raghavan Muthu explained ,

Prepared statements are compiled , so in case where you are going to execute a query again and again , prepared statements are better choice.Just set the appropriate value at proper indexes and execute it.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Mizoram Java"

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
Scott Selikoff


(thanks Rahul)
 
Zoram Paul
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about Java from Mizoram
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mizoram,

I think you SHOULD have to adjust your REAL NAME as its one of the mandatory policies in JavaRanch.

Please have a look this url for the example.

Also have a look at the following URLs.

  • thread in the Sun's tutorial on PreparedStatements.
  • Hemanth's JDBC Tutorial

  • HtH.
    [ July 10, 2007: Message edited by: Raghavan Muthu ]
     
    Zoram Paul
    Ranch Hand
    Posts: 59
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How about Java from Mizoram
     
    Zoram Paul
    Ranch Hand
    Posts: 59
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sEE i HAVE cHANGED mY nAME
     
    praveen sharma
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i wanna ask one question well as i saw above you people said Statement is best when single hit is to be made in database and when different hits are to be made then is the best.but i wanted to knew when we want to execute different query every time into our custom bean lets say i have created one Connection bean which makes loads the driver and establish the connection with database then i think Statement is the best choice.what you people say i don't think so will be the best choice.

    Send me the reply
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic