Spring and JDBC

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.

1. Open Spring Tool kit we installed here.

2. 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. 

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.

Please find link to download source code.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan