Tuesday, January 25, 2022

Fetching the data from Database Server in JDBC

 Fetching the data from Database Server in JDBC

  • Whenever we execute DQL Query, we get a result which is referred to as Resulatant or Processed Data.
  •  The Processed or Resultant Data can be fetched by ResultSet Interface which is a part of JDBC API.
  •   java.sql.ResultSet
  • It is an interface which is a part of JDBC API and the implementations are provided by respective Database Server or vendors as a part of JDBC Driver.
  •  A set of methods of ResultSet interface are used to fetch the Processes or Resultant data which are known as getXXX().
  • There are two different methods which are overloaded methods present in a ResultSet Interface:-

   +XXX getXXX(int coloumnnumber/coloumnindex )  // ( coloumn number ---- coloumn index )

 

   +XXX getXXX(String coloumnname/coloumnlabel ) // ( coloumn name ----- coloumn label )


 If datatype is integer:-

  

  a) +int getInt(int coloumnnumber)

 

  b) +int getInt(String coloumnname)


 If datatype is String:-


  a) +String getString(int coloumnnumber)

 

  b) +String getString(String coloumnname)


 If datatype is Double:-


  a) +double getDouble(int coloumnnumber)

 

  b) +double getDouble(String coloumnname)


  •  The return type of getXXX() method is the respective data-type.

  • The return type of setXXX() method is void.



 // hasNext() --- next()  // Collection


 // next() --- getXXX()  // JDBC


 next():-


  •  It is used to check whether the next record is present or not and returns a boolean value called true or false but not the record.


   + boolean next()


executeQuery():-

  1.  It is a specialized method which is used to execute only DQL Queries.

  2.  The outcome of a DQL is Processed or Resultant Data which can be fetched by ResultSet Interface which is a part of JDBC API.

  3.  Hence, the Return type for executeQuery() is ResultSet interface.

  4.  whenever we try to execute a DML query using this method it throws an exception called SQLException.

  Syntax:-


          +ResultSet executeQuery("Only DQL")

 

Implementation Object:-


  

        ResultSet rs = stmt.executeQuery(" DQL "); 


insert into thane.student values(1,'Oats',45.3); // Hard-coding

Oats -- Wheat


PlaceHolder:-

  • It is a parameter which holds Dynamic values at the run time by the User.

  •  In case of JDBC, place holder is represented by as " ? ".


Declaration of PlaceHolder in the Query:-


String inQry=" insert into thane.student values(?,?,?) ; // 1 - id  2 - name  3 - perc


String inQry1=" insert into thane.student values(?,?,?) ;


String upQry=" update thane.student set Product_Name = ? where id = ? ; //2 placeholders -- 2 times


String delQry=" delete from thane.student where id = ? ;


String selQry=" select * from thane.student where id = ? ;


Rules to set the data for a PlaceHolder:-


There are 3 different rules to set the data for PlaceHolder namely:-


  1.  We have to set the data for a Placeholder before the execution.

  2. The number of data must exactly match with the number of Placeholder.

  3. We have to set the data for a Placeholder by using setXXX().


  +void setXXX(int placeholdernumber/placeholderindex, XXX data)


Ex:-

      sid=sc.nextInt();

     

        setInt(1, 15); // setInt(1, sid);


        setString(2, "Steve"); // setString(2, sc.next());


        setDouble(3, 34.5); // setDouble(3, sc.nextDouble());

 


0 comments:

If you have any doubts,please let me know