PreparedStatement interface
The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized query.
Let's see the example of parameterized query:
As you can see, we are passing parameter (?) for the values. Its value will be set by calling the setter methods of PreparedStatement.
Why use PreparedStatement?
Improves performance: The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once.
How to get the instance of PreparedStatement?
The prepareStatement() method of Connection interface is used to return the object of PreparedStatement. Syntax:
Methods of PreparedStatement interface
The important methods of PreparedStatement interface are given below:
Method | Description |
---|---|
public void setInt(int paramIndex, int value) | sets the integer value to the given parameter index. |
public void setString(int paramIndex, String value) | sets the String value to the given parameter index. |
public void setFloat(int paramIndex, float value) | sets the float value to the given parameter index. |
public void setDouble(int paramIndex, double value) | sets the double value to the given 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. |
Example of PreparedStatement interface that inserts the record
First of all create table as given below:
Now insert records in this table by the code given below:
Example of PreparedStatement interface that updates the record
Example of PreparedStatement interface that deletes the record
Example of PreparedStatement interface that retrieve the records of a table
Example of PreparedStatement to insert records until user press n
Java ResultSetMetaData Interface
The metadata means data about data i.e. we can get further information from the data.
If you have to get metadata of a table like total number of column, column name, column type etc. , ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object.
Commonly used methods of ResultSetMetaData interface
Method | Description |
---|---|
public int getColumnCount()throws SQLException | it returns the total number of columns in the ResultSet object. |
public String getColumnName(int index)throws SQLException | it returns the column name of the specified column index. |
public String getColumnTypeName(int index)throws SQLException | it returns the column type name for the specified index. |
public String getTableName(int index)throws SQLException | it returns the table name for the specified column index. |
How to get the object of ResultSetMetaData:
The getMetaData() method of ResultSet interface returns the object of ResultSetMetaData. Syntax: |
Example of ResultSetMetaData interface :
Output:Total columns: 2 Column Name of 1st column: ID Column Type Name of 1st column: NUMBER
0 comments:
Post a Comment