Statement
Statement
PreparedStatement interface
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