Statement interface in java

Statement interface

 Used to create statement object & handle the static dynamic queries

Statement interface methods

close()
 It allows us to explicitly close the statement object
 This actually releases all the resources used on database server & JDBC driver statement.close()

execute()
 It executes parsed SQL statement against database with the help of JDBC drivers
 It is useful when SQL statement returns multiple result sets. This occurs generally in stored procedure
 The execute() method will return true if the result is ResultSet object & false if result is integer result

executeUpdate()
 Enables us to execute SQL update statement like delete, insert & update
 It returns an integer which determine how many records are affected

executeQuery()
 Enables us to send “select” statement to the database & receive result from database
 It returns a ResultSet object
 Method next() moves ResultSet object to next record
 Initially the record pointer is placed on a blank record preceded to first
 As long as there are records, this method returns true else it returns false
 getUpdateCount(): returns no. of records which are updated

Example
import java.sql.*;
class test
{
public static void main(String args[])throws Exception
{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”system”);
Statement stmt=con.createStatement();
int result=stmt.executeUpdate(“delete from tab where id=1″);
System.out.println(result);
con.close();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *