aspose file tools
The moose likes JDBC and the fly likes Statement, PreparedStatement, Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Statement, PreparedStatement, " Watch "Statement, PreparedStatement, " New topic
Author

Statement, PreparedStatement,

Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
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.



ZORAM<br />"If it's true that we are here to help others,<br />then what exactly are the others here for?"
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
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

Joined: Nov 29, 2005
Posts: 2300
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.


Rahul Bhattacharjee
LinkedIn - Blog
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3652

"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)


My Blog: Down Home Country Coding with Scott Selikoff
Zoram Paul
Ranch Hand

Joined: Jul 10, 2007
Posts: 59
How about Java from Mizoram
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

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

    Joined: Jul 10, 2007
    Posts: 59
    How about Java from Mizoram
    Zoram Paul
    Ranch Hand

    Joined: Jul 10, 2007
    Posts: 59
    sEE i HAVE cHANGED mY nAME
    praveen sharma
    Ranch Hand

    Joined: May 27, 2006
    Posts: 44
    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
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: Statement, PreparedStatement,
     
    Similar Threads
    How to execute two SQL stmts. simulteneously ?
    PreparedStatement-Statement
    Help!!!!!******* java.sql.SQLException********
    java.sql.SQLException: ORA-01008: not all variables bound
    Pre compilation of Prepared Statement