How to integrate JSF and Spring Framework


If you would like to integrate the JSF MVC framework with the Spring Framework, you can use the following approach;
There are also other configurations  available however this one fits very well while developing web applications using JSF and Spring.

In this example, I am using the basic .jars required by Spring Framework. These jars are;

spring-web.jar
spring-webmvc-2.5.jar
org.springframework.expression-3.1.0.RC1.jar
org.springframework.core-3.1.0.RC1.jar
org.springframework.context-3.1.0.RC1.jar
org.springframework.beans-3.1.0.RC1.jar
org.springframework.asm-3.1.0.RC1.jar

You also the following jars inside your project’s lib folder in order to use JSF framework;

jstl.jar
standard.jar
jsf-impl.jar
jsf-api.jar
commons-beanutils.jar
commons-collections.jar
commons-digester.jar
commons-logging.jar

Here you can see the project’s library structure;

SpringJSFApplication.class this class will be used as  the main application inside the program, it also has a reference for the FooService class  initialized by Spring Framework.

package com.tunatore.spring.application;

import com.tunatore.spring.service.FooService;

/**
*
* @author tunatore
*/
public class SpringJSFApplication {

private FooService fooService;

/**
* @return the fooService
*/
public FooService getFooService() {
return fooService;
}

/**
* @param fooService the fooService to set
*/
public void setFooService(FooService fooService) {
this.fooService = fooService;
}

}

 Foo.class is used a regular bean and it is solely Java POJO

package com.tunatore.spring.bean;

/**
*
* @author tunatore
*/
public class Foo {
private String name = "FooBean Name is here!";

public Foo(String name) {
this.name = name;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

}

 FooService.class is used to get a Foo Object (or from Database)

package com.tunatore.spring.service;

import com.tunatore.spring.bean.Foo;

/**
*
* @author tunatore
*/
public class FooService {
public Foo getFooBean() {
return new Foo("This is a new Foo [initiated by Spring Framework]");
}
}

The following file is required to configure Spring beans

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Document : applicationContext.xml
 Created on : 28 November 2011, 13:47
 Author : tunatore
 Description:
 applicationContext.xml file is used by
 Spring for bean initializations
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<bean id="springJSFApplication" class="com.tunatore.spring.application.SpringJSFApplication">
 <property name="fooService" ref="fooService"/>
</bean>

<bean id="fooService" class="com.tunatore.spring.service.FooService"/>

</beans>

web.xml file should include the location of the applicationContext.xml and two Spring specific listeners
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
 <servlet-name>Faces Servlet</servlet-name>
 <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>Faces Servlet</servlet-name>
 <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
 <session-config>
 <session-timeout>
 30
 </session-timeout>
 </session-config>
 <welcome-file-list>
 <welcome-file>faces/index.jsp</welcome-file>
 </welcome-file-list>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext*.xml</param-value>
 </context-param>
 <listener>
 <listener-class>
 org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
 <listener>
 <listener-class>
 org.springframework.web.context.request.RequestContextListener
 </listener-class>
 </listener>
</web-app>

With the following faces-config.xml file
fooService Service object will be initialized through Spring Framework. You  also need to define 
org.springframework.web.jsf.DelegatingVariableResolver inside this file.

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>

<!-- =========== FULL CONFIGURATION FILE ================================== -->

<faces-config version="1.2"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

<application>
 <variable-resolver>
 org.springframework.web.jsf.DelegatingVariableResolver
 </variable-resolver>
 </application>
 <managed-bean>
 <managed-bean-name>SpringJSFApplication</managed-bean-name>
 <managed-bean-class>
 com.tunatore.spring.application.SpringJSFApplication
 </managed-bean-class>
 <managed-bean-scope>session</managed-bean-scope>
 <managed-property>
 <property-name>fooService</property-name>
 <value>#{fooService}</value>
 </managed-property>
 </managed-bean>
</faces-config>

This index page will be call fooService methods 

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<f:view>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <title>Spring JSF Integration Example</title>
 </head>
 <body>
 <h1><h:outputText value="#{SpringJSFApplication.fooService.fooBean.name}"/></h1>
 </body>
 </html>
</f:view>

When you run the program, you will see the following screen on the browser;

You can download the program’s source by clicking here (This is a war file including sources)

Comments or questions are welcomed

Advertisements

ClassNotFoundException: org.springframework.web.context.ContextLoaderListener


      Click here to attend Spring Framework 4.x and certification course with a discount

If you get the following exception while integrating Spring Framework with your applications, just download the spring-web.jar from the following link and add it inside the project’s lib folder.

Download spring-web.jar

SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4149)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:675)

How to close (remove) Vaadin Modal Window by pressing a button


Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

If you want to remove or close a modal window after pressing a button inside the Vaadin Modal Window, use the following code snippets;

1.solution is;

@Override
public void buttonClick(ClickEvent event) {
	Button source = event.getButton();
        if (source == send) { 
	    getApplication().getMainWindow().showNotification("Send Button Pressed");
	    getApplication().getMainWindow().removeWindow(getWindow());
	} else if (source == cancel){
	    getApplication().getMainWindow().showNotification("Cancel Button Pressed");
	} 
}

2.solution is;

@Override
public void buttonClick(ClickEvent event) {
	Button source = event.getButton();
        if (source == send) { 
	    getApplication().getMainWindow().showNotification("Send Button Pressed");
            //first access the parent window and remove the current window
	    ((Window) getWindow().getParent()).removeWindow(getWindow());
	} else if (source == cancel){
	    getApplication().getMainWindow().showNotification("Cancel Button Pressed");
	} 
}

How to calculate method execution time in Java (using System.currentTimeMillis())


Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

Here you can find a code example for calculating the execution time of a method;


package methodexecutiontimer;

/**
*
* @author tunatore
*/
public class MethodExecutionTimer {

public static void main(String[] args) {

  long start = System.currentTimeMillis();
  heavyMethod();
  long end = System.currentTimeMillis();
  System.out.println("Method execution total time"
  + " in seconds ==&gt; " + (end - start) / 1000 + " seconds");

}

public static void heavyMethod() {

  for (int i = 1; i &lt; 5; i++) {
  try {
      Thread.sleep(i * 1000); // * 1000 makes it second 1*1000 = 1 second
  } catch (InterruptedException ex) {
  ex.printStackTrace()
  }

 }
}
}

 

Program output;

run:
Method execution total time in seconds ==> 10 seconds
BUILD SUCCESSFUL (total time: 10 seconds)

How to use Spring JDBC Template with BoneCP Connection Pooling


Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

In this post, I am going to give some information about configuring Spring Framework’s JDBC Template.

In traditional Java programming, programmers have to open connection while getting access for databases.

During this phase, programmers open connections, close connections, resultsets and statements for every data access from the database server.

However, if you use Spring Framework’s JDBC Template you don’t need to these steps and leave the work to Spring.

After including necessary jars (libraries), the Spring framework will do the work for you. And if you create a reference inside code for the JDBC Template, Spring will get a connection for you, close it also. You can also provide one time mapping for the column names inside your code and use it anywhere in your application.

By doing that, you will be available to re-use the code snippets inside your application thus you will have a clean and manageable code, a better design for traditional Java applications.

So using Spring based applications, you will only need to define connection parameters, provide SQL statements, declare parameters, set parameters and iterate over selected data sets.

What is BoneCP?

BoneCP is a Java Connection Pooling application used by including its jars and dependencies inside your project’s lib folder.

If you use BoneCP or any other connection pools similar to BoneCP, you won’t require to make any configuration on Application Server and the connection pooling framework will open many connections and store them inside a queue and make them available to use by database access requests.

This approach in Java programming will provide a good performance while accessing database servers because in every request a connection (opening new one) requires a high I/O operation on Servers.

The following application will try to give more information about integrating a connection pool (you can use a normal database access also), BoneCP, with Spring’s JDBC Template.

In this example, I am using MySQL database with its example database WORLD. (Country table in this database will be used as a Base Table)

The following operations will be done on Country table by using SpringJDBCTemplate.

Here is the Country class;

package com.tunatore.springjdbc;

/**
 *
 * @author tunatore
 */
public class Country {

 private String code;
 private String name;
 private String continent;
 private String region;

/**
 * @return the code
 */
 public String getCode() {
 return code;
 }

/**
 * @param code the code to set
 */
 public void setCode(String code) {
 this.code = code;
 }

/**
 * @return the name
 */
 public String getName() {
 return name;
 }

/**
 * @param name the name to set
 */
 public void setName(String name) {
 this.name = name;
 }

/**
 * @return the continent
 */
 public String getContinent() {
 return continent;
 }

/**
 * @param continent the continent to set
 */
 public void setContinent(String continent) {
 this.continent = continent;
 }

/**
 * @return the region
 */
 public String getRegion() {
 return region;
 }

/**
 * @param region the region to set
 */
 public void setRegion(String region) {
 this.region = region;
 }

}

CountryDAO interface

</pre>
package com.tunatore.springjdbc;
import java.util.List;

/**
 *
 * @author tunatore
 */
public interface CountryDAO {
 public boolean insertCountry(Country c);
 public boolean insertCountries(List<Country> countries);
 public boolean deleteCountryByCode(String code);
 public boolean updateCountry(Country u);
 public Country getCountryByCode(String code);
 public List<Country> getAllCountries();
}

JDBCCountryDAO class

package com.tunatore.springjdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

/**
 *
 * @author tunatore
 */
public class JDBCCountryDAO implements CountryDAO {

private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
 this.jdbcTemplate = new JdbcTemplate(dataSource);
 }
 /**
 * @return the jdbcTemplate
 */
 public JdbcTemplate getJdbcTemplate() {
 return jdbcTemplate;
 }
 @Override
 public boolean insertCountry(final Country c) {
 boolean result = false;
 final String SQL = "INSERT INTO WORLD.COUNTRY (CODE,NAME,CONTINENT,REGION) "
 + " VALUES(?,?,?,?)";
 //KeyHolder keyHolder = new GeneratedKeyHolder();
 int row= this.jdbcTemplate.update(new PreparedStatementCreator(){
 @Override
 public PreparedStatement createPreparedStatement(Connection connection)
 throws SQLException {
 PreparedStatement ps =connection.prepareStatement(SQL);
 ps.setString(1, c.getCode());
 ps.setString(2, c.getName());
 ps.setString(3, c.getContinent());
 ps.setString(4, c.getRegion());
 return ps;
 }
 });
 if (row > 0)
 result=true;
 //if you want to return the generated auto increment key use keyHolder
 /*,keyHolder);
 *return keyHolder.getKey().intValue();
 */
 return result;
 }

 @Override
 public boolean insertCountries(final List<Country> countries) {
 boolean result = true; //you should catch the exception here in Production applications
 final String SQL = "INSERT INTO WORLD.COUNTRY (CODE,NAME,CONTINENT,REGION) "
 + " VALUES(?,?,?,?)";
 int[] updateCounts = this.jdbcTemplate.batchUpdate(SQL, new BatchPreparedStatementSetter() {
 @Override
 public void setValues(PreparedStatement ps, int i) throws SQLException {
 Country c = countries.get(i);
 ps.setString(1, c.getCode());
 ps.setString(2, c.getName());
 ps.setString(3, c.getContinent());
 ps.setString(4, c.getRegion());
 }
 @Override
 public int getBatchSize() {
 return countries.size();
 }
 });
 return result;
 }

 @Override
 public boolean deleteCountryByCode(final String code) {
 boolean result = false;
 final String SQL = "DELETE FROM WORLD.COUNTRY"
 + " WHERE CODE=?";
 int row = jdbcTemplate.update(new PreparedStatementCreator() {
 public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
 PreparedStatement ps = connection.prepareStatement(SQL);
 ps.setString(1, code);
 return ps;
 }
 });
 if (row>0)
 result = true;
 return result;
 }

@Override
 public boolean updateCountry(final Country c) {
 boolean result = false;
 final String SQL = "UPDATE WORLD.COUNTRY SET NAME=?,"
 + "CONTINENT=?,REGION=? WHERE CODE=?";
 int row = jdbcTemplate.update(new PreparedStatementCreator() {
 public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
 PreparedStatement ps = connection.prepareStatement(SQL);
 ps.setString(1, c.getName());
 ps.setString(2, c.getContinent());
 ps.setString(3, c.getRegion());
 ps.setString(4, c.getCode());
 return ps;
 }
 });
 if (row>0)
 result = true;
 return result;
 }

 @Override
 public Country getCountryByCode(String code) {
 String SQL = "SELECT * FROM WORLD.COUNTRY"
 + " WHERE code = ?";
 try {
 return (Country) this.jdbcTemplate.queryForObject(SQL,
 new CountryMapper(),new Object[]{code});
 } catch (EmptyResultDataAccessException e) {
 //this will return a null if the query cant find any countried with
 //code given by the user
 return null;
 }
 }

@Override
 public List<Country> getAllCountries() {
 String SQL = "SELECT * FROM WORLD.COUNTRY";
 List<Country> countries = this.jdbcTemplate.query(SQL,
 new RowMapper<Country>() {
 //using anonymous inner class RowMapper here
 @Override
 public Country mapRow(ResultSet rs, int rowNum) throws SQLException {
 Country c = new Country();
 c.setCode(rs.getString("code"));
 c.setName(rs.getString("name"));
 c.setContinent(rs.getString("continent"));
 c.setRegion(rs.getString("region"));
 return c;
 }
 });
 return countries;
 }

 //You should use this mapper
 //If you have dublicate RowMappers
 //this is a static Inner Class implementing RowMapper
 private static final class CountryMapper implements RowMapper<Country> {
 public Country mapRow(ResultSet rs, int rowNum) throws SQLException {
 Country c = new Country();
 c.setCode(rs.getString("code"));
 c.setName(rs.getString("name"));
 c.setContinent(rs.getString("continent"));
 c.setRegion(rs.getString("region"));
 return c;
 }
 }
}

MainProgram class

package com.tunatore.springjdbc;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author tunatore
 * Spring JDBC Template Example
 * CRUD application fro creating a Country in the World
 * By using this software, you don't require to open and close standard
 * JDBC connection, SPRING framework will do the work for you
 */
public class MainProgram {

public static void main(String[] args) {

 try {
 //you need to load the beans.xml file from the Classpath
 //Spring will initialize all the beans defined in this file
 //Database information will be also get from this file and jdbc.properties file
 //which is also referenced by beans.xml file
 //Spring will create and manage all beans defined in this file
 ApplicationContext appContext =
 new ClassPathXmlApplicationContext("com/tunatore/springjdbc/beans.xml");
 //If you want to get the implementation of JDBCCountry interface
 //getBean will get the countryDAO from Spring Application
 JDBCCountryDAO countryDAO = (JDBCCountryDAO)appContext.getBean("countryDAO");

 //now here we can use all the methods defined in JDBCCountryDAO interface
 //getting all countries
 List<Country> countries = countryDAO.getAllCountries();
 for (Iterator<Country> it = countries.iterator(); it.hasNext();) {
 Country country = it.next();
 System.out.println("Country name ==> Code: " + country.getCode());
 System.out.println("Name: " + country.getName());
 System.out.println("Continent: " + country.getContinent());
 System.out.println("Region: " + country.getRegion());
 }

 //getting a country by code
 Country USA = countryDAO.getCountryByCode("USA");
 System.out.println("Country name ==> Code: " + USA.getCode());
 System.out.println("Name: " + USA.getName());
 System.out.println("Continent: " + USA.getContinent());
 System.out.println("Region: " + USA.getRegion());

 //update a country by code
 USA.setName("United States of Java");
 countryDAO.updateCountry(USA);
 USA = countryDAO.getCountryByCode("USA");
 System.out.println("After update Name: " + USA.getName());

 //inserting a new country
 Country newCountry = new Country();
 newCountry.setCode("NNN");
 newCountry.setName("NEW COUNTRY");
 newCountry.setContinent("Asia");
 newCountry.setRegion("NEW REGION");
 boolean isInserted = countryDAO.insertCountry(newCountry);
 System.out.println("Country isInserted ==> " + isInserted);

 //inserting countries
 Country newCountry1 = new Country();
 newCountry1.setCode("NN1");
 newCountry1.setName("NEW COUNTRY");
 newCountry1.setContinent("Asia");
 newCountry1.setRegion("NEW REGION");
 Country newCountry2 = new Country();
 newCountry2.setCode("NN2");
 newCountry2.setName("NEW COUNTRY");
 newCountry2.setContinent("Asia");
 newCountry2.setRegion("NEW REGION");

 List<Country> newCountriesList = new ArrayList<Country>();
 newCountriesList.add(newCountry1);
 newCountriesList.add(newCountry2);
 boolean isCountriesInserted = countryDAO.insertCountries(newCountriesList);
 System.out.println("Countries isInserted ==> " + isCountriesInserted);

 //deleting a new country
 boolean isDeleted = countryDAO.deleteCountryByCode("NN1");
 System.out.println("Country NN1 isDeleted ==> " + isDeleted);

 } catch (Exception ex) {
 Logger.getLogger(MainProgram.class.getName()).log(Level.SEVERE, null, ex);
 }

 }
}

beans.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <bean id="countryDAO" class="com.tunatore.springjdbc.JDBCCountryDAO">
 <property name="dataSource" ref="dataSource"/>
 </bean>

 <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
 <property name="driverClass" value="${jdbc.driverClass}"/>
 <property name="jdbcUrl" value="${jdbc.url}"/>
 <property name="username" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>
 <property name="idleConnectionTestPeriodInMinutes" value="60"/>
 <property name="idleMaxAgeInMinutes" value="240"/>
 <property name="maxConnectionsPerPartition" value="30"/>
 <property name="minConnectionsPerPartition" value="10"/>
 <property name="partitionCount" value="3"/>
 <property name="acquireIncrement" value="5"/>
 <property name="statementsCacheSize" value="100"/>
 <property name="releaseHelperThreads" value="3"/>
 </bean>

 <!-- if you don't want to use connection pooling you should comment out the following lines
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="${jdbc.driverClass}"/>
 <property name="url" value="${jdbc.url}"/>
 <property name="username" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>
 </bean>
 -->
 <context:property-placeholder location="com/tunatore/springjdbc/jdbc.properties"/>

</beans>

jdbc.properties file

#this file store all the neccessary information
#related to database
#in this example MySQL database is used
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/world
jdbc.username=root
jdbc.password=admin

log4j.xml file content

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
 <layout class="org.apache.log4j.PatternLayout">
 <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
 </layout>
</appender>
<root>
 <level value="error" />
 <appender-ref ref="console" />
</root>
</log4j:configuration>

Note that if you change the level value to debug, you will see many debug logs inside the console. It is better to set it to debug if you want to get more information regarding the Spring application initialization.

And on the following screenshot you can see the required libraries for the program. Most of the libraries are required for Spring Framework and BoneCP.

And if you run the MainProgram  you will see an output like the following one;

….
….
….

Country name ==> Code: VNM
Name: Vietnam
Continent: Asia
Region: Southeast Asia
Country name ==> Code: VUT
Name: Vanuatu
Continent: Oceania
Region: Melanesia
Country name ==> Code: WLF
Name: Wallis and Futuna
Continent: Oceania
Region: Polynesia
Country name ==> Code: WSM
Name: Samoa
Continent: Oceania
Region: Polynesia
Country name ==> Code: YEM
Name: Yemen
Continent: Asia
Region: Middle East
Country name ==> Code: YUG
Name: Yugoslavia
Continent: Europe
Region: Southern Europe
Country name ==> Code: ZAF
Name: South Africa
Continent: Africa
Region: Southern Africa
Country name ==> Code: ZMB
Name: Zambia
Continent: Africa
Region: Eastern Africa
Country name ==> Code: ZWE
Name: Zimbabwe
Continent: Africa
Region: Eastern Africa
Country name ==> Code: USA
Name: United States of Java
Continent: North America
Region: North America
After update Name: United States of Java
Country isInserted ==> true
Countries isInserted ==> true
Country NN1 isDeleted ==> true

Download Source Code for SpringJDBCTemplate with BoneCP

log4j:WARN No appenders could be found for logger


If you get the following warning in your application after adding log4j.jar inside your application. You should need to put log4j.xml and log4j.dtd in your application’s classpath. (putting these files inside a source folder will  be also worked, it should be in the classpath)

If you use  Netbeans IDE, it is better to create a source folder named log4j for the config files like the one on the following image

 

 

 

 

 

run:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
BUILD SUCCESSFUL (total time: 1 second)

Exception: PreparedStatementCallback; SQL []; Generated keys not requested.


      Click here to attend Spring Framework 4.x and certification course with a discount

Changing the MySQL connector from

mysql-connector-java-5.1.15-bin.jar to

mysql-connector-java-5.1.2.jar fixed the problem that I had.

org.springframework.dao.TransientDataAccessResourceException:
PreparedStatementCallback; SQL []; Generated keys not requested.
You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate()
or Connection.prepareStatement().; nested exception is java.sql.SQLException:
Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to
Statement.executeUpdate() or Connection.prepareStatement().     
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate
(SQLStateSQLExceptionTranslator.java:107)     
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate
(AbstractFallbackSQLExceptionTranslator.java:72)     
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate
(AbstractFallbackSQLExceptionTranslator.java:80)     
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate
(AbstractFallbackSQLExceptionTranslator.java:80)     
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:603)     
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:843)