Statement Interface in JDBC | Java JDBC Tutorials
This Java JDBC Tutorials “Statement Interface in JDBC” post is providing clearly about Statement interface functionality and their use in JDBC application development.
- To execute a Query in the database, we need to use Statement interface methods.
- We can get the Object of Statement interface as follows :
Statement obj = <Connection_interface_object>.createStatement();
- The Statement interface is used to execute a static query.
- The statement interface has several methods for execute the SQL statements and also get the appropriate result as per the query sent to the database.
Some of the most common methods of Statement interface:
- void close() : This method frees an object of type Statement from database and other JDBC resources.
- boolean execute(String s) : This method executes the SQL statement specified by s. Used to execute DDL(Data Definition Language) queries such as create, drop, alter, truncate.
- ResultSet executeQuery(String s) : This method is used to execute the SQL statement specified by s and returns the object of type ResultSet. Used to execute DRL(Data Retrieve language) queries such as “select”.
- int getMaxRows() : This method returns the maximum number of rows those are generated by the executeQuery() method.
- int executeUpdate(String s) : This method executes the SQL statement specified by s. The SQL statement may be a SQL insert, update and delete statement.
- int executeBatch() : Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.
- boolean execute(String SQL) : Returns a boolean value of false if a query will be executed; otherwise, it returns true. Use this method to execute SQL DDL statements or when you need to use truly dynamic SQL(complete sql statement as input in the form of String).