Ibm Offer Letter 2021
Ibm Offer Letter 2021
Ibm Offer Letter 2021
Spring JDBC
How many types of application are there in JAVA?
By looking at the applications and the way we build the applications & by looking at the nature
of the application that we are build these applications are classified into in three different
groups.
1) Standalone Application or Desktop based applications.
The application is running in which it is installed (same System). It consumes the memory of
the same systems & only one user will access that application.
2) Distributed or Thin browser based applications.
This application is again classified into multiple types.
i) Intra-Network Applications.
It will used by the employee of a particular organization. Outer world people
will not access it.
ii) Internet Applications.
Spring JDBC
Spring JDBC provided abstraction layer on java JDBC and simplifies persistency logic
development.
In Bigger application instead of using java JDBC it is recommended to use Spring JDBC.
Then the spring JDBC is catches the exceptions and wraps the exceptions into spring JDBC
Exceptions which are unchecked exception, SO we don’t need to bother about to catch those
exception whether we have alternate path or not.
Now we are free from writing the try and catch block. Which is benefits going for spring
JDBC rather go for java JDBC.
All the Exception that are in the java JDBC are checked Exception .That is the problem in
the java JDBC. The entire code is written in the java JDBC is inside the try & catch block which
is annoying try and catch block.
Even if you don’t have anything to do after catching you don’t have any alternate path of
execution still you have to catches the exception.
So it’s a problem with java JDBC. Instead of it, if you go for spring JDBC all the Exception
that are present there inside the spring JDBC are unchecked exceptions. So if you really want
to handle the exception then only you need to write the try and catch block otherwise you will
completely free from writing the try and catch block.
Spring Exceptions
Spring JDBC comes with bunch of Exception classes.
To classify the problem in your application and to easily know the cause of the problem Spring
JDBC has provided bunch of exception classes which are called spring JDBC Exception classes.
For all spring JDBC Exception classes there is a parent classes called DataAccsesExceptions.
Any Exception that are there in the spring JDBC will extend from DataAccsesExceptions.
4th Difference: (Transaction)
In java JDBC programmer has to write the logic for transaction. Instead of it if you go for
spring JDBC the transaction management logic will be taken care by the spring JDBC itself.
During the start of the application 80 to 90% of the database design has to be completed.
And anything that has been still left will be related to the unwanted requirements only.
Some requirements are not real and while during the progress of the application developments
if those requirements are going to be clarifying then there will be a change in the database
design, otherwise during the developments the database tables will not changed. Once the
database model is finalized or completed then only developments of the application will
happened.
Once the database model has been completed the design tables that we have come of with
will be converted into SQL scripts. And these SQL script will be written in a file and it will be
distributed with the whole team. The developments of the application will be happens using
those tables only, the tables will not be created by the applications.
We should add the column by writing alter scripts sending this query to database
administrator. Ask the administrator to add the column. But it is wrong.what do you mean by it ?
So we have already an application has been developed which is running in the production
environment or has been given to testing environment any of cases you can consider. Now my
application has been developed in development environment. And the application has been tested
in a QA environment. My development environment has database & my test environment also will
have database. Now when I moved from development to QA I need to have tables being created
in QA environment otherwise the code which we have written will not work without the tables.
Now what do I need to do if I want to add a extra column in to the database table ? Its
simple execute an alter query here in the database table then that code that is there in test
environment will not consider this table . I write the logic for the persist the column into the
data base table. Now the version my code is 1.2 . Now finally adding the column and modifying
the code in my application , my application logic is now given to QA team for testing . But my
code will not work. Because the code we have deployed here is having extra additional column
which is not there in the QA database . So I should not do it. There is a process will be there .
The changes that I have making in adding the column , the relivant change I have made in java
should push into all the environments of the same state. That coordination has to be doen.
Otherwise the whole application is broken in all the environment.
That’s why first alter query has to be written and will execute in the local environment and
create an SQL script file with that alter query . Now test yourself that your code is working or
not.
Now coming to your code into repository and then take the version no (1.2) now with this you
send a email to database administrator, project manager and the lead saying this is an additional
column that I have added into this corresponding table as per the enhancement requirement .
And for this additional column that I have added into the table the corresponding code changes
are been pushed into the svm repository and the version no of the code changes is 1.2 .
If you are deploying the code with version 1.1 then don’t take this alter query but if you are
taking the higher side of the code 1.2 and deploying it into the QA environment along with that
execute the alter query , now the database will have the column here then only deploy the code .
This we have to email to the database administrator, lead, and project manager and to the
deplorer. So the developer has to document this whole process to all the environment.
When it comes to JDBC template there are two ways of working with JDBC template.
Classic approach
It is the old style which we come across. It is similar to java JDBC.
For example:In java JDBC programmer has to do everything like create the connection, create
the statement, execute the query , get the result set . SO it is resembles like java JDBC
approach when we work with classic approach. Even though API changes but the mechanism or
working is same.
While working with any approach of Spring JDBC we need to use data source (Connection
pooling) to interact with database software.
So now I have to give this code to the JdbcTemplate so that jdbc template takes care of
carrying this code by passing the connection as input to carrying the operation. SO until the
code completes the execution I need the connection.
So am not calling my code or methods to execute the operation. Jdbc template is calling my
code by passing the required set of inputs as parameter to our methods. So if jdbc template
has to call my method on my class I need to give the object of my class to jdbc template .
So I have to create the object of my class and give the object to JdbcTemplate by saying I
have a method can you please call the method by passing the required set of input that’s why my
code will get executed. As am not calling my method and my code is handover to someone to
calling the method that’s the reason it is call CALLBACK MECHANISM.
Classic Approach Example-1(Dynamic Query)
Internals
Most of time our logic depends upon callback mechanism. Whatever we wanted to do in classic
approach will be divided in to 2 phases of execution.
First phase of execution is called preparing phase (Creator phase). That what you need to
execute your code write all this code under preparing phase
Second phase of execution is called callback phase(Callback phase). The execution of the code
will happens in callback phase. Or collecting the results.
Every piece of code that you are writing under Spring Jdbc classic approach will falls under 2
phase
Like java JDBC in spring JDBC there are 2 statements to execute the query.
1) Statement
2) Prepared Statement
Now there are 2 phases where our code is divided.
i) Creator phase
ii) Callback phase
In creator phase we need to create the prepared statement and in callback phase we need to
execute the prepared statement and get the results.
Now to create the statement we need connection. But we don’t want to create the connection
because if we create the connection we need to manage it.
If I don’t create the connection then jdbc template has to provide the connection.How jdbc
template provide the connection ?
To get the connection from the jdbc template we need to write a class and inside the class we
need to write a method by passing connection as parameter to that method.
Jdbc template has to call that method . But how does the jdbc template know your class has
that method to call by passing connection as parameter?
That’s why every class that we wanted to give to the jdbc template to get it executed should
implements from standard interfaces that are provided by jdbc template . So we need to write
2 pices of logic. For creator phase and callback phase.
queryForMap()
select * from project where project_no=1
This is used to execute query that gives single record as result.
There are three over loaded Methods.
queryForList()
select project_No,Title from project where project_no=1
This is used to execute query that gives multiple records.
public List<Map<String, Object>> queryForList(String sql)
Non-Select Operations
update()
batchUpdate()
Query Base Approach Example-1
queryforList
To get complete Row of a Project
Output
To get one Column (queryforObject)
SqlParameterSource
It uses database table metadata concepts are used to insert record dynamically without
insert SQL Query . It just take table name, column name, column values from the programmers
and uses them to generate dynamic SQL> insert query to insert record.
We can give these column names and values either as Map object or as SQLParameterSource
implementation class Object.
The Map Object contains column name as keys and column values as values.
To create SimpleJdbcInsert object DataSource object is dependant object.
This class makes as database Independent. We can switch one database to another database
easily.
Mapping SQL operations as sub classes
This is call technique in spring jdbc to perform persistence operation. This technique makes
programmer to develope sub classes where we set tha datasource, declaring parameters and
compile query only one time but we execute query multiple times by reusing the object of sub
class for multiple times.
This concept is similar to JDO (Java Data Object) programming. Spring JDBC supplies
SqlQuery,SqlUpdate classes to develop these sub classes.
Use SqlQuery for select operation and use SqlUpdate for non-select operations.
SqlQuery
We develop separator subclass extending from SqlQuery for select query inside DAO class.
This subclass supplies DataSource query to super class to execute query for multiple times
by compiling the query only for one time.
SqlQuery supplies execute() method to execute query that gives the List of objects and also
give finder() method to execute query that gives only one Object(One Record).
This class also gives maprow(--,--) method which can be used to process each record of
ResultSet object in to another object.
The execute(),finder() method internally it calls mapRow() method.
Since SqlQuery is abstract class having more abstract methods we prefer using
‘MappingSqlQuery’ which is subclass in the inheritance hierarchy of SqlQuery.
While developing subclass for SqlQuery/MappingSqlQuery classes we use
declareParameter(-);
compile()
This technique say’s give everything to super class from subclass for query execution and
reuse subclass object.
MappingSqlQuery
SqlUpdate
It similar to SqlQuery class but given for non-select queries (update queries) execution.
We can prepare subclass to SqlUpdate class inside the DAO class where we supply
datasource,query to SqlUpdate class only once.
But we use these sub classes object for multiple times to execute same non select query with
the support of update() method to execute non-select query with same or different parameters
for multiple times.
For every non select query we need to take separate sub class for SqlUpdate inside the DAO
class.
SQlUpdate-1
SQlUpdate-2