javaEE mvc例子详解(一)

2014-11-23 19:41:10 · 作者: · 浏览: 94

\


一个不错的例子值得细细品味:

下面按照包顺序将代码贴出来供大家参考:

IEmpDAO

package org.lzch.dao;

import java.util.List;

import org.lzch.vo.Emp;

public interface IEmpDAO {
	public boolean doCreate(Emp emp)throws Exception;
	public boolean doUpdate(Emp emp)throws Exception;
	public boolean doDelete(int empno)throws Exception;
	public List findAll(int currentPage,int lineSize,String keyword)throws Exception;
	public int getAllCount(String keyword)throws Exception;
	public Emp findEmpById(int empno)throws Exception;
	public boolean getAllEname(String ename)throws Exception;
}

EmpDAOImpl

package org.lzch.dao.impl;

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

import org.lzch.dao.IEmpDAO;
import org.lzch.vo.Emp;

public class EmpDAOImpl implements IEmpDAO {
	private Connection conn=null;
	public EmpDAOImpl(Connection conn){
		this.conn=conn;
	}
	//添加用户信息
	public boolean doCreate(Emp emp) throws Exception {
		// TODO Auto-generated method stub
		boolean flag=false;
		PreparedStatement pstmt=null;
		try{
			this.conn.setAutoCommit(false);		//手动提交
			String sql="INSERT INTO emp1(empno,ename,job,hiredate,sal,comm,photo)VALUES( , , , , , , )";
			pstmt=this.conn.prepareStatement(sql);
			pstmt.setInt(1, emp.getEmpno());
			pstmt.setString(2, emp.getEname());
			pstmt.setString(3, emp.getJob());
			pstmt.setDate(4, new java.sql.Date(emp.getHiredate().getTime()));
			pstmt.setFloat(5, emp.getSal());
			pstmt.setFloat(6, emp.getComm());
			pstmt.setString(7, emp.getPhoto());
			int count=pstmt.executeUpdate();
			this.conn.commit();		//提交
			if(count>0){
				flag=true;
			}
		}catch(Exception e){
			this.conn.rollback();
		}finally{
			try{
				pstmt.close();
			}catch(Exception e){
				throw e;
			}
		}
		return flag;
	}
	//删除
	public boolean doDelete(int empno) throws Exception {
		// TODO Auto-generated method stub
		boolean flag=false;
		PreparedStatement pstmt=null;
		try{
			String sql="DELETE FROM emp1 WHERE empno= ";
			pstmt=this.conn.prepareStatement(sql);
			pstmt.setInt(1, empno);
			int count=pstmt.executeUpdate();
			if(count>0){
				flag=true;
			}
		}catch(Exception e){
			throw e;
		}finally{
			try{
				pstmt.close();
			}catch(Exception e){
				throw e;
			}
		}
		return flag;
	}
	//更新
	public boolean doUpdate(Emp emp) throws Exception {
		// TODO Auto-generated method stub
		boolean flag=false;
		PreparedStatement pstmt=null;
		try{
			String sql="UPDATE emp1 SET ename= ,job= ,hiredate= ,sal= ,comm= ,photo=  WHERE empno= ";
			pstmt=this.conn.prepareStatement(sql);
			pstmt.setString(1, emp.getEname());
			pstmt.setString(2, emp.getJob());
			pstmt.setDate(3, new java.sql.Date(emp.getHiredate().getTime()));
			pstmt.setFloat(4, emp.getSal());
			pstmt.setFloat(5, emp.getComm());
			pstmt.setString(6, emp.getPhoto());
			pstmt.setInt(7, emp.getEmpno());
			int count=pstmt.executeUpdate();
			System.out.println("emp_UPDATE_SQL==="+sql);
			if(count>0){
				flag=true;
			}
		}catch(Exception e){
			throw e;
		}finally{
			try{
				pstmt.close();
			}catch(Exception e){
				throw e;
			}
		}
		return flag;