Hi ,
Can any one tell me differences between Statement ,Prepared statement,CallableStatement and when to use which (Clear Idea)

I would suggest to look for tutorials on all of them.
In sort:

> Statement for executing "raw" queries:

String name = "Name A";
String query = "select * from table_A where name = [B]'[/B]" + name + "[B]'[/B]";

// Use statement to execute the above query.

> PreparedStatement for parametrized queries:

String name = "Name A";
String query = "select * from table_A where name = ? ";

PreparedStament ps = ....;
ps.clearParameters();
ps.setString(1, name);
ResultSet rs = ps.executeQuery();

> CallableStatement is used to execute stored procedures. I don't have an example with me. Try searching the internet for the last one.

And check the API for all of them

Statement is used when u have fixed query...
eg: select * from emp where eid=10...
this query will always provide a constant output..i.e. it will always retrieve the details of that particular employee whose id is 10
this kind of query is also know as hard core query...this kind of querying should be avoided...

Prepared Statement is used in case of dynamic data retrieval...i.e this is used for retrieving data as per user requirements at run time...
eg:

DataInputStream dis=new DataInputStream(System.in());
int no=Integer.parseInt(dis.readLine());

string query="Select * from emp where eid= ?";

[this "?" acts as a placeholder for the data to be entered by the user at runtime]

PreparedStatement ps=con.prepareStatement(query);
ps.setInt(1,no);

[here "1" stands for the first "?mark" to be matched with the user input number ==== as in our case we have only ?mark so no complications]

Callable statement is used for procedure calling or function calling...
i.e. if u have created any procedures or functions based on your tables in SQL server / Oracle....then through java api u can call them to perform some calculations n stuff....

please reply if this information was helpful to you...

thanks,
Jenifer

Edited 7 Years Ago by __avd: Add [code] tags. Encase your code in: [code] and [/code] tags.

Statement is used when u have fixed query...
eg: select * from emp where eid=10...
this query will always provide a constant output..i.e. it will always retrieve the details of that particular employee whose id is 10
this kind of query is also know as hard core query...this kind of querying should be avoided...

Prepared Statement is used in case of dynamic data retrieval...i.e this is used for retrieving data as per user requirements at run time...
eg:
DataInputStream dis=new DataInputStream(System.in());
int no=Integer.parseInt(dis.readLine());

string query="Select * from emp where eid= ?";

[this "?" acts as a placeholder for the data to be entered by the user at runtime]

PreparedStatement ps=con.prepareStatement(query);
ps.setInt(1,no);

[here "1" stands for the first "?mark" to be matched with the user input number ==== as in our case we have only ?mark so no complications]

Callable statement is used for procedure calling or function calling...
i.e. if u have created any procedures or functions based on your tables in SQL server / Oracle....then through java api u can call them to perform some calculations n stuff....

please reply if this information was helpful to you...

thanks,
Jenifer

You just repeated what I just said.
Also you can have dynamic queries with Staments as I said in my post.
You can have dynamic queries with both Stament and PreparedStament but the latter is faster only if it is executed many times.

This article has been dead for over six months. Start a new discussion instead.