Friday, 1 November 2024

How to retrieve data from the database into a JDBC Client?


Advanced Java: 06

 Q. How to retrieve data from the database into a JDBC client?

By Submitting "SELECT SQL" Statement we can retrieve data from the database. executeQuery() method of statement object is used to submit select statement to the DBMS.

  • executeQuery() method takes SELECT SQL statement as a string argument.
  • It returns Resultset object.
Q. Explain about executeQuery() Method?
  • executeQuery() method is JDBC API Method.
  • used to send SELECT Statement.
  • It belongs to statement object.
  • It's return type is ResultSet.
  • It throws SQLException if anything goes wrong.
Q. Explain about ResultSet()?
  • Object oriented representation of database records received from the DBMS (Database server) is nothing but Resultset Object.
  • ResultSet Object is logically divided into 3 Partitions.
  1. Zero Record Area.
  2. Record Area.
  3. No Record Area.
  • How resultset object internally look like?
  • When ResultSet is open, a logical pointer known as Cursor points to zero record area of the resultset.
  • When the cursor is pointing to ZERO Record Area or NO RECORD AREA, we should not try to read the data from the resultset. otherwise SQLException is raised.
  • When the cursor is pointing to Record holding area then only you have to read the data. so how to move the cursor to record holding area as a programmer?
  • java.sql.resultset has a method. "Next ()" to move the cursor.
  • This method does two things: 1. Moves the cursor in forward direction by one record area. 2. After moving cursor, it returns true if record is present. It returns false if record is not there.
  • ResultSet Object has a getter method to read data from the resultset record. these method takes column number of the resultset record as arguments and return the value.




After Execution of above code output will look like as follows.


Note:
getter methods are overloaded method you can give column names also or column number.

data structures and algorithms Web Developer

No comments:

Post a Comment