个人感觉这个DAO实例写的还可以吧??呵呵

package org.tarena.shopping.dao;

import java.util.List;

import org.tarena.shopping.bean.Computer;

public interface ComputerDAO {
   
	public Computer findById(long id) throws Exception;
	
	public void update(long id,Computer cp) throws Exception;
	
	public void add(Computer cp) throws Exception;
	
	public void delete(long id) throws Exception;
	
	public List<Computer> findAll() throws Exception;
}



实现类:哎注释太少,没办法写多了感觉就没意思了

package org.tarena.shopping.dao.impl;

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

import org.tarena.shopping.bean.Computer;
import org.tarena.shopping.dao.ComputerDAO;
import org.tarena.shopping.util.DBUtil;
import org.tarena.shopping.util.FactoryUtil;

public class ComputerDAOImpl implements ComputerDAO {

	private Computer cp;
	private Connection conn;
	private PreparedStatement ps;
	
	public void add(Computer cp) throws Exception {

		conn = DBUtil.getConnection();
		String sql = "insert into stu_computer_22(name,description,pic,price) value(?,?,?,?)";
		ps =  conn.prepareStatement(sql);
		ps.setString(1, cp.getName());
		ps.setString(2, cp.getDescription());
		ps.setString(3, cp.getPic());
		ps.setDouble(4, cp.getPrice());
		ps.executeUpdate();
		
		DBUtil.closed(ps,conn);
	}

	public void delete(long id) throws Exception {

		conn = DBUtil.getConnection();
		String sql = "delete from stu_computer_22 where id=?";
		ps = conn.prepareStatement(sql);
		ps.setLong(1, id);
		ps.executeUpdate();
		
		DBUtil.closed(ps, conn);
	}

	public List<Computer> findAll() throws Exception {
		
		List<Computer> list = new ArrayList<Computer>();
		
		conn = DBUtil.getConnection();
		String sql = "select * from stu_computer_22 order by id asc";
		ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			cp = new Computer();
			cp.setId(rs.getLong("id"));
			cp.setName(rs.getString("name"));
			cp.setDescription(rs.getString("description"));
			cp.setPic(rs.getString("pic"));
			cp.setPrice(rs.getDouble("price"));
			list.add(cp);
		}
		rs.close();
		DBUtil.closed(ps, conn);
		return list;
	}

	public Computer findById(long id) throws Exception {

		conn = DBUtil.getConnection();
		String sql = "select * from stu_computer_22 where id=?";
		ps = conn.prepareStatement(sql);
		ps.setLong(1, id);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			cp = new Computer();
			cp.setId(rs.getLong("id"));
			cp.setName(rs.getString("name"));
			cp.setDescription(rs.getString("description"));
			cp.setPic(rs.getString("pic"));
			cp.setPrice(rs.getDouble("price"));
		}
		rs.close();
		DBUtil.closed(ps, conn);
		return cp;
	}

	public void update(long id,Computer cpt) throws Exception {

		//如果不是数据库中查询得到的computer对象,那么就直接返回;
		if(id!=cpt.getId()) return;
		conn = DBUtil.getConnection();
		String sql = "update stu_computer_22 set name=?,description=?,pic=?,price=? where id=?";
		ps = conn.prepareStatement(sql);
		ps.setString(1, cpt.getName());
		ps.setString(2, cpt.getDescription());
		ps.setString(3, cpt.getPic());
		ps.setDouble(4, cpt.getPrice());
		ps.setLong(5, id);
		ps.executeUpdate();
		
		DBUtil.closed(ps, conn);
	}
       //测试的练习,测试还行都没问题,最后显示正常。
	public static void main(String[] args) throws Exception {
		ComputerDAO com = (ComputerDAO) FactoryUtil.getDAOInstance(ComputerDAO.class);
		List<Computer> list = com.findAll();
		
	
		for (Computer computer : list) {
			System.out.println(computer.getId()+",\t"
					+computer.getName()+",\t"
					+computer.getDescription()+",\t"
					+computer.getPic()+",\t"
					+computer.getPrice());
		}
	}
	
}

你可能感兴趣的:(java,DAO,sql,bean)