prepared statement

19

Click here to load reader

Upload: shubhangi

Post on 19-Jul-2016

12 views

Category:

Documents


0 download

DESCRIPTION

ppt prepared statement

TRANSCRIPT

Page 1: Prepared Statement

Prepared Statement

Shubhangi Shinde

Page 2: Prepared Statement

PreparedStatement features are

• Easy to insert parameters into the SQL statement.

• Easy to reuse the PreparedStatement with new parameters.

• May increase performance of executed statements.

• Enables easier batch updates.

Page 3: Prepared Statement

Creating a PreparedStatement• String sql = "select * from COFEES where

SUP_ID=?";• PreparedStatement preparedStatement =

connection.prepareStatement(sql); • Once a PreparedStatement is created (prepared)

for the above SQL statement, you can insert parameters at the location of the question mark. This is done using the many setXXX() methods. Here is an example:

• preparedStatement.setLong(1, 123);

Page 4: Prepared Statement

With executeQuery()

• executeQuery() method returns a ResultSet.• String sql = "select * from people where

firstname=? and lastname=?"; • PreparedStatement preparedStatement =

connection.prepareStatement(sql); • preparedStatement.setString(1, “ASMIT"); • preparedStatement.setString(2, “SHUKLA");• ResultSet result =

preparedStatement.executeQuery();

Page 5: Prepared Statement

With executeUpdate()• The executeUpdate() method is used when updating

the database. It returns an int which tells how many records in the database were affected by the update.

• String sql = "update people set firstname=? , lastname=? where id=?";

• PreparedStatement preparedStatement = connection.prepareStatement(sql);

• preparedStatement.setString(1, "Gary"); • preparedStatement.setString(2, "Larson"); • preparedStatement.setLong (3, 123); • int rowsAffected =

preparedStatement.executeUpdate();

Page 6: Prepared Statement

PreparedStatement Performance

• It takes time for a database to parse an SQL string, and create a query plan for it. A query plan is an analysis of how the database can execute the query in the most efficient way.

• If you submit a new, full SQL statement for every query or update to the database, the database has to parse the SQL and for queries create a query plan.

• By reusing an existing PreparedStatement you can reuse both the SQL parsing and query plan for subsequent queries.

• This speeds up query execution, by decreasing the parsing and query planning overhead of each execution.

Page 7: Prepared Statement

Batch Updates

• A batch update is a batch of updates grouped together, and sent to the database in one "batch", rather than sending the updates one by one.

• Sending a batch of updates to the database in one go, is faster than sending them one by one, waiting for each one to finish.

• There is less network traffic involved in sending one batch of updates (only 1 round trip), and the database might be able to execute some of the updates in parallel.

Page 8: Prepared Statement

• You can batch both SQL inserts, updates and deletes. It does not make sense to batch select statements.

• There are two ways to execute batch updates:– Using a Statement– Using a PreparedStatement

Page 9: Prepared Statement

Statement Batch Updates

• You do so using the addBatch() and executeBatch()methods.Statement statement = null; try{ statement = connection.createStatement();

statement.addBatch("update people set firstname='John' where id=123");

statement.addBatch("update people set firstname='Eric' where id=456");

statement.addBatch("update people set firstname='May' where id=789");

int[] recordsAffected = statement.executeBatch(); } finally { if(statement != null) statement.close(); }

Page 10: Prepared Statement

PreparedStatement Batch Updates

• You can also use a PreparedStatement object to execute batch updates.

Page 11: Prepared Statement

• String sql = "update people set firstname=? , lastname=? where id=?";

• PreparedStatement preparedStatement = null;• try{ preparedStatement = connection.prepareStatement(sql);• preparedStatement.setString(1, "Gary");• preparedStatement.setString(2, "Larson"); • preparedStatement.setLong (3, 123); • preparedStatement.addBatch(); • preparedStatement.setString(1, "Stan"); • preparedStatement.setString(2, "Lee"); • preparedStatement.setLong (3, 456); • preparedStatement.addBatch();• int[] affectedRecords = preparedStatement.executeBatch(); }• finally { if(preparedStatement != null)

{ preparedStatement.close(); } }

Page 12: Prepared Statement

Transaction Management

• By default all jdbc connection is in auto-commit mode. Every jdbc statement is committed to database after completion.

• Turning off auto-commit will have following advantages:– To increase performance.– To maintain integrity of business performance.– To use distributed transactions.

• Trnsactions enable you to control database.

Page 13: Prepared Statement

• Transactions treats a single SQL statement or a group of SQL statements as one logical unit, and if any statement fails, the whole transaction fails.

• conn.setAutoCommit(false);• Once you are done with your changes and you want

to commit the changes then call commit() method on connection object as follows:conn.commit( );

• Otherwise, to roll back updates to the database made using the Connection named conn, use the following code:conn.rollback( );

Page 14: Prepared Statement

• try{• //Assume a valid connection object conn

conn.setAutoCommit(false);• Statement stmt = conn.createStatement(); String

SQL = "INSERT INTO Employees " + "VALUES (106, 20, 'Rita', 'Tez')"; stmt.executeUpdate(SQL);

• SQL = "INSERTED IN Employees " + "VALUES (107, 22, 'Sita', 'Singh')"; stmt.executeUpdate(SQL);

• // If there is no error. • conn.commit(); }• catch(SQLException se){ // If there is any error.

conn.rollback(); }

Page 15: Prepared Statement

Using Savepoints

• Savepoint interface gives you additional transactional control.

• When you set a savepoint you define a logical rollback point within a transaction.

• If an error occurs past a savepoint, you can use the rollback method to undo either all the changes or only the changes made after the savepoint.

Page 16: Prepared Statement

Methods of savepoints

• setSavepoint(String savepointName): defines a new savepoint. It also returns a Savepoint object.

• releaseSavepoint(Savepoint savepointName): deletes a savepoint. Notice that it requires a Savepoint object as a parameter. This object is usually a savepoint generated by the setSavepoint() method.

Page 17: Prepared Statement
Page 18: Prepared Statement
Page 19: Prepared Statement