Friday, April 19, 2013

Why do we need java frameworks?


What is core java and advanced java?
Before we start talking about java framework, we should know about core and advance java.
Core java comes with java 'Standard Edition' and it has to do with the basic package of Java objects that are typically used for general desktop applications.
Where as advance java is specialisation in some domain, such as networking, web, DCOM or database handling.


What is Frameworks?
Framework is set of reusable software program that forms the basis for an application. Frameworks helps the programmers to build the application quickly. Earlier it was very hard to develop complex web applications. Now it’s very easy to develop such application using different kinds of frameworks such as Struts, Struts 2, Hibernate,  JSF, Tapestry, JUnit, Log4j, Spring etc.
http://www.roseindia.net/frameworks/

Example of Java frameworks
MVC frameworks : Spring
ORM frameworks : Hibernate, Oracle TopLink, iBATIS
Presentation layer frameworks : Struts, JSF, Apache Wicket

Example of Framework usage
I will show how JDBC that inserts data into the database and how simply do it using java frameworks(Spring & Hibernate).

Consider there is a Customer table in your database.

CREATE TABLE `customer` (                              
             `CUSTOMER_ID` BIGINT(20) NOT NULL AUTO_INCREMENT,    
             `NAME` VARCHAR(45) NOT NULL,                          
              PRIMARY KEY (`CUSTOMER_ID`),                          
              UNIQUE KEY `CUSTOMER_ID` (`CUSTOMER_ID`)              
              )

JDBC Insert customer method using PreparedStatement

private void insertCustomerIntoTable(Customer customer) throws SQLException {
  Connection dbConnection = null;
  PreparedStatement preparedStatement = null;
  String insertTableSQL = "INSERT INTO CUSTOMER ( CUSTOMER_ID, NAME) VALUES (?,?)";
  try {
   dbConnection = getDBConnection();
   preparedStatement = dbConnection.prepareStatement(insertTableSQL);
   preparedStatement.setInt(1, customer.getCustomerId());
   preparedStatement.setString(2, customer.getName());
   preparedStatement.executeUpdate();

   } catch (SQLException e) {
    System.out.println(e.getMessage());
   } finally {
    if (preparedStatement != null) {
    preparedStatement.close();
   }
    if (dbConnection != null) {
    dbConnection.close();
   }
  }
  }

We have to code this much no of line to achieve our goal.(I do not show the database connection method and Customer class with getters and setters )

Now when we configure this project with spring mvc and hibernate frameworks code is simplify like this

@Transactional
private void insertCustomerIntoTable(Customer customer){
      session.save(customer);
}

That’s it. That’s what all we need to do. @Transactional annotation will handle the transactional part in database access and hibernate session’s save method will insert customer data into table.If anything goes  wrong and Exception throws,this method will rollback transaction.
(to achieve this we need to do some configurations like Datasource, HibernateSessionFactory, TransactionManager spring beans)

There are so many example like this,when you develop application using java frameworks.Main advantage is we can concentrate our application business logic rather than handling data access common boilerplate code.
Other than this some of advantage of frameworks are
  • Hiding complexity
  • Increase performance
  • Modularity
  • Flexibility, Maintainability, and Extensibility