public final class JdbcUtils{
private DataSource myDataSource=null;
//私有函数
private JdbcUtils(){
}
//确保驱动只被注册一次
static{
try{
Properties prop=new Properties();
InputStream is=JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
prop.load(is);
myDataSource=BasicDataSourceFactory.createDataSource(prop);
}catch(ClassNotFoundException e){
throw new ExceptionInInitializerError(e);
}
}
//获得数据源
public static DataSource getDataSource(){
return myDataSource;
}
//获得连接
public static Connection getConnection(){
//建立连接(Connection)
return myDataSource.getConnection();
}
//释放资源
public static void free(ResultSet rs,Statement st,Connection conn){
try{
if(rs!=null)
rs.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(st!=null)
st.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
if(conn!=null)
try{
conn.close();
}catch(SQLException e){
e.printStackTrance();
}
}
}
}
}
//使用SimpleTemplate
public class UserDaoSpringImpl implements UserDao{
SimpleJdbcTemplate simple=new SimpleJdbcTemplate(JdbcUtils.getDataSource());
//增加
public void addUser(User user){
String sql="insert into user(name.birthday) vlaues(:name,:birthday)";
SqlParameterSource param=new BeanPropertySqlParameterSource(user);
KeyHolder keyHolder=new GeneratedKeyHolder();
this.simpleJdbcTemplate.getNamedParameterJdbcOperations().update(sql,param,keyHolder);
user.setId(keyHolder.getKey().initValue());
}
//删除
public void delete(User user){
String sql="delete from user where id=?";
this.simpleJdbcTemplate.update(sql,user.getId());
}
//查询
publci User findUser(String loginName,String password){
String sql="select id,name from user where name=?";
return this.simpleJdbcTemplate.queryForObject(sql,ParameterizedBanPropertyRowMapper.newInstance(User.class),loginName);
}
//查询
publci User getUser(int userId){
String sql="select id,name from user where id=?";
return this.simpleJdbcTemplate.queryForObject(sql,ParameterizedBanPropertyRowMapper.newInstance(User.class),userId);
}
//更新
public void update(User user){
String sql="update user set name=?,birthday=? where id=?";
this.simpleJdbcTemplate.update(sql,user.getName(),use.getBirthday(),user.getId())
//或者
String sql="update user set name=:name,birthday=:birthday where id=:id";
this.simpleJdbcTemplate.update(sql,new BeanPropertySqlParameterSource(user));//BeanPropertySqlParameterSource方法中的:id必须和类的属性名一样
}
}
daoconfig.properties文件:
userDaoClass=cn.itcast.jdbc.dao.impl.UserDaoJdbcImpl;
public class DaoFactory{
//DaoFactory.getInstance()方法执行时候,首先会对静态的代码块和属性进行初始化,所以userDao要在instance前面定义,否则userDao为null
private static UserDao userDao=null;
private static DaoFactory instance=new DaoFactory();
private DaoFactory(){
try{
Properties prop=new Properties();
InputStream inStream=DaoFactory.class.getClassLoader().getResourceAsStream("daoconfig.properties");//文件放在classpath下面
prop.load(inStream);
String userDaoClass=prop.getProperty("userDaoClass");
userDao=Class.forName(userDaoClass).newInstance();
}catch(IOException e){
throw new ExceptionInInitializeError(e);
}
}
pubic static DaoFactory getInstance(){
return instance;
}
public UserDao getUserDao(){
return userDao;
}
}
那么使用如下:UserDao userDao=DaoFactory.getInstance().getUserDao();
阅读(5066) | 评论(0) | 转发(0) |