Tuesday, 17 January 2017

MORE ON STATEMENT

Disadvantage of Statement

1. Not efficient because SQL statement is compiled every-time before execution.
2. SQL injection problem.
3. SQL statement is static (SQL statement is send to database with values for compilation).
4. It is used in order to execute SQL statement only once.
5. Statement does not allows parameters.


How to read data from database using Statement

Statement provide the following method to send query(select) statement.
1. ResultSet executeQuery(String sql) - executes the given SQL statement, which returns a single ResultSet object.


ResultSet

  • It is an interface.
  • This interface is implemented by JDBC driver developer.
  • A table of data representing a database ResultSet, which is usually generated by executing a statement that queries the database.
  • ResultSet contains result of executed query.
  • A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row.
  • Syntax - ResultSet rs = st.executeQuery("select * from emp");
ResultSet provides navigation methods in-order to move cursor from one row to another. These are -

1. boolean first() - Moves the cursor to the first row in this ResultSet object.
2. boolean next() - Moves the cursor forward one row from its current position.
3. boolean previous() - Moves the cursor to the previous row in this ResultSet object.
4. boolean last() - Moves the cursor to the last row in the ResultSet object.
5. boolean relative(int row) - Moves the cursor a relative number of rows, either positive or negative.
6. boolean absolute(int row) - Moves the cursor to the given row number in this ResultSet object.
  • A default ResultSet object is not update-able and has a cursor that moves forward only. Thus we can iterate through it only once and only from the first row to the last row.
  • ResultSet provide getter methods to read data from ResultSet object.
For each datatype ResultSet provide one getter method -
  • int getInt(int columnIndex)
  • int getInt(int columnname)
  • String getString(int columnIndex)
  • String getString(String columnName)

No comments:

Post a Comment