August 17 2016

Java calendar last day of month

Java calendar last day of month

Following are the different ways to get last day of the month

  • Set the Calendar object to be in the month for which you need the last date, getActualMaximum(Calendar.DAY_OF_MONTH) will give you the last date of that month.
  • Setting the calendar to the first of next month and subtract a day.

calendar.get(Calendar.DAY_OF_WEEK) returns int values from 1 to 7(1-Sunday and 7-Saturday)

Example Continue reading

August 3 2016

Spring NamedParameterJdbcTemplate example

Spring NamedParameterJdbcTemplate example

Spring NamedParameterJdbcTemplate is template like JdbcTemplate with set of methods supporting JDBC operations. NamedParameterJdbcTemplate supports named parameters. A positional parameter (?) is set by its index in the clause. A named parameter (:paramName) is set by its name.

NamedParameterJdbcTemplate allows us to represent and pass the sql parameters with parameter names rather than the positional parameter (‘?’).

In the below example, EMPLOYEE_ID parameter is set by name ‘ :empId’(colon followed by a parameter name). Continue reading

Category: Spring | LEAVE A COMMENT
August 1 2016

Spring JdbcTemplate stored procedure example

Spring JdbcTemplate stored procedure example

Spring JdbcTemplate provides support for calling stored procedures. The method ‘call’ is added in JdbcTemplate to execute the stored procedures.

public Map call(CallableStatementCreator csc,List declaredParameters) throws DataAccessException

The method takes two parameters

  1. CallableStatementCreator csc

First parameter of the method is CallableStatementCreator interface which is used by JdbcTemplate to get CallableStatement. Implementation class of the interface has to implement the ‘createCallableStatement’ method. Continue reading

Category: Spring | LEAVE A COMMENT
July 28 2016

Spring RowMapper Example

Spring RowMapper Example

The RowMapper is interface used by JdbcTemplate for mapping each row of data in the ResultSet.

public interface RowMapper<T>

Implementation class of the interface has to implement the ‘mapRow’ method and map each row of ResultSet to required Object. The ‘mapRow’ method returns an Object for the current row. SQLExceptions will be caught and handled by the calling JdbcTemplate.

Object mapRow(ResultSet rs,int rowNum) throws SQLException

In the below example, mapRow() method is implemented to map each row of ResultSet to Employee Object.

Example Continue reading

Category: Spring | LEAVE A COMMENT
July 26 2016

Spring JdbcTemplate batchupdate example

Spring JdbcTemplate batchupdate example

Batch update option allows us to submit multiple queries to database at once. Spring JdbcTemplate supports batch updates through JDBC Statement and PreparedStatement. The Spring JdbcTemplate provides following overloaded methods for batch updates

int[] batchUpdate(String[] sql) throws DataAccessException

Executes multiple SQL queries on a single JDBC Statement

int[] batchUpdate(String sql, BatchPreparedStatementSetter pss) throws DataAccessException

Executes multiple SQL queries on a single PreparedStatement

The batch update support is introduced in JDBC 2.0.If your driver does not support batch updates then the above methods executes the queries as separate updates on single JDBC Statement and PreparedStatement.

Example Continue reading

Category: Spring | LEAVE A COMMENT
July 15 2016

Spring AOP AspectJ annotation example

Spring AOP AspectJ annotation example

Spring AspectJ provides annotation support for Spring AOP. AspectJ automatically generates proxy for the bean to intercept method invocations as configured.

How to enable @AspectJ support?

If you are using xml configuration file include <aop:aspectj-autoproxy/>

<aop:aspectj-autoproxy/>

If you are using configuration class add @EnableAspectJAutoProxy annotation Continue reading

Category: Spring | LEAVE A COMMENT