Sunteți pe pagina 1din 8

Spring JDBC Application

First create a package

package packagepk;
public interface CustomerDAO { public void insert(Customer customer); public Customer findByCustomerId1(); }

Class which implements above package


package packagepk;

import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.support.JdbcDaoSupport;

import packagepk.CustomerDAO; import packagepk.Customer;

public class JdbcCustomerDAO implements CustomerDAO {

private DataSource dataSource; private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; }

public void insert(Customer customer){

// code for JDBC Template // // // // // // // } }); jdbcTemplate.update(sql, new Object[] { customer.getCustId(), customer.getName(),customer.getAge() jdbcTemplate = new JdbcTemplate(dataSource);

Connection conn = null; try { conn = dataSource.getConnection(); String sql = "INSERT INTO CUSTOMER "+"(CUST_ID, NAME, AGE,INFO) VALUES (?, ?, ? ,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, customer.getCustId()); ps.setString(2, customer.getName()); ps.setInt(3, customer.getAge());

ps.setString(4, customer.getInfo()); ps.executeUpdate(); ps.close();

} catch (SQLException e) { throw new RuntimeException(e);

} finally { if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }

public Customer findByCustomerId1(){

String sql = "SELECT * FROM CUSTOMER WHERE INFO LIKE '%fidel%'";

Connection conn = null;

try { conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql);

Customer customer = null; ResultSet rs = ps.executeQuery(); while (rs.next()) { customer = new Customer(rs.getInt("CUST_ID"),rs.getString("NAME"),rs.getInt("Age"),rs.ge tString("INFO")); System.out.println(rs.getInt("CUST_ID")+" " +rs.getInt("Age")+" "+rs.getString("INFO"));

"+rs.getString("NAME")+" }

rs.close(); ps.close(); return customer; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } }

public Customer findByCustomerId(int custId) { // TODO Auto-generated method stub return null; } }

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-2.5.xsd"> <import resource="Spring-Datasource.xml" /> <import resource="Spring-Customer.xml" /> </beans>

Spring-Datasource.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-2.5.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mydemo" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> </beans>

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-2.5.xsd">

<bean id="customerDAO" class="packagepk.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> </beans>

Customer.java

package packagepk; import java.sql.Timestamp; public class Customer { int custId; String name; int age; String info; public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public Customer(int i, String string, int j,String info) { this.custId=i; this.name=string; this.age=j; this.info=info; } 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; }

App.java

package packagepk;

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import packagepk.CustomerDAO; import packagepk.Customer;

public class App { public static void main( String[] args ) { ApplicationContext context =new ClassPathXmlApplicationContext("Spring-Module.xml");

CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO"); Customer customer = new Customer(8, "mmmmm",23,"Java Developer in FIDEL"); customerDAO.insert(customer);

Customer customer1=customerDAO.findByCustomerId1();

// Customer customer1 = customerDAO.findByCustomerId(1); // System.out.println(customer1);

} }

S-ar putea să vă placă și