JAva+servlet+jsp接続データベースの削除変更

11481 ワード

1.まず、データベースに接続する方法を含む共通のパッケージを作成します.毎回データベースに接続する必要はありません.
public String url="jdbc:mysql://localhost:3306/rwgl?useSSL=false";
	@SuppressWarnings("unused")
	public String userName="root";//      
	@SuppressWarnings("unused")
	public String pwd="password";//      
protected void initDriver() {//      
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

 
  
protected Connection getConn() {
		//       ,          
		initDriver();
		try {
			conn = DriverManager.getConnection(url, userName, pwd);
		} catch (SQLException e) {

			e.printStackTrace();
		}

		return conn;
	}

2.

package com.systop.rwgl.employee.model;

import java.io.Serializable;
import java.util.Date;

public class Employee implements Serializable{

	
	private static final long serialVersionUID = -5469025483066661789L;
	//     
	private int id;
	private int userid;
	private int deptid;
	private int no;
	private String name;
	private String sex;
	//   date     
        private Date birthday;
	private String duty;
	private String address;
	private String number;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getUserid() {
		return userid;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public int getDeptid() {
		return deptid;
	}
	public void setDeptid(int deptid) {
		this.deptid = deptid;
	}
	
	
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
	public String getDuty() {
		return duty;
	}
	public void setDuty(String duty) {
		this.duty = duty;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public void setBirthday(String parameter) {
		// TODO Auto-generated method stub	
	}
}

3. form ,action , action ,

action="${pageContext.request.contextPath}/employee?action=add"

servlet ,

 
  
package com.systop.rwgl.employee.controller;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.systop.rwgl.employee.dao.EmployeeDao;
import com.systop.rwgl.employee.model.Employee;
import com.systop.rwgl.user.model.User;

public class EmployeeServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	/**
	 * doget        a       ,
	 *   get        post     。
	 * dopost          
	 * 
	 *       
	 */
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		doPost(request,response);
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		
		       String action = request.getParameter("action");
                       //         action    add          
		       if(action != null && action.equals("add")) {
		           EmployeeDao employeedao = new EmployeeDao();
                          //       	
                          System.out.println("?????????????????"+action);
			
			try {
	                  //    employee    ,            ,  employee 
                          Employee employee = new Employee();		
                          //    no  ,int     String   
	                  employee.setNo(Integer.valueOf(request.getParameter("no")));
                          //              name    
	 	          employee.setName(request.getParameter("name"));
		          employee.setSex(request.getParameter("sex"));
		          //            					
                          employee.setBirthday(Date.valueOf(request.getParameter("birth")));
		          employee.setDuty(request.getParameter("zhiwu"));
		          employee.setAddress(request.getParameter("zhuzhi"));
		          employee.setNumber(request.getParameter("number"));
                          //Dao    addEmployee   ,     	        	        
                          employeedao.addEmployee(employee);
	        	  //                 
	        	try {
			    List list = employeedao.getEmployee("");
			    for(int i = 0;i

4. Dao

 
  
package com.systop.rwgl.employee.dao;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
import com.systop.core.dao.BaseDao;
import com.systop.rwgl.employee.model.Employee;
import com.systop.rwgl.user.model.User;

public class EmployeeDao extends BaseDao{
    /**
     *          
     * @param employee
     * @throws Exception
     */
	public void addEmployee(Employee employee) throws Exception{
		String sql = "insert into task_employee (EM_NO,EM_XM,EM_XB,EM_SR,EM_ZW,EM_ZZ,EM_SFZH) values (?,?,?,?,?,?,?)";//   insert  
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = (Connection) getConn();
			stmt = conn.prepareStatement(sql);
			/*System.out.println(sql);*/
			stmt.setInt(1, employee.getNo());// servlet      employee , employee         
			stmt.setString(2, employee.getName());// employee          sql        
			stmt.setString(3, employee.getSex());
			stmt.setDate(4, (Date) employee.getBirthday());
			stmt.setString(5, employee.getDuty());
			stmt.setString(6, employee.getAddress());
			stmt.setString(7, employee.getNumber());
			stmt.execute();
			/*System.out.println(stmt);*/
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
	}
	/**
	 *             ,       
	 * @param deptNo
	 * @return
	 * @throws SQLException
	 */
	public List getEmployee(String deptNo) throws SQLException{	
     //  select   
		String sql = "SELECT ID,EM_NO,EM_XM,EM_XB,EM_SR,EM_ZW,EM_ZZ,EM_SFZH  FROM task_employee";
		Connection conn = null;
		PreparedStatement stmt = null;
		List employeeList = new ArrayList();
		try {
			conn = (Connection) getConn();
			stmt = conn.prepareStatement(sql);
			
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				Employee u = new Employee();
				u.setId(rs.getInt(1));
				u.setNo(rs.getInt(2));
				u.setName(rs.getString(3));
				u.setSex(rs.getString(4));
				u.setBirthday(rs.getDate(5));
				u.setDuty(rs.getString(6));
				u.setAddress(rs.getString(7));
				u.setNumber(rs.getString(8));
				
				employeeList.add(u);	
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
		return employeeList;
	}
	/**
	 *   id          
	 * @param employee
	 * @throws SQLException
	 */
	public void deleteEmployee(Employee employee) throws SQLException {
		String sql="delete from task_employee where id=?";//delete sql  
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = (Connection) getConn();
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, employee.getId());//     id  ,    id      
			
			stmt.execute();		
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
	}
	/**
	 *       
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public List getOne(int id) throws SQLException
	  {
	    String sql = " select ID,TASK_USER_ID,TASK_DEPT_ID,EM_NO,EM_XM,EM_XB,EM_SR,EM_ZW,EM_ZZ,EM_SFZH from task_employee where ID=?";
	    Connection conn = null;
      PreparedStatement stmt = null;
      conn = (Connection) getConn();
		stmt = conn.prepareStatement(sql);
		stmt.setInt(1, id);
		ResultSet rs = stmt.executeQuery();
		List employeeOne = new ArrayList();
		while (rs.next())
	    {
	      Employee em = new Employee();
	       em.setId(rs.getInt(1));
	       em.setUserid(rs.getInt(2));
			em.setDeptid(rs.getInt(3));
			em.setNo(rs.getInt(4));
			em.setName(rs.getString(5));
			em.setSex(rs.getString(6));
			em.setBirthday(rs.getDate(7));
			em.setDuty(rs.getString(8));
			em.setAddress(rs.getString(9));
			em.setNumber(rs.getString(10));
			employeeOne.add(em);
	    }
		return employeeOne;
	  }
	/**
	 *     
	 * @param employee
	 * @throws SQLException
	 */
	public void updateEmployee(Employee employee) throws SQLException{
		String sql = "update task_employee set EM_NO=?,EM_XM=?,EM_XB=?,EM_SR=?,EM_ZW=?,EM_ZZ=?,EM_SFZH=? where ID=?";//update sql              ,   update   
		Connection conn = null;
	    PreparedStatement stmt = null;
	    try {
			conn = (Connection) getConn();
			stmt = conn.prepareStatement(sql);
			/*System.out.println(sql);*/
			stmt.setInt(1, employee.getNo());
			stmt.setString(2, employee.getName());
			stmt.setString(3, employee.getSex());
			stmt.setDate(4, (Date) employee.getBirthday());
			stmt.setString(5, employee.getDuty());
			stmt.setString(6, employee.getAddress());
			stmt.setString(7, employee.getNumber());
			stmt.setInt(8, employee.getId());
			stmt.execute();
			/*System.out.println(stmt);*/
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
			if (stmt != null) {
				stmt.close();
			}
		}
	}
}