package DAO.Imp; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapperResultReader; import DAO.UserDAO; import Model.User; /** * DAO层的实现 * * * @author 李嘉陵 * @since 2006-4-30 8:41:26 * @version 0.10a **/ public class UserDAOImp implements UserDAO{ private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } class UserRowMapper implements RowMapper { public Object mapRow(ResultSet rs,int index) throws SQLException { User u = new User(); u.setId(rs.getString("ID")); u.setName(rs.getString("Name")); u.setPassword(rs.getString("Password")); return u; } } public void selectWithTemp() { String sql = "select * from admin"; jdbcTemplate.query(sql,new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { System.out.println("ID: "+rs.getString("ID")+" Name: "+rs.getString("name")); } } ); } public List select(String where) { List list; String sql = "select * from admin "+where; list = jdbcTemplate.query(sql,new RowMapperResultReader(new UserRowMapper())); return list; } public User selectById(String id) { String sql = "select * from admin where id=?"; final User u = new User(); final Object[] params = new Object[] {id}; jdbcTemplate.query(sql,params, new RowCallbackHandler(){ public void processRow(ResultSet rs) throws SQLException { u.setId(rs.getString("ID")); u.setName(rs.getString("NAME")); u.setPassword(rs.getString("PASSWORD")); } }); return u; } public void update(String how) { String sql = how; jdbcTemplate.update(sql); } public void insert(User u) { String sql = "insert into admin (ID,NAME,PASSWORD) values (admin_id_seq.nextval,?,?)"; Object[] params = new Object[] { u.getName(), u.getPassword()}; jdbcTemplate.update(sql,params); } } |