Tag Archives: jdbc

Spring JDBCTemplate code example – view/download (Github)

In the below code example shows how the Spring JDBCTemplate simplifies the code to perform the database-related operations.

The complete examples running code is available to download from Github

DAO Interface:

package com.spring.jdbc;

import java.util.List;

/**
 *
 * @author codedairy
 */
public interface CustomerDAO {

    public Customer getCustomer(Integer customerId);
    public int createCustomer(Customer newCustomer);
    public List retrieveAllCustomers();

}

Implementation of our DAO class

package com.spring.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author codedairy
 */
public class CustomerDAOImpl implements CustomerDAO {

    private JdbcTemplate jdbcTemplate;

    private final String select = "SELECT * FROM CUSTOMER WHERE ID= ?";
    private final String insert = "INSERT INTO CUSTOMER( NAME, AGE) VALUES(?,?)";
    private final String retrieveall = "SELECT * FROM CUSTOMER";

    @Override
    public Customer getCustomer(Integer customerId) {

        Customer c = (Customer)getJdbcTemplate().queryForObject(select, new Object[]{customerId}, new RowMapper() {

            @Override
            public Customer mapRow(ResultSet rs, int i) throws SQLException {

                Customer cust = new Customer();
                cust.setId(new Integer(rs.getString("ID")));
                cust.setName(rs.getString("NAME"));
                cust.setAge(new Integer(rs.getString("AGE")));
                return cust;
            }
        });
        return c;
    }

    @Override
    public int createCustomer(Customer newCustomer) {
       return getJdbcTemplate().update(insert, new Object[]{newCustomer.getName(),newCustomer.getAge()});
    }

    @Override
    public List retrieveAllCustomers() {
        List<Map> list = getJdbcTemplate().queryForList(retrieveall);

        List custList = new ArrayList();
        for(Iterator itr = list.iterator(); itr.hasNext();){
            Map map = (Map) itr.next();

            Integer id = (Integer) map.get("ID");
            String name = (String)map.get("NAME");
            Integer age = (Integer)map.get("AGE");

            Customer cust = new Customer(id,name,age);

            custList.add(cust);

        }

        return custList;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

}

Our Main executable class is:


package com.spring.jdbc.test;

import com.spring.jdbc.Customer;
import com.spring.jdbc.CustomerDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author codedairy 
 */
public class CustomerExec {
    
    public static void main(String args[]){
        
        
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("customer_jdbc.xml");
        CustomerDAO customerDAO =  (CustomerDAO) applicationContext.getBean("customerdao");
        
        
        Customer customer = new Customer();    
        customer.setAge(20);
        customer.setName("Roger Chick");
        
        Customer customer1 = new Customer();    
        customer1.setAge(30);
        customer1.setName("Stephen Hardy");
        
         Customer customer2 = new Customer();    
        customer2.setAge(30);
        customer2.setName("Kuldip Bajwa");
        
        //CREATE CUSTOMER RECORD EXAMPLE
        System.out.println("Rows affected: " + customerDAO.createCustomer(customer));
        System.out.println("Rows affected: " + customerDAO.createCustomer(customer1));
        
        //RETRIEVE CUSTOMER RECORDS EXAMPLE
        System.out.println("CUSTOMER TABLE RECORDS: "+customerDAO.retrieveAllCustomers().toString());
       
        //QUERYING A PARTICULAR RECORD FROM CUSTOMER TABLE - For e.g., by Customer ID
        System.out.println("Record Found: \n" + customerDAO.getCustomer(3).toString());
        
        
        
    }
}



Achieving a more performing Web services middle tier

by Kuldip Bajwa

In this article we will discuss different ways and methods of improving performance in an enterprise Java based application specifically concentrating on the Web services Integration layer.

  Let’s briefly start by discussing the different methodologies of performing Remote Procedural Calls (RPC) from a JEE Application integration tier perspective, and then delve into performance pros and cons for each.

These include:

  1. Document and literal style Web services using JAX-RPC and JAX-WS
  2. Wire speed solutions for example DataPower devices
  3. Concurrency using Asynchronous communication and passing contextual information to spawned
  4. threads and Asynchronous beans.

  5. Web service interface best practices for performance gains

Introduction to SOAP and Web services

  SOAP was originally intended to be a cross-Internet form of DCOM or CORBA. The name of an early SOAPlike technology was WebBroker – Web-based object broker. It made perfect sense to model an inter-application
protocol on DCOM, CORBA, RMI etc. because they were the current models for solving inter-application interoperability problems.

  These technologies achieved only limited success before they were adapted for the Web. RPC models are great for closed-world problems. A closed world problem is one where you know all of the users, you can share a data model with them, and you can all communicate directly as to your needs.

  Scalability was comparatively easy in such an environment: you just tell everybody that the RPC API is going to change on such and such a date and perhaps you have some changeover period to avoid downtime. When you want to integrate a new system you do so by building a point-to-point integration.

  On the other hand, when your user base is too large to communicate coherently you need a different strategy.You need a pre-arranged framework that allows for evolution on both the client and server sides. You need to depend less on a shared, global understanding of the rights and responsibilities of a participant. You need to put in hooks where compliant clients and serves can innovate without contacting you. You need to leave in explicit mechanisms for interoperating with systems that do not have the same API. RPC protocols are usually poorly suited for this kind of evolution. Changing interfaces tends to be extremely difficult. Integrating services typically takes complicated software “glue” which is the motivating factor behind extending the capabilities of the first-generation Web services framework for the enterprise.

Second generation SOE

  The vast amount of second-generation Web service specifications that have emerged, position SOA as a viable successor to prior distributed platforms. Their feature sets continue to broaden, as do vendor-sponsored variations of the specifications themselves. The continuing maturity of theses standards and their implementations sets the stage for the viable evolution of a Service-Orientated-Enterprise (SOE). The generation Web service specifications are listed below which can be embedded and used as part of either JAXRPC or JAX-WS:

  1. WS-Coordination
  2. WS-Transaction
  3. Business Process Execution Language for Web services (BPEL4WS)
  4. WS-Reliable Messaging
  5. WS-Addressing
  6. WS-Policy
  7. WS-Policy Assertions
  8. WS-Policy Attachments
  9. WS-Attachments
  10. SOAP with Attachments (SwA)
  11. WS-Security Framework
  12. Extensible Access Control Mark-up Language (XACML)
  13. WS-Federation
  14. WS-Secure Conversation
  15. WS-Authorisation
More, to follow in next article !!!!