Creating Spring Application in Eclipse IDE
Creating Spring Application in Eclipse IDE
Let's see the 5 steps to create the first spring application using eclipse
IDE.
There are mainly three jar files required to run this application.
o org.springframework.core-3.0.1.RELEASE-A
o com.springsource.org.apache.commons.logging-1.1.1
o org.springframework.beans-3.0.1.RELEASE-A
package com.pu;
To create the xml file click on src - new - file - give the file name
such as applicationContext.xml - finish. Open the
applicationContext.xml file, and write the following code
The bean element is used to define the bean for the given class.
The property subelement of bean specifies the property of the
Student class named name. The value specified in the property
element will be set in the Student class object by the IOC container.
Create the java class e.g. Test. Here we are getting the object of
Student class from the IOC container using the getBean() method of
BeanFactory. Let's see the code of test class.
package com.pu;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Student student=(Student)factory.getBean("studentbean");
student.displayInfo();
}
}
Spring JDBC Example
CREATE TABLE Student(ID INT,NAME VARCHAR(20),AGE
INT);
St
ep Description
s
Create a project with a name SpringExample and
1 create a package com.pu under the src folder in the
created project.
Add required Spring libraries using Add External
2 JARs option as explained in the Spring Hello
World Example chapter.
Add Spring JDBC specific latest libraries mysql-
connector-java.jar, org.springframework.jdbc.j
3 ar and org.springframework.transaction.jar in
the project. You can download required libraries if
you do not have them already.
Create DAO interface StudentDAO and list down
all the required methods. Though it is not required
4 and you can directly
write StudentJDBCTemplate class, but as a good
practice, let's do it.
Create other required Java
5 classes Student, StudentMapper, StudentJDBCTem
plate and MainApp under the com.pu package.
Make sure you already created Student table in
TEST database. Also make sure your MySQL
6 server is working fine and you have read/write
access on the database using the give username and
password.
Create Beans configuration file Beans.xml under
7
the src folder.
The final step is to create the content of all the Java
8 files and Bean Configuration file and run the
application
StudentDAO.java
package com.pu;
import java.util.List;
import javax.sql.DataSource;
Student.java file
package com.pu;
StudentMapper.java file
package com.pu;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
StudentJDBCTemplate.java
package com.pu;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
MainApp.java file
package com.pu;
import java.util.List;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationConte
xt;
import com.tutorialspoint.StudentJDBCTemplate;
StudentJDBCTemplate studentJDBCTemplate =
(StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
System.out.println("------Records Creation--------");
studentJDBCTemplate.create("Zara", 11);
studentJDBCTemplate.create("Nuha", 2);
studentJDBCTemplate.create("Ayan", 15);
System.out.println("--Listing Multiple Records---");
List<Student> students = studentJDBCTemplate.listStudents();
for (Student record : students) {
System.out.print("ID : " + record.getId() );
System.out.print(", Name : " + record.getName() );
System.out.println(", Age : " + record.getAge());
}
System.out.println("--Updating Record with ID = 2 -" );
studentJDBCTemplate.update(2, 20);
System.out.println("--Listing Record with ID = 2 --" );
Student student = studentJDBCTemplate.getStudent(2);
System.out.print("ID : " + student.getId() );
System.out.print(", Name : " + student.getName() );
System.out.println(", Age : " + student.getAge());
}
}