Breaking News
yeni film izle hd film seyret film izle

INTRODUCTION OF QUERYING AND MODIFYING DATA

INTRODUCTION OF QUERYING AND MODIFYING DATA

PreparedStatement: This is an interface of java.sql package which extends Statement interface. If you want to execute Statement object many times then we should use PreparedStatement object as it reduces the execution time. PreparedStatement object is faster than the Statement object as it is precompiled. In PreparedStatement we use IN parameter whose values are not known when the Sql statement is created. So we use “?” as a IN parameter and we also know it by the name of parameter marker. In this interface the modification has been done on the methods execute, executeQuery and executeUpdate. These methods are modified in such a way so that they take no argument.
An element in a batch consists of a parameterized command and an associated set of parameters when a PreparedStatement is used. The batch update facility is used with a PreparedStatement to associate multiple sets of input parameter values with a single PreparedStatement object. The sets of parameter values together with their associated parameterized update command can then be sent to the underlying DBMS engine for execution as a single unit.
The example below inserts two new employee records into a database as a single batch. ThePreparedStatement.setXXX() methods are used to create each parameter set (one for each employee), while the PreparedStatement.addBatch() method adds a set of parameters to the current batch.
// turn off autocommit
con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement(
“INSERT INTO employees VALUES (?, ?)”);
stmt.setInt(1, 2000);
stmt.setString(2, “Kelly Kaufmann”);
stmt.addBatch();
stmt.setInt(1, 3000);
stmt.setString(2, “Bill Barnes”);
stmt.addBatch();
// submit the batch for execution
int[] updateCounts = stmt.executeBatch();
Finally, PreparedStatement.executeBatch() is called to submit the updates to the DBMS. Calling PreparedStatement.executeBatch() clears the statement’s associated list of batch elements. The array returned by PreparedStatement.executeBatch() contains an element for each set of parameters in the batch, similar to the case for Statement. Each element either contains an update count or the generic `success’ indicator .
Error handling in the case of PreparedStatement objects is the same as error handling in the case of Statement objects. Some drivers may stop processing as soon as an error occurs, while others may continue processing the rest of the batch. As for Statement, the number of elements in the array returned by BatchUpdateException.getUpdateCounts() indicates whether or not the driver continues processing after a failure. The same three array element values are possible, as for Statement. The order of the entries in the array is the same order as the order in which elements were added to the batch.
Description of program:
In this program we are going to insert data in the database by using the setObject method of PreparedStatement interface. Before going into the details of the program we should firstly need to establish the connection with MySQL database by the help of JDBC driver. After establishing the connection now we will insert the data in setObject method. If the data gets added in the database table then it will display a message “Record is added in the table.” otherwise it will show “SQL statement is not executed!”.
Description of code:
prepareStatement(String sql):
This method returns the PreparedStatement object for sending the parameterized SQL statement to the database that contains the pre-compiled SQL statement. Here the pre- compiled means once the statement has been compiled then it will not compile the same thing again. It takes the string type arguments which contains one or more ‘?’ parameter placeholders.
setObject(int par_index, object obj):
It is used for setting the values of parameterized index by using the given object in this method. It takes two arguments to given below:
par_index: It specifies the parameter like: the first is 1, second is 2, ……
object obj: It contains the parameter values to given by the users.
Here is the code of program:
import java.sql.*;
public class PreparedStatementSetObject{
public static void main(String[] args) {
System.out.println(“Prepared Statement Set Array Example!”);
Connection con = null;
try{
Class.forName(“com.mysql.jdbc.Driver”);
con = DriverManager.getConnection
(“jdbc:mysql://localhost:3306/jdbctutorial”,”root”,”root”);
try{
PreparedStatement prest = con.prepareStatement(“insert emp_sal values(?,?)”);
prest.setObject(1,”Sushil”);
prest.setObject(2,15000);
int n = prest.executeUpdate();
System.out.println(n + ” Record is added in the table.”);
con.close();
}
catch (SQLException s){
System.out.println(“SQL statement is not executed!”);
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
The PreparedStatement interface inherits from Statement and differs from it in two ways:
1. Instances of PreparedStatement contain an SQL statement that has already been compiled. This is what makes a statement “prepared.”
2. The SQL statement contained in a PreparedStatement object may have one or more IN parameters. An IN parameter is a parameter whose value is not specified when the SQL statement is created. Instead, the statement has a question mark (“?”) as a placeholder for each IN parameter. The “?” is also known as a parameter marker. An application must set a value for each question mark in a prepared statement before executing the prepared statement.
Because PreparedStatement objects are precompiled, their execution can be faster than that of Statement objects. Consequently, an SQL statement that is executed many times is often created as a PreparedStatement object to increase efficiency.
Being a subclass of Statement, PreparedStatement inherits all the functionality of Statement. In addition, it adds a set of methods that are needed for setting the values to be sent to the database in place of the placeholders for IN parameters. Also, the three methods execute, executeQuery, and executeUpdate are modified so that they take no argument. The Statement forms of these methods (the forms that take an SQL statement parameter) should never be used with a PreparedStatement object.