声明:例子来源于《Spring2.0 技术手册》 一书
闲话不多说
环境 Spring2.0
建表语句:
create table t_user(
id number(6),
name varchar2(12),
age int
)
需要的jar 包
程序结构图
*注意beans-config.xml文件是在src目录下
先定义接口
public interface IUserDAO { public void insert(User user); public User find(int id); }
package com.spring2.demo; public class User { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } }
package com.spring2.demo; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; public class UserDao implements IUserDAO { private DataSource dataSource; public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public User find(int id) { // TODO Auto-generated method stub Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; String sql = "SELECT * FROM t_user WHERE id=?"; try{ conn = dataSource.getConnection(); psmt = conn.prepareStatement(sql); psmt.setInt(1, id); rs = psmt.executeQuery(); while(rs.next()){ int uid = rs.getInt(1); String name = rs.getString(2); int age = rs.getInt(3); User user = new User(); user.setId(uid); user.setName(name); user.setAge(age); return user; } }catch (SQLException e) { e.printStackTrace(); }finally{ if(null != psmt){ try { psmt.close(); } catch (Exception e) {} } if(null != conn){ try { conn.close(); } catch (Exception e) {} } } return null; } public void insert(User user) { // TODO Auto-generated method stub int id = user.getId(); String name = user.getName(); int age = user.getAge(); Connection conn = null; PreparedStatement psmt = null; String sql = "INSERT INTO t_user(id, name, age) VALUES(?,?,?)"; try{ conn = dataSource.getConnection(); psmt = conn.prepareStatement(sql); psmt.setInt(1, id); psmt.setString(2, name); psmt.setInt(3, age); psmt.executeUpdate(); }catch (SQLException e) { e.printStackTrace(); }finally{ if(null != psmt){ try { psmt.close(); } catch (Exception e) {} } if(null != conn){ try { conn.close(); } catch (Exception e) {} } } } }
<?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.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@IP地址:1521:orcl" /> <property name="username" value="用户名" /> <property name="password" value="密码" /> </bean> <bean id="userDAO" class="com.spring2.demo.UserDao"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
测试:
package com.spring2.demo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDaoDemo { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml"); User user = new User(); user.setId(2); user.setName("Jerry"); user.setAge(50); IUserDAO userDAO = (IUserDAO) context.getBean("userDAO"); userDAO.insert(user); System.out.println("INSERT FINISHED>>>>>>>"); user = userDAO.find(1); System.out.println("name-->"+user.getName()); } }