hi all Can anybody explain me why we need prepared stmt,callablestmt and stored procedures...what are the advantages of using each over others.. thankx priya
Ratan Dhariwal
Greenhorn
Joined: Aug 01, 2001
Posts: 3
posted
0
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 Ratan...
Shashank Hiwarkar
Ranch Hand
Joined: Feb 17, 2002
Posts: 32
posted
0
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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.