Spring jdbc is a module, used for building database applications aspart of the Spring framework. There are lot of advantages of building the applications using spring jdbc over java jdbc api.
Problems with java jdbc API
---------------------------
1. java jdbc is an api, apis will not provide boiler-plate logic in building the applications. The developer has to write lot of code repeatitively in perform database operations through the application like
1.1 creating the connection
1.2 statement
1.2 extracting the resultset ...etc
so we endup in writing huge amount of code while working with java jdbc, due to which we run into maintainability problems.
2. managing the resources are very difficult.
Each resource should be closed in a specific order as ResultSet, Statement, Connection, and individual resource should be kept intheir own try/catch block while closing
try {
// load the driver
}catch(SqlException | ClassNotFoundException e) {
}finally {
try {
if(rs != null) {
rs.close();
}
}catch(SqlException e) {}
try {
if(stmt != null) {
stmt.close();
}
}catch(SqlException e) {}
try {
if(con != null) {
con.close();
}
}catch(SqlException e) {}
}
3. While working with java jdbc api, we endup in wrapping the code within annoying try/catch blocks that makes the code difficult to read and maintain.
What is an exception?
exception is an object wrapped with failure information that has been caused during the execution of the program due to which the program has been terminated. by looking at the exception information, the programmer can easily understand the root cause of the failure and can make necessary changes in the application logic, so that the program becomes robust in execution and can improve the quality of code/application.
before java, languages like c, cobol, pascal or c++ there is no concept of exceptions, if a program ran into an error during execution it would terminate abnormally leaving not information regarding that failure.
Java language has introduced the concept of Exceptions, so that any java program during the execution has run into an error, the JVM captures the error information and wraps into an exception object and throws it, so that the program got a chance to look at that error information interms of exception.
exception = reporting the failures
exception handling = managing the failures
- Exception Handling is a mechanism through which we can recover a program out of failure by providing alternate path of execution interms of writing try/catch block.
try {
// original code
}catch(Exception e) {
// alternate path of code
}
// other lines of code
- For all the Exceptions that are reported by the Spring jdbc api, the base exception class is DataAccessException which is an UnChecked exception, so we dont need to write ourcode while working with spring jdbc api classes around try/catch block
- There are several other exception classes that are provided by spring jdbc api representing different type of failures, but all of them are sub-classes of DataAccessException only
UniqueConstraintViolationException
IntegrityConstraintVilationException
etc
- While working with spring jdbc, it has close integration with spring transaction module, which takes care of managing the transactionality irrespective of global/local, so it is easy to manage transactions within our application if we are using spring jdbc.
Comments
Post a Comment