0% found this document useful (0 votes)
18 views

1) Create The Table in Database::: 2) Create The Class Customer Module Includes The Setter N Getter Methods .As

The document outlines the steps to create a Spring JDBC application to connect to a database and perform CRUD operations. The steps include: 1. Creating a Customer model class with getters and setters 2. Defining a CustomerDAO interface with database methods 3. Implementing the CustomerDAO interface in JdbcCustomerDAO class 4. Configuring the Spring XML files to define the data source, map the DAO implementation, and import dependencies.

Uploaded by

govindbirajdar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

1) Create The Table in Database::: 2) Create The Class Customer Module Includes The Setter N Getter Methods .As

The document outlines the steps to create a Spring JDBC application to connect to a database and perform CRUD operations. The steps include: 1. Creating a Customer model class with getters and setters 2. Defining a CustomerDAO interface with database methods 3. Implementing the CustomerDAO interface in JdbcCustomerDAO class 4. Configuring the Spring XML files to define the data source, map the DAO implementation, and import dependencies.

Uploaded by

govindbirajdar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Spring-Jdbc-Connection._Govind 1) Create the Table in Database::: 2) Create the class Customer Module includes the setter N getter Methods.

as:
package com; public class Customer { int custId; String name; int age; public Customer(int i, String name, int j) { this.custId=i; this.name=name; this.age=j; } //getter N setter of above prpos... public int getCustId() { return custId; } public void setCustId(int custId) { this.custId = custId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

3) Create the DAO customer Interface customerDAO as: which includes methods for performaing the required DB operation../
package com; public interface CustomerDAO { public void insert(Customer customer); //public Customer findByCustomerId(int custId);

Spring-Jdbc-Connection._Govind
}

4) Create the Class JDBCCUSTDAO which implements the CustomerDAO interface.it contains the implementation of methods.i.e. Your Business Logic.
As
package com; import import import import import public java.sql.Connection; java.sql.PreparedStatement; java.sql.ResultSet; java.sql.SQLException; javax.sql.DataSource; class JdbcCustomerDAO implements CustomerDAO { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void insert(Customer customer){ String sql = "INSERT INTO CUSTOMER " + "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)"; Connection conn = null; try { conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, customer.getCustId()); ps.setString(2, customer.getName()); ps.setInt(3, customer.getAge()); ps.executeUpdate(); ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }

Spring-Jdbc-Connection._Govind
}

5) Create the xml files 1) Spring-Module.xml


<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="/Spring-Datasource.xml" /> <import resource="/Spring-Customer.xml" /> </beans>

2) Spring-customer.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="customerDAO" class="com.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> </beans>

3) Spring-Datasource.xml which contains the data_source properties


<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" />

Spring-Jdbc-Connection._Govind
<property name="url" value="jdbc:mysql://localhost:3306/spring" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> </beans>

You might also like