1) Create The Table in Database::: 2) Create The Class Customer Module Includes The Setter N Getter Methods .As
1) Create The Table in Database::: 2) Create The Class Customer Module Includes The Setter N Getter Methods .As
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
}
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>
Spring-Jdbc-Connection._Govind
<property name="url" value="jdbc:mysql://localhost:3306/spring" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> </beans>