Tutorial Home
Hibernate
-
Advantage of Hibernate over JDBC
- Hibernate Setup with an web Application
- First Hibernate Application
- Hibernate mapping with Database TABLE
- Hibernate Data Type-Java Data Type - SQL Data Type mapping
- One to Many Relation in Hibernate
- One to Many Relation in Hibernate bi-directional
- Many to Many Relation in Hibernate
- HQL: The Hibernate Query Language
- Criteria Queries
- Criteria Queries : Equal (eq), Not Equal(ne), Less than (le), greater than (gt),greater than or equal(ge) and Ordering the results
- Criteria Queries: And OR conditions
- Hibernate generator to generate id (primary key)
- prevent concurrent update in Hibernate,slate object updatation in Hibernate,version checking in Hibernate
Struts
- Model View Controller (MVC)
- Model View Controller (MVC)
- Struts Flow-How Struts Works?
- Struts Tutorial - Struts Setup- First Struts Action class setup
- Message Resources
- Validation Framework
- Validation Framework-client side
- ForwardAction
- IncludeAction
- DispatchAction
- LookupDispatchAction
- DynaActionForm
- DynaActionForm
- Struts Tutorial - Mutli-click prevention using struts tokens-Prevent Duplicate Submission
- Logic Iterate Map and List
JSP
- JSP Tutorial
- Introduction to JSP
- JSP Comments
- JSP Syntax
- JSP Scripting Elements :Scriptlet, expression, declaration
- JSP Directives
- implicit objects in JSP
- JSP Actions
- Introduction to JSP
- jsp:useBean
- The jsp:setProperty Action
- The jsp:getProperty Action
- Introduction to JSP
Spring
- Spring Tutorial
- Introduction to Spring
- Benefits of Using Spring Framework
- Inversion of Control in Spring
- Introduction to BeanFactory
- Dependency Injection in Spring
- Collections Setter Injection
- Bean Scopes in Spring
- Spring IOC Setup Step by Step
- Bean Lifecycle in Spring
- ApplicationContext
- MessageSources in Spring
- Web Spring MVC framework
- Developing Your First Spring Web Application
- Developing Your Second Spring Web Application with Spring Form
- Developing Your First Spring Web Application with Spring Validation Framework with Code Example
- Spring integration with Hibernate
|
Spring Tutorial Step by Step
JdbcTemplate
JdbcTemplate is used to access database using JDBC
The JdbcTemplate class is the central class in the JDBC core package.This class executes SQL queries, update statements or stored procedure calls, imitating iteration over ResultSets and extraction of returned parameter values
It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.
The PreparedStatementCreator callback interface creates a prepared statement given a Connection provided by this class.
CallableStatementCreator interface which creates callable statement.
The RowCallbackHandler interface extracts values from each row of a ResultSet
Below is the steps for JdbcTemplate configuration and Excecute Queries
Step 1. Add the JDBC configuration in the applicationContext.xml (Spring Configuration file)
Spring allows you to define resources like a JDBC DataSource as beans in an application context.
Set up a JDBC DataSource and integrate with JdbcTemplate.
<bean id="empDao" class="com.techfaq.EmpDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/tech"/>
<property name="username" value="techfaq"/>
<property name="password" value="techfaq"/>
</bean>
|
Step 2. Create JdbcTemplate
The JdbcTemplate class is the central class in the JDBC core package.This class executes SQL queries, update statements or stored procedure calls, imitating iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.JdbcTemplate class are threadsafe once configured. This is important because it means that you can configure a single instance of a JdbcTemplate and then safely inject this shared reference into multiple DAOs .
JdbcTemplate is stateful, in that it maintains a reference to a DataSource, but this state is not conversational state
JdbcTemplate is created in the setter for the DataSource.
Below you can see how you can configure JdbcTemplate.
public class EmpDAO {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); }
}
|
Examples of JdbcTemplate
public class EmpDAO {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); }
public int getCount() { return this.jdbcTemplate.queryForInt("select count(*) from EMP"); }
public void createTable() { this.jdbcTemplate.execute("create table EMP (id integer, name varchar(100))"); }
public List getList() { return this.jdbcTemplate.queryForList("select * from EMP");
//The list returned would look something like this:
//[{name=Ram, id=1}, {name=Shyam, id=2}]
} }
|
Other Examples
public void setName(int id, String name) { this.jdbcTemplate.update("update emp set name = ? where id = ?", new Object[] {name, new Integer(id)}); }
public Emp findEmployee(long id) { String sql = "select id, name from EMP where id = ?"; RowMapper mapper = new RowMapper() { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Emp emp = new Emp(); emp.setId(rs.getInt("id")); emp.setName(rs.getString("name")); return emp; } };
public String getName(){ String name = (String) this.jdbcTemplate .queryForObject("select name from emp where id = ?", new Object[]{new Integer(12)}, String.class); retrun name; }
this.jdbcTemplate.execute("create table emp (id integer, name varchar(100))");
Call to stored procedure
this.jdbcTemplate.update("call SUPPORT.REFRESH_EMP_SUMMARY(?)", new Object[]{new Integer(id)});
this.jdbcTemplate.update("insert into emp (name) values (?)", new Object[] {"Dakue"});
this.jdbcTemplate.update("delete from emp");
|
PreparedStatementCreator Example
The method takes a PreparedStatementCreator as its first argument, and this is the way the required insert statement is specified. The other argument is a KeyHolder, which will contain the generated key on successful return from the update.
final String INSERT_SQL = "insert into Emp (name) values(?)";
KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update( new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(INSERT_SQL); ps.setString(1, "Ram"); return ps; } }, keyHolder);
|
|
|