建立一个dao接口,建立一个dao实现类
public interface CustomerDao {
//插入方法,传一个Customer对象进去
public void insert(Customer custname);
//根据名字查询
public Customer selectByName(String custname);
//根据用户名和密码查询
public Customer selectByNamePwd(String custname,String pwd);
//查询全部
List<Customer> selectAll();
}
建立一个dao实现类
public class CustomerDAOImpl implements CustomerDao{
public void insert(Customer cust) {
Connection conn = JDBCConnectionFactory.getConnection();
String sql = "insert into customer values(?,?,?,?)";
try {
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1, cust.getCustname());
ps.setString(2, cust.getPwd());
ps.setInt(3,cust.getAge());
ps.setString(4,cust.getAddress());
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Customer selectByName(String custname) {
Customer cust = null;
Connection conn = JDBCConnectionFactory.getConnection();
String sql = "select * from customer where custname=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, custname);
ResultSet rs = ps.executeQuery();
if(rs.next()){
cust = new Customer(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return cust;
}
public Customer selectByNamePwd(String custname, String pwd) {
// TODO Auto-generated method stub
Customer cust = null;
Connection conn = JDBCConnectionFactory.getConnection();
String sql = "select * from customer where custname=? and pwd = ?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, custname);
ps.setString(2, pwd);
ResultSet rs = ps.executeQuery();
if(rs.next()){
cust = new Customer(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return cust;
}
public List<Customer> selectAll() {
List<Customer> list = new ArrayList<Customer>();
Connection conn = JDBCConnectionFactory.getConnection();
String sql = "select custname,age,address from customer";
try {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
list.add(new Customer(rs.getString(1),null,rs.getInt(2),rs.getString(3)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
}
建立一个service接口
public interface CustomerService {
//登录
public boolean login(String custname,String pwd);
//注册
public boolean register(Customer cust)throws RegisterException;
//查看个人信息
public Customer viewPersonal(String custname);
public List<Customer> viewAll();
}
建立一个service实现类
public class CustomerServiceImpl implements CustomerService{
private CustomerDao dao;
public boolean login(String custname, String pwd) {
Customer cust = dao.selectByNamePwd(custname, pwd);
if(cust!=null){
return true;
}else{
return false;
}
// TODO Auto-generated method stub
}
public boolean register(Customer cust) throws RegisterException {
// TODO Auto-generated method stub
Customer c = dao.selectByName(cust.getCustname());
if(c==null){
dao.insert(cust);
return true;
}
return false;
}
public Customer viewPersonal(String custname) {
// TODO Auto-generated method stub
return dao.selectByName(custname);
}
public List<Customer> viewAll() {
// TODO Auto-generated method stub
return dao.selectAll();
}
}
service层调用dao层接口
dao接口写方法,接口实现类 继承接口,写具体实现方法
service接口写方法,接口实现类的方法中调用dao层方法