DAO模式

主要是新建一个interface,然后实例化这个interface,然后对表内的数据封装。最后一个test,四个java如下


customer.java

package my_sql_test;

public class Customer {

	//table == customer
	private int id;
	private String name;
	private String email;
	
	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 String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String toString(){
		return id+":"+name+":"+email;
	}
}


customerDao.java
package my_sql_test;

import java.util.List;

public interface CustomerDao {

	public void add(Customer C);
	public void update(Customer C);
	public void delete(int id);
	public Custome getCustomerById(int id);
	public List<Customer> query();
	
	//CRUD
}

customerDaoImpl.java

package my_sql_test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CustomerDaoImpl implements CustomerDao {

	@Override
	public void add(Customer C) {
		// TODO Auto-generated method stub
		String sql="insert into customer (name,email)values (?,?)";
		Connection conn=DBUtil.open();
		try {
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, C.getName());
			pstmt.setString(2, C.getEmail());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(conn);
		}
		
	}

	@Override
	public void update(Customer C) {
		// TODO Auto-generated method stub
		
		String sql="update customer set name=?,email=? where id=?";
		Connection conn=DBUtil.open();
		try {
			PreparedStatement pstmt=conn.prepareStatement(sql);
			
			pstmt.setString(1, C.getName());
			pstmt.setString(2, C.getEmail());
			pstmt.setInt(3, C.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(conn);
		}
		
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
		String sql="delete from customer where id=?";
		Connection conn=DBUtil.open();
		try {
			PreparedStatement pstmt=conn.prepareStatement(sql);
			
			pstmt.setInt(1, id);
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(conn);
		}
		
	}

	@Override
	public Custome getCustomerById(int id) {
		// TODO Auto-generated method stub
		
		String sql="select id,name,email from customer where id=?";
		Connection conn=DBUtil.open();
		try {
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1,id);
			ResultSet rs=pstmt.executeQuery();
			if(rs.next()){
				String name=rs.getString(2);
				String email=rs.getString(3);
				Customer C=new Custome();
				C.setId(id);
				C.setName(name);
				C.setEmail(email);
				return C;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(conn);
		}
		return null;
}

	@Override
	public List<Customer> query() {
		// TODO Auto-generated method stub
		String sql="select * from customer";
		Connection conn=DBUtil.open();
		try {
			Statement stmt=conn.createStatement();

			List<Customer> list=new ArrayList<Custome>();
			ResultSet rs=stmt.executeQuery(sql);
			while(rs.next()){
				int id=rs.getInt(1);
				String name=rs.getString(2);
				String email=rs.getString(3);
				Customer C=new Custome();
				C.setId(id);
				C.setName(name);
				C.setEmail(email);
				list.add(C);
			}
			return list;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(conn);
		}
		return null;
	}

}

testDao.java


package my_sql_test;

import java.util.List;

public class testDao {

	public static void main(String[] args){
		CustomerDao dao=new CustomerDaoImpl();
		Customer C=new Customer();
		
		Customer c=dao.getCustomerById(2);
		System.out.println(c);
		
	/*	List<Customer> list=dao.query();
		System.out.println(list);
		*/
	//	dao.delete(1);
		
		/*C.setId(1);
		C.setName("i");
		C.setEmail("ii");
		dao.update(C);*/
		
		/*C.setName("tree");
		C.setEmail("itree");
		dao.add(C);*/
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值