File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Preapred stmt 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 "Preapred stmt" Watch "Preapred stmt" New topic

Preapred stmt

priya shankar
Ranch Hand

Joined: May 12, 2003
Posts: 51
hi all
Can anybody explain me why we need prepared stmt,callablestmt and stored procedures...what are the advantages of using each over others..
Ratan Dhariwal

Joined: Aug 01, 2001
Posts: 3
They are different means of accessing database.
In short
Prepared statement are precompiled when you prepare them and they can be used multiple time. after they are prepared (ofcourse in same connection) leading to more efficient dataaccess. SQL statements that accept parameters can be used for prepared statement.
for example
String sql = " Select EmpName, EmpSSN from Employee where employeeID = ? ";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, 110592)
ResultSet rs = pstmt.executeQuery

If same SQL statement is used repeated times it is probably a good Idea to use prepared statements. If you want to modularize or avoid formatting of sql in java you can prefer using prepared statements. Do note that There is performance overhead for precompiling. It really pays only when it is reused.

Stored procedures are SQL statements compiled and stored in database, Most of the time they are propritery please refer to your database vendors documentation. They are alternate but less prefered way to acces, as they are not portable.
Callable statements are actually calls to stored procedures stored in databases. Callable statements help us make such calls, to any stored procedure.

Please look at java Docs for more information in regard to APIs. There is another thing called batch statements using JDBC, but use of Batch statements depends on JDBC driver and support from database vendor.
Hope it is helpful
Shashank Hiwarkar
Ranch Hand

Joined: Feb 17, 2002
Posts: 32
Dear Friends,
Prepared statements are like the stored procedure / funtion in the database. They are parsed only once, like funtions / procedures. It executes DML / DDL statements like stored procedure.
While Statements are like other DML / DDL statements of database, which are executed every time you call them.
Stored procedures and Functions in database are executed with Callable Statement.

Shashank Hiwarkar<br />SCJP, SCWCD
I agree. Here's the link:
subject: Preapred stmt
It's not a secret anymore!