Connection interface :
- A Connection is a session between a Java application and a database.
- Connection interface methods as follows
public Statement createStatement() | creates a statement object that can be used to execute SQL queries. |
public void setAutoCommit(boolean status) | is used to set the commit status. By default, it is true. |
public void commit() | saves the changes made since the previous commit/rollback is permanent. |
public void rollback() | Drops all changes made since the previous commit/rollback. |
public void close() | closes the connection and Releases a JDBC resources immediately. |
Statement interface:
- The Statement interface provides methods to execute queries with the database.
- Commonly used methods of Statement interface:
The important methods of Statement interface are as follows:
public ResultSet executeQuery(String sql) | is used to execute SELECT query. It returns the object of ResultSet. |
public int executeUpdate(String sql) | is used to execute specified query, it may be create, drop, insert, update, delete etc. |
public boolean execute(String sql) | is used to execute queries that may return multiple results. |
public int[] executeBatch() | is used to execute batch of commands. |
executeUpdate() | executeQuery() | execute() |
This statement is used to execute SQL statements which update or modify database | This method is use to execute the SQL statements which retrieve some data from database. | This method can be used for any kind of SQL statements. |
This method returns an int value which represents number of rows affected by the query. | This method returns a ResultSet object which contains the result returned by query. | This method returns a Boolean value. |
This method is use to execute non select query. | This method is use to execute select query. | This method is use to execute select and non-select queries. |
DML->INSERT, UPDATE and DELETE | Example: SELECT | Any Type of SQL statements. |
Create Statement object:
- getStatement() method of Connection returns Statement object.
- Statement interface has functionality to execute different kinds of SQL statements.
- Statement stmt = con.createStatement();