Previous | Home | Next |
First you make a Table inside the Mysql Database.
CREATE TABLE `amit`.`employee` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL, `salary` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) )
Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:
- Spring 3.0
- Eclipse IDE
- JDK 1.6
Step1. Now you create a simple Java Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your project as SpringwithJDBCTemplate using the wizard window as given below.

Step2. Now add spring jar in your project, To do this, right click on your project name XmlSchemaBasedAop and then follow the following option available in context menu: Build Path -> Configure Build Path to display the Java Build Path window as given below. There are some jar files required to run this application, To load the jar files in eclipse IDE, Right click on your project - Build Path - Add external archives - select all the required jar files - finish.
- antlr-runtime-3.0.1
- org.springframework.aop-3.1.0
- org.springframework.asm-3.1.0.
- org.springframework.aspects-3.1.0.
- org.springframework.beans-3.1.0.
- org.springframework.context.support-3.1.0.
- org.springframework.context-3.1.0.
- org.springframework.core-3.1.0.
- org.springframework.expression-3.1.0.
- commons-logging-1.1.1
- com.springsource.org.aopalliance-1.0.0.jar
- org.springframework.jdbc-3.0.0.M2.jar
- org.springframework.transaction-3.1.0.RELEASE.jar
- mysql-connector-java-3.0.10-stable-bin.jar
Step3. Now you create simply java file with any name. First we need to create a package with any name like com.r4r. To do this, Right click on src in package explorer section and follow the option : New -> Package. and then Right click on package(com.r4r) - New - class - Write the class name e.g. Employee - finish. Write the following code:
package com.r4r; public class Employee { private int id; private String name; private int salary; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public Employee(int id, String name, int salary) { super(); this.id = id; this.name = name; this.salary = salary; } }
Step4. Now you create another Java class named EmpDao.java.
package com.r4r; import org.springframework.jdbc.core.JdbcTemplate; public class EmpDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } // Save Record in database. public int saveEmployee(Employee e){ String query="insert into employee values ('"+e.getId()+"','"+e.getName()+"','"+e.getSalary()+"')"; return jdbcTemplate.update(query); } // Update Record in Database. public int updateEmployee(Employee e){ String query="update employee set name= '"+e.getName()+"',salary='"+e.getSalary()+"' where id='"+e.getId()+"' "; return jdbcTemplate.update(query); } //Delete Record in Database. public int deleteEmployee(Employee e){ String query="delete from employee where id='"+e.getId()+"' "; return jdbcTemplate.update(query); }
Step5. Now create the xml file click on src- new -file -give the file name like applicationContext.xml then finish.
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/amit" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"></property> </bean> <bean id="edao" class="com.r4r.EmpDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>
Step6.Now Create the java Main class (MainApp.java).
package com.r4r; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDao dao=(EmpDao)ctx.getBean("edao"); int status=dao.saveEmployee(new Employee(103,"Amit",1000)); System.out.println(status); int status1=dao.updateEmployee(new Employee(102,"smith",1500)); System.out.println(status1); Employee e=new Employee(status1, null, status1); e.setId(103); int status2=dao.deleteEmployee(e); System.out.println(status2); } } }
Now run the Application you see the following output

Previous | Home | Next |