banner image
Sedang Dalam Perbaikan

Why do we use prepared Statement object?


Why do we use prepared Statement object?

The PrepaeredStatement are used for executing precompiled SQL statements and passing in different parameters to it.
 PreparedStatement object differs from Statement object as that it is used to create a statement in standard form that is sent to database for compilation, before actually being used.
 Each time you use it, you simply replace some of the marked parameters (?) using some setter methods.
 We can create PreparedStatement object by using prepareStatementmethod of the connection class.

The SQL query is passed to this method as an argument as shown below. PreparedStatement pStmt = con.prepareStatement (“UPDATE tableName SET columnName = ? ” + “WHERE columnName =?”);
 Notices that we used marked parameters (?) in query. We will replace them later on by using various setter methods.
 If we want to replace first? With String value, we use setString method and to replace second? With int value, we use setInt method. This is shown in the following code snippet.
pStmt.setString (1 , stringValue); pStmt.setInt (2 , intValue)
Why do we use prepared Statement object? Why do we use prepared Statement object? Reviewed by MCH on April 27, 2014 Rating: 5

No comments:

Powered by Blogger.