Posts

Complete date with date format Using Hibernate

// @Temporal(TemporalType.DATE)—comment temporal date in pojo to get both date and time     @Column (name= "HOW_TO_RP_LAST_UPDATE_DATE" , length=7)     public Date getHowToRpLastUpdateDate() {         return this . howToRpLastUpdateDate ;     } @Temporal(TemporalType.DATE) /*add temporal date in pojo to get only date*/     @Column (name= "HOW_TO_RP_LAST_UPDATE_DATE" , length=7)     public Date getHowToRpLastUpdateDate() {         return this . howToRpLastUpdateDate ;     } /*Alternatively you can also have@ Temporal*/ @Temporal (TemporalType. TIMESTAMP )       @Column (name= "ACTUAL_DATE" , nullable= false , length=7)       public Date getActualDate() {             return actualDate ;     ...

Spring MVC- Redirect from one controller to another

In spring mvc its very easy to redirect from one controller to another controller Example : @Controller @RequestMapping("/springcontroller1") public class SpringController1{ } @Controller public class SpringController2{ //thinking of redirecting on clicking on a method @RequestMapping(value = "/SpringController2cancel.abc", method = RequestMethod.POST, params = "action=cancel")     public ModelAndView cancel(@ModelAttribute("MyForm") MyForm myForm, BindingResult errors,SessionStatus sessionStatus,HttpServletRequest request) throws MyException {         ModelAndView modelAndView = new ModelAndView();                 sessionStatus.setComplete();         //redirecting to controller1         return new ModelAndView("redirect:/springcontroller1.abc");     } }

spring jdbc template rowmapper example

    @SuppressWarnings("unchecked")     public ArrayList getSearchResultByColumn_isAdmin(ABCResultForm  form, String roleId) {                 String query = StringConstants.ABC_RESULT_SEARCH_BY_COLUMN;         System.out.println("Comes here getSearchResultByColumn");         int count = 0;                        query = query + " ORDER BY PROCESSING_DATE_START desc ";                 return (ArrayList) getJdbcTemplate().query(query,new Object[] {},new RowMapper() {                             public Object mapRow(ResultSet rs, int rowNum) throws SQLException {     ...

convert string to BigDecimal

public BigDecimal convertStringtoBigDecimal(String a){ return new BigDecimal(a); } //can do a null check--before invoking this to avoid null pointer exception

power(base,n) recursively in java

Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared). powerN(3, 1) → 3 powerN(3, 2) → 9 powerN(3, 3) → 27 ------------------========================================== public int powerN(int base, int n) { int result=base; if(n==0) return 1; if(n > 0){ return result * (powerN(base,n-1)); } return result; }

fibonocci number in java

The fibonacci sequence is a famous bit of mathematics, and it happens to have a recursive definition. The first two values in the sequence are 0 and 1 (essentially 2 base cases). Each subsequent value is the sum of the previous two values, so the whole sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on. Define a recursive fibonacci(n) method that returns the nth fibonacci number, with n=0 representing the start of the sequence. fibonacci(0) → 0 fibonacci(1) → 1 fibonacci(2) → 1 public int fibonacci(int n) { if(n==0) return 0; else if (n == 1) return 1; else if (n == 2) return 1; else return fibonacci(n-1) + fibonacci(n-2); }

BunnyEars--Java Number sequencing problem

We have bunnies standing in a line, numbered 1, 2, ... The odd bunnies (1, 3, ..) have the normal 2 ears. The even bunnies (2, 4, ..) we'll say have 3 ears, because they each have a raised foot. Recursively return the number of "ears" in the bunny line 1, 2, ... n (without loops or multiplication). bunnyEars2(0) → 0 bunnyEars2(1) → 2 bunnyEars2(2) → 5 ---------------------------------------- public int bunnyEars2(int bunnies) { if(bunnies==0) return 0; else if(bunnies%2==0) return 3 + bunnyEars2(bunnies-1); else return 2+bunnyEars2(bunnies-1); }