0% found this document useful (0 votes)
20 views20 pages

Statement

Uploaded by

tejamicrosoftacc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views20 pages

Statement

Uploaded by

tejamicrosoftacc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Prepared Statement

PreparedStatement interface

• The PreparedStatement interface is a


subinterface of Statement. It is used to
execute parameterized query.
• Let's see the example of parameterized query:
• String sql="insert into emp values(?,?,?)";
How to get the instance of PreparedStatement?

• The prepareStatement() method of


Connection interface is used to return the
object of PreparedStatement.
Syntax:
• public PreparedStatement prepareStatement(
String query)throws SQLException{}
Methods

Method Description
public void setInt(int paramIndex, int sets the integer value to the given
value) parameter index.
public void setString(int paramIndex, sets the String value to the given
String value) parameter index.
public void setFloat(int paramIndex, sets the float value to the given
float value) parameter index.
public void setDouble(int paramIndex, sets the double value to the given
double value) parameter index.
public int executeUpdate() executes the query. It is used for
create, drop, insert, update, delete etc.
public ResultSet executeQuery() executes the select query. It returns an
instance of ResultSet.
Insert Values

• PreparedStatement stmt=con.prepareStatement("insert into
Emp values(?,?)");
• stmt.setInt(1,101);//
1 specifies the first parameter in the query
• stmt.setString(2,“Priya");

• int i=stmt.executeUpdate();
• System.out.println(i+" records inserted");

• con.close();
Example of PreparedStatement interface
that updates the record
• PreparedStatement stmt=con.prepareStateme
nt("update emp set name=? where id=?");
• stmt.setString(1,"Sonoo");//
1 specifies the first parameter in the query i.e.
name
• stmt.setInt(2,101);
• int i=stmt.executeUpdate();
• System.out.println(i+" records updated");
Example of PreparedStatement interface that deletes the record

• PreparedStatement stmt=con.prepareStateme
nt("delete from emp where id=?");
• stmt.setInt(1,101);

• int i=stmt.executeUpdate();
• System.out.println(i+" records deleted");
Fetch Data
• Example of PreparedStatement interface that
retrieve the records of a table
• PreparedStatement stmt=con.prepareStatemen
t("select * from emp");
• ResultSet rs=stmt.executeQuery();
• while(rs.next()){
• System.out.println(rs.getInt(1)+" "+rs.getString(
2));
• }
Types of Statements
Callable Statement
• CallableStatement cstmt = null;
• try
• { String SQL = "{call getEmpName (?, ?)}";
cstmt = conn.prepareCall (SQL);
• .}
JDBC - Result Sets

• The SQL statements that read data from a database


query, return the data in a result set.
• The SELECT statement is the standard way to select
rows from a database and view them in a result
set. The java.sql.ResultSet interface represents the
result set of a database query.
• A ResultSet object maintains a cursor that points to
the current row in the result set. The term "result
set" refers to the row and column data contained
in a ResultSet object.
• The methods of the ResultSet interface can be
broken down into three categories −
• Navigational methods − Used to move the cursor
around.
• Get methods − Used to view the data in the
columns of the current row being pointed by the
cursor.
• Update methods − Used to update the data in the
columns of the current row. The updates can then
be updated in the underlying database as well.
• The cursor is movable based on the properties of the
ResultSet. These properties are designated when the
corresponding Statement that generates the ResultSet is
created.
• JDBC provides the following connection methods to create
statements with desired ResultSet −
• createStatement(int RSType, int RSConcurrency);
• prepareStatement(String SQL, int RSType, int RSConcurrency);
• prepareCall(String sql, int RSType, int RSConcurrency);
• Statement stmt =
conn.createStatement(ResultSet.TYPE_SCROLL
_INSENSITIVE
, ResultSet.CONCUR_READ_ONLY);
JDBC - Data Types
JDBC - Transactions

• If your JDBC Connection is in auto-


commit mode, which it is by default, then
every SQL statement is committed to the
database upon its completion.
• For example, if you have a Connection object
named conn, code the following to turn off
auto-commit −
• conn.setAutoCommit(false);
Commit & Rollback

• 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( );

You might also like