Exforsys

Online Training

differce B/W statement and preparestatement

This is a discussion on differce B/W statement and preparestatement within the Java Tutorials forums, part of the Articles and Tutorials category; hi i am kaatamraju.i wouldlike to know diff.B/W statement and preparestatement ,if antone know please send me ...


Go Back   Exforsys > Articles and Tutorials > Java Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-28-2005, 07:42 AM
Junior Member
 
Join Date: Dec 2005
Posts: 2
kaatamraju is on a distinguished road
differce B/W statement and preparestatement

hi
i am kaatamraju.i wouldlike to know diff.B/W statement and preparestatement ,if antone know please send me
bye.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-25-2006, 12:40 AM
Junior Member
 
Join Date: Jan 2006
Location: India
Posts: 1
Servepreet is on a distinguished road
Send a message via Yahoo to Servepreet
Short answer:
1. The PreparedStatement is a slightly more powerful version of a Statement, and should always be at least as quick and easy to handle as a Statement.
2. The PreparedStatement may be parametrized.

Longer answer: Most relational databases handles a JDBC / SQL query in four steps:
1. Parse the incoming SQL query
2. Compile the SQL query
3. Plan/optimize the data acquisition path
4. Execute the optimized query / acquire and return data

Code samples
Statement example
// Assume a database connection, conn.
Statement stmnt = null;
ResultSet rs = null;
try
{
// Create the Statement
stmnt = conn.createStatement();

// Execute the query to obtain the ResultSet
rs = stmnt.executeQuery("select * from aTable");
}
catch(Exception ex)
{

System.err.println("Database exception: " + ex);
}


PreparedStatement example

// Assume a database connection, conn.
PreparedStatement stmnt = null;
ResultSet rs = null;
try
{
// Create the PreparedStatement
stmnt = conn.prepareStatement("select * from aTable");

// Execute the query to obtain the ResultSet
rs = stmnt.executeQuery();
}
catch(Exception ex)
{
System.err.println("Database exception: " + ex);
}


Another advantage of the PreparedStatement class is the ability to create an incomplete query and supply parameter values at execution time. This type of query is well suited for filtering queries which may differ in parameter value only:


SELECT firstName FROM employees WHERE salary > 50
SELECT firstName FROM employees WHERE salary > 200


To create a parametrized prepared statement, use the following syntax:


// Assume a database connection, conn.
PreparedStatement stmnt = null;
ResultSet rs = null;
try
{
// Create the PreparedStatement, leaving a '?'
// to indicate placement of a parameter.
stmnt = conn.prepareStatement(
"SELECT firstName FROM employees WHERE salary > ?");

// Complete the statement
stmnt.setInt(1, 200);

// Execute the query to obtain the ResultSet
rs = stmnt.executeQuery();
}
catch(Exception ex)
{
System.err.println("Database exception: " + ex);
}


Servepreet
09358104361
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 11:23 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.