Statement Types
Statement Types
A Statement object sends SQL statements to a database. There are three kinds of Statement
objects. Each is specialized to send a particular type of SQL statement:
1. Create a Statement: From the connection interface, you can create the object for this
interface. It is generally used for general–purpose access to databases and is useful while
using static SQL statements at runtime.
Syntax:
Implementation: Once the Statement object is created, there are three ways to execute it.
boolean execute(String SQL): If the ResultSet object is retrieved, then it returns true
else false is returned. Is used to execute SQL DDL statements or for dynamic SQL.
int executeUpdate(String SQL): Returns number of rows that are affected by the
execution of the statement, used when you need a number for INSERT, DELETE or
UPDATE statements.
ResultSet executeQuery(String SQL): Returns a ResultSet object. Used similarly as
SELECT is used in SQL.
Example:
// Class
class Sample {
2. Prepared Statement represents a recompiled SQL statement, that can be executed many
times. This accepts parameterized SQL queries. In this, “?” is used instead of the parameter,
one can pass the parameter dynamically by using the methods of PREPARED STATEMENT
at run time.
Illustration:
Considering in the people database if there is a need to INSERT some values, SQL
statements such as these are used:
To do the same in Java, one may use Prepared Statements and set the values in the ? holders,
setXXX() of a prepared statement is used as shown:
Implementation: Once the PreparedStatement object is created, there are three ways to
execute it:
execute(): This returns a boolean value and executes a static SQL statement that is
present in the prepared statement object.
executeQuery(): Returns a ResultSet from the current prepared statement.
executeUpdate(): Returns the number of rows affected by the DML statements such
as INSERT, DELETE, and more that is present in the current Prepared Statement.
Example:
// Main class
class Sampledb{
3. Callable Statement are stored procedures which are a group of statements that we compile
in the database for some task, they are beneficial when we are dealing with multiple tables
with complex scenario & rather than sending multiple queries to the database, we can send
the required data to the stored procedure & lower the logic executed in the database server
itself. The Callable Statement interface provided by JDBC API helps in executing stored
procedures.
Example:
// Java Program illustrating Callable Statement in JDBC
// Main class
class Sampledb1{
ResultSet Interface is present in the java.sql package. It is used to store the data which are
returned from the database table after the execution of the SQL statements in the Java
Program. The object of ResultSet maintains cursor point at the result data. In default, the
cursor positions before the first row of the result data.
The next() method is used to move the cursor to the next position in a forward direction. It
will return FALSE if there are no more records. It retrieves data by calling the
executeQuery() method using any of the statement objects. It may be Statement or
PreparedStatement or CallableStatement object. PreparedStatement, and CallableStatement
interfaces are the sub-interfaces of the Statement interface.
Statement Interface
PreparedStatement Interface
We can use getX() method to get the data of the columns while iterating through the results
where X – is the datatype of the column. We can use either Column Names or Index to get
the values using getX() methods.
while(rs1.next())
We can also mention index number of the Column instead of Column Name in the getX()
methods.
while(rs1.next())
}
ResultSet Types
In default, we can iterate the data/values in ResultSet which have returned as an output of the
executed SQL statement in the forward direction. We can iterate the values in other directions
using Scrollable ResultSet. We can specify the type and concurrency of ResultSet while
creating Statement, PreparedStatement, and CallableStatement objects.
1. TYPE_FORWARD_ONLY: It is the default option, where the cursor moves from start to end
i.e. in the forward direction.
2. TYPE_SCROLL_INSENSITIVE: In this type, it will make the cursor to move in both forward and
backward directions. If we make any changes in the data while iterating the stored data it
won’t update in the dataset if anyone changes the data in DB. Because the dataset has the
data from the time the SQL query returns the Data.
3. TYPE_SCROLL_SENSITIVE: It is similar to TYPE_SCROLL_INSENSITIVE, the difference is if
anyone updates the data after the SQL Query has returned the data, while iterating it will
reflect the changes to the dataset.
ResultSet Concurrency
Some databases don’t support concurrency mode for all ResultSet types. In that case, we need
to check whether they support our desired type and concurrency mode using
supportsResultSetConcurrency() method.
1. Navigational Methods
2. Getter Methods
3. Setter Methods
4. Miscellaneous Methods
First, we will discuss the Navigational Methods and then will move further.
Boolean absolute(int row): It is used to move the cursor to the specified row which is
mentioned in the parameter and return true if the operation is successful else return false.
Void afterLast(): It makes the ResultSet cursor to move after the last row.
Void beforeFirst(): It makes the ResultSet cursor to move before the first row.
Boolean first(): It makes the ResultSet cursor to move to the first row. It returns True if the
operation is successful else False.
Boolean last(): It makes the ResultSet cursor to move to the last row. It returns True if the
operation is successful else False.
Boolean next(): It makes the ResultSet cursor to move to the next row. It returns True if
there are more records and False if there are no more records.
Boolean previous(): It makes the ResultSet cursor to move to the previous row. It returns
True if the operation is successful else False.
Boolean relative(): It moves the cursor to the given number of rows either in the forward or
backward direction.
Int getRow(): It returns the current row number the ResultSet object is pointing now.
Void moveToCurrentRow(): It moves the cursor back to the current row if it is currently in
insert row.
Void moveToInsertRow(): It moves the cursor to the specific row to insert the row into the
Database. It remembers the current cursor location. So we can use the moveToCurrentRow()
method to move the cursor to the current row after the insertion.