The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database.
Syntax of createStatement() method
public Statement createStatement() throws SQLException
Example to create the statement object
Statement stmt=con.createStatement();
4) Execute the query
The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table.
Syntax of executeQuery() method
public ResultSet executeQuery(String sql) throws SQLException
Example to execute query
ResultSet rs=stmt.executeQuery(“select * from emp”);
By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection.