3

I tried to understand the difference between PreparedStatements & CallableStatements and I couldn't get it. so please can anyone convert following sql Query to CallableStatement. I know how to convert Statement into a PreparedStatement but having problems with CallableStatements.

as a java.sql.Statement

SELECT * FROM Customer WHERE customerId = 'C001'

as a java.sql.PreparedStatement

SELECT * FROM Customer WHERE customerId = ? //set customerId using preparedStatement.setString(1,"C001")

How to write this same query in CallableStatements

Thanks in advance!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Lakshan Dissanayake
  • 521
  • 1
  • 4
  • 18
  • CAllablestatement used for StoredProcedure Activities. – Akshay Joy Mar 24 '13 at 18:38
  • 1
    Possible duplicate of [JDBC - Statement, PreparedStatement, CallableStatement and caching](https://stackoverflow.com/questions/8371053/jdbc-statement-preparedstatement-callablestatement-and-caching) – rene Oct 07 '17 at 20:58

3 Answers3

6

As stated in the CallableStatement API:

The interface used to execute SQL stored procedures.

And as such, it can not be used to execute queries.

MrLore
  • 3,759
  • 2
  • 28
  • 36
1

Callable Statements are used for accessing stored procedures.

You have to write stored procedure in database

for

SELECT * FROM Customer WHERE customerId = 'C001'

And

please look into the following links to have help on callable statements -

1 > http://www.mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-out-parameter-example/

2> http://www.tutorialspoint.com/jdbc/callablestatement-object-example.htm

Thanks

Piyas De
  • 1,786
  • 15
  • 27
0

check this

DIfference Between Stored Procedures and Prepared Statements..?

CallableStatement used for mainly StoredProcedure

Community
  • 1
  • 1
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23