Spring DBCP

Step 1 Create database

In this example, I just use the default test database. Now we need to create user table:

Step 2 – Create new maven-based java project

Step 2.1 Create new java project

Go to File->New->Others… Select Java project then click Next. You can choose any name for the project.

Step 2.2 Convert the project to Maven Project

Right click to the project; select Configure->Convert to Maven Project. Eclipse will generate Project Object Model (pom.xml) file. Open the file by Eclipse text editor then paste the content below.

Note: you can create normal java project and add jar files to your project build path. But it’s easier & faster using maven as maven is integrated with Eclipse IDE; so you don’t need to manually install it. 

Step 3 Create XML Bean Configuration file

  1. Create package name info.java.tips.config
  2. Create an empty xml file named spring-bean.xml under package then paste the content below.

Step 4 Create properties file

Right click to info.java.tips.config package; select New->Others… Select File under General category and name it jdbc.properties and paste the following content:

Step 5 Create java bean class

Copy the below code; select your src folder in your project  and press CTRL + V to paste the code. Eclipse IDE will automatically create new class & package.

Step 6 Create DAO classes

Create package name info.java.tips.dao. Under the package, create DAO interface & its implementation.

Step 6.1 Create UserDao interface

Step 6.2 Create DAO class

Step 7 Create test class

Compile & Run the application

Go to Project -> Clean to clean & build the project. Eclipse will download & add maven dependencies to your project build path; then compile your classes.

Right click to the test class; select Run As -> Java Application