JDBC Integration
As part of this topic we will be integrating Spring and JDBC using PreparedStatement.
We will be using Spring Tool Suit to create spring Java application which is nothing but an extension of Eclipse editor as part of this blog using with bean.xml.
To download and setup Spring Tool Kit please click on this link.
Follow below steps to create our very first Spring application project.
- Open Spring Tool kit we installed here.
- Click on File in top navigation menu and than click on new. There will be an option Spring Starter Project start appearing once we click on create new option.
Below window will start appearing.
Provide below highlighted value for Group and Artifact for the project and click on Next.
3. In the below screen select open Web option and select Spring Web. Click on Finish will create Spring project.
4. Create Person.java pojo class as mentioned in below screenshot with id, firstName, and age as a field with setter getter.
5. Create an interface PersonDao.java class to declare function which will help us to insert, update, select and delete record from database.
6. Create person table in MySQL database using below query
create table person(id int NOT NULL AUTO_INCREMENT, firstname varchar(20), age int, primary key(id));
7. Add below highlighted code in application pom.xml which will help us to connect with MySQL database.
8. Insert Person record in MySQL:
Create JdbcPersonDAO.java class which will be implementing PersonDao.java interface.
Create a DataSource variable at line number 13 with setter method to collect all configurations from bean.xml file.
Create method to insertPerson() to insert a Person record in the database.
9. Create bean.xml under below hierarchy src/main/resources.
10. Place below content inside created bean.xml
Above code from line number 10 to 15 will help to create JDBC data source object having all details require to connect with MySQL database.
Create an id=personDAO of com.example.demo.JdbcPersonDAO class and assign a dataSource value to it.
11. Place below code at line number 11 and 12 inside Springannotation1Application.java class.
12. Run main method from Springannotation1Application.java class will generate below output.
13. Delete and List down all Person records:
Below listAllPerson() method fetch all records from person table and print all the results.
delete() method will delete record for specific id from person MySQL table.
uncomment the below lines to delete a person record and and list all erson Records.
Run main method from Springannotation1Application.java class will generate below output.
14. Update and getPerson(id) Person record:
Below update() method will update person age to 30 having id equal to 3.
getPerson(id) method will fetch Person record from MySQL database having id equal to 3.
Uncomment the below lines to update a person record and get person details by id.
Run main method from Springannotation1Application.java class will generate below output.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.