Tuesday, June 20, 2017

Difference between excuteQuery(), excute(), exceteUpdate() in JDBC



excuteQuery(), excute(), exceteUpdate()::-


execute():- used to execute any SQL command

executeUpdate():- used to execute only non-select commans, like Insert,delete ,update

ececuteqQuery() :- used to execute select commands.

execteBatch():- used to executes set of non-select commands.

Call executeQuery() on JDBC statement obj to send and execute SQL select Query in DB s/w. This method returns result set Obj representing batch of records that are retrieved.

Call executeUpdate(-) on JDBC statement obj to send and execute SQL non-select query in DB s/w. This method returns numeric value representing no of records that are effected.


BatchProcessing/batchUpdation:-

          Inset of that keep all the related SQL Queries in a batch and send that batch to db s/w at once and execute that batch of SQL queries back to java apps as a batch. This is calle batch processing and it reduces s/w round trips b/w java applications and db s/w.

         We can place select SQL queries in batch of batch processing so it is called as a batch updation because all non-select SQL Queries are called as called as updateQuerys.

To add SQL Queries to batch use addBatch() and to execute batch of SQL queries use executeBatch(0 method.

Advantages of working with PreparedStatement || Java High Q



Advantages of working with PreparedStatement:
               



               a)      Allows to deal with precompiled SQL Query
               
               b)      Allows to place parameters  (?) in the SQL Query
               
               c)      Suitable to execute same query for multiple times either with same values or different  
                       values.
               
              d)     Allows to set values to query param in java style(no need of converting input values as  
                     required for SQL Queries).
              
             e)      Does not raise SQL Injection problem.
             
             f)       Suitable for inserting date values.
            
             g)      Suitable for inserting large obj.
           
            h)      Reduces the network traffic between java application and between java application  
                   and DataBase s/w and etc.

Standard steps for developing JDBC code || Java High Q



Standard steps for developing JDBC code:


         A)    Register JDBC driver s/w with DriverManager
         B)    Establish the connection with Database s/w
         C)    Create JDBC statement obj
         D)    Send and execute SQL query in db s/w
         E)     Get Result from db s/w and process the result
        F)     Close the connection with db s/w

           Example :-



               public static void main(String[] args)throws Exception {
  //register jdbc driver
               Class.forName("oracle.jdbc.driver.OracleDriver");
//Establish the connection Connection          
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
//create Statement obj

                 Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                                                                                                
                 ResultSet.CONCUR_UPDATABLE);
             
                       //create ResultSet obj
                 ResultSet rs=st.executeQuery("Select sno,sname,sadd from student");
                      //process the ResultSet
                             int cnt=0;
                           while(rs.next()){
                        if(cnt==0)
                     Thread.sleep(20000);
                         rs.refreshRow();
                     System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
                           cnt++;
                        }

             }








Friday, June 9, 2017

Spring MVC workflow Overview Spring MVC Execution Flow Diagram Java High Q


Spring MVC workflow overview














1)The client requests for a Resource in the web application.


2) The sprint front controller i.e DistacherServlet makes a request to HandlerMapping to identify the particular controller for the given url.


3)HandlerMapping identifies the controller for the given request and sends to the DispatcherServlet.

4)DispatcherServlet will call the handlerRequest (req, res) method on Controller. Controller is developer by writing a simple java class which implements controller interface or extends any controller class.


5)Controller will call the business method according to business requirement.

6)Service class calls the DAO Class method for the business data.


7)DAO interact with the Database to get the database data.

8)Database gives the result.


9)DAO returns the same data to service.

10)DAO given data will be processed according to the business requirements, and returns the results to Controller.


11)The Controller returns the Model and the View in the form of ModelAndView object back to Front Controller.

12)The Front Controller i.e DispatcherServlet then tries to resolve the actual View(which may be JSP  Velocity or Free marker) by consulting the ViewResolver object.


14)ViewReslover selected View is rendered back to the DispatcherServlet.

15)DispatcherServlet consult the particular View with the Model.


16)View executes and returns HML output to the DispatcherServlet.

17)DistpatcherServlet sends the output to the Browser.