Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JDBC and Relational Databases and the fly likes prepared statement Vs Stored procedures Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "prepared statement Vs Stored procedures" Watch "prepared statement Vs Stored procedures" New topic

prepared statement Vs Stored procedures

Hari babu
Ranch Hand

Joined: Jun 25, 2001
Posts: 208
Hi all,
Whats the diffrenece between Stored procedure and prepared statements
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
You can think of a stored procedure as a method that exists within a db. i.e. code that lives in your db that you can execute. The language that they are written in (SPL) usually depends on the db vendor. For example, the syntax of an Oracle sp is different than an Informix sp. The benefits are: They execute quickly because they are an integral part of the db and are therefore optimized; They make it easy to encapsulate multiple queries/updates into one procedure.
PreparedStatements are part of the JDBC spec and are used to execute ANY kind of a statement against a db. Commonly, they are used to execute a "SELECT" or "UPDATE" statement, but they can even be used to execute a stored procedure! (although CallableStatements are better for executing stored procedures). When you call connection.prepareStatement(SQL), you are basically saying "I intend to execute this statement against this connection". They are very useful if you are executing the same statement multiple times, just with different parameters. Since you only "prepare" the statement once and then just change the input params as needed, the db has to check the sytax, build the query plan, etc., only once.
I think I went too far. To recap...
A stored procedure is compiled, db-specific code that resides within a db.
A PreparedStatement is just an efficient way in Java to execute any kind of an SQL statement.

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
I agree. Here's the link:
subject: prepared statement Vs Stored procedures
jQuery in Action, 3rd edition