DAO数据访问对象、构建

DAO:Data Access Objects 是针对应用程序访问数据库的统一编程接口

构建DAO层
1.构建模型:创建表。(在数据库层面)
2.构建实体类(JavaBean):实质就是创建对象。构建包路径时,命名规范:domain、vo、ao等等
  特点:
	   (1)无参的构造器。
	   (2)相对应的属性信息。
	   (3)提供相对应的getter/setter方法。
	   (4)必须提供可序列化。
3.构建DAO层接口:IXXXDAO.class
    接口:DAO层接口只是提供对实体类的操作方法。
    比如:queryAll(),queryById(int id),insert(User u),update(int id),delete(int id)
4.构建DAO层实现类:IXXXDAOImpl.class
    实现DAO层接口定义的方法。
5.应用程序调用DAO层接口
案例
package com.dragon.usermanager.domain;
import java.io.Serializable;

public class User implements Serializable{
     
	private static final long serialVersionUID = 1L;
	
	private int uid;//在创建时要跟数据库中的表相对应
	private String uname;
	private String upad;
	private int uage;
	private String usex;
	public int getUid() {
     
		return uid;
	}
	public void setUid(int uid) {
     
		this.uid = uid;
	}
	public String getUname() {
     
		return uname;
	}
	public void setUname(String uname) {
     
		this.uname = uname;
	}
	public String getUpad() {
     
		return upad;
	}
	public void setUpad(String upad) {
     
		this.upad = upad;
	}
	public int getUage() {
     
		return uage;
	}
	public void setUage(int uage) {
     
		this.uage = uage;
	}
	public String getUsex() {
     
		return usex;
	}
	public void setUsex(String usex) {
     
		this.usex = usex;
	}		
}
package com.dragon.usermanager.dao;
import java.util.List;
import com.dragon.usermanager.domain.User;

public interface IUserDao {
     
	public List<User> queryAll();//将查询的值都放到集合中
	public boolean insert(User u);
	public void delete(int uid);
}
package com.dragon.usermanager.daoimpl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.dragon.usermanager.dao.IUserDao;
import com.dragon.usermanager.domain.User;

import jdbc.DBUtil;

/**
 * @author Dragon
 *
 */
public class IUserDaoImpl implements IUserDao{
     

	@Override
	public List<User> queryAll() {
     
		
		List<User> userlist = new ArrayList<User>();
		
		try {
     
			Connection conn =DBUtil.getConnection();//创建连接
			Statement st=conn.createStatement();	//创建句柄
			String sql ="select * from user";		//编写SQL语句
			ResultSet rs =st.executeQuery(sql);		//执行,返回结果集
			while(rs.next()){
     
				User u = new User();
				u.setUid(rs.getInt("uid"));
				u.setUname(rs.getString("uname"));
				u.setUpad(rs.getString("upwd"));
				u.setUage(rs.getInt("uage"));
				u.setUsex(rs.getString("usex"));
				userlist.add(u);
			}
		} catch (SQLException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		return userlist;
	}

	@Override
	public boolean insert(User u) {
     
		boolean b = false;
		try {
     
			Connection conn = DBUtil.getConnection();//创建连接
			Statement st = conn.createStatement();//创建句柄
			String sql = "insert into user values (null,'"+u.getUname()+"','"+u.getUpad()+"',"+u.getUage()+",'"+u.getUsex()+"')";
		    System.out.println(sql);
		    b = st.execute(sql);	
		    
		} catch (SQLException e) {
     
			
			e.printStackTrace();
		}
		return b;
	}

	@Override
	public void delete(int uid) {
     
		try {
     
			Connection conn = DBUtil.getConnection();
			Statement st = conn.createStatement();
			String sql="delete from user where uname='zhangsan'";
			int i = st.executeUpdate(sql);
			System.out.println(i);		
			
		} catch (SQLException e) {
     
			e.printStackTrace();
		}			
	}	
}
package com.dragon.usermanager.test;
import java.util.List;
import com.dragon.usermanager.dao.IUserDao;
import com.dragon.usermanager.daoimpl.IUserDaoImpl;
import com.dragon.usermanager.domain.User;

public class Test {
     

	public static void main(String[] args) {
     
		User u =new User();
		u.setUname("zhaoliu");
		u.setUpad("1212345");
		u.setUage(15);
		u.setUsex("t");
		IUserDao userdao =new IUserDaoImpl();
		userdao.insert(u);			//插入数据
		
		List<User> user1 = userdao.queryAll();
		for (int i = 0; i < user1.size(); i++) {
     
			System.out.println(user1.get(i).getUname());
		}
		
		userdao.delete(3);			//删除数据
	}

}

你可能感兴趣的:(JDBC,DAO,数据库连接池)