java+servlet+jsp查询所有信息及查询一条记录

时间:2025-02-06 19:35:18

我做的是一个员工信息查询

1.首先在中实例化字段

package ;

import ;
import ;

public class Employee implements Serializable{

	
	private static final long serialVersionUID = -5469025483066661789L;
	private String name;
	private String sex;
	private String birthday;
	private String duty;
	private String address;
	private String number;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		 = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		 = sex;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		 = birthday;
	}
	public String getDuty() {
		return duty;
	}
	public void setDuty(String duty) {
		 = duty;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		 = address;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		 = number;
	}
	
}
2.在EmployeeDao中进行查询,这个方法将在EmployeeServlet中调用,
   //查询所有员工信息
	@SuppressWarnings("finally")
	public List getEmployee(String no) throws Exception {
		    Connection conn = null;
	        PreparedStatement stmt = null;
			List employeeList = new ArrayList();
			try {
				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";    //要执行的SQL
				conn = getConn();
				stmt = (sql);
				ResultSet rs = ();
				while (()) {
					Employee e = new Employee();
					((1));
					((2));
					((3));
					((4));
					((5));
					((6));
					((7));
					((8));
					((9));
					((10));
					(e);
				}
			} catch (SQLException e) {
				();
			} finally {
				if (conn != null) {
					();
				}
				if (stmt != null) {
					();
				}
				return employeeList;
			}		
	}
	/**
	 * 查询一条记录
	 * @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 = getConn();
		stmt = (sql);
		(1, id);
		ResultSet rs = ();
		List employeeOne = new ArrayList();
		while (())
	    {
	      Employee em = new Employee();
	       ((1));
	       ((2));
			((3));
			((4));
			((5));
			((6));
			((7));
			((8));
			((9));
			((10));
			(em);
	    }
		return employeeOne;
	  }


3.在EmployeeServlet中调用Dao中的方法

                /**
        	 * 查询
        	 */
		  EmployeeDao employeeDao = new EmployeeDao();
		    List employeeList;
			try {
				employeeList = ("");  //调用Dao中的方法存到List集合中
		           /*   for(int i = 0; i<();i++) {
		    	            Employee employee = (Employee)(i);  //for循环用来在控制台输出字段,查看是否能获取到
		    	            (());
		          }*/
		    ("employeeList",employeeList);   //通过employeeList传到前台

        	    ("/employee/").forward(request, response);   //重定向,设置跳转页面
			}catch (Exception e) {
			// TODO Auto-generated catch block
			();
		}
		
	} 
       //查询一条记录
		int id=(("id"));
	     /*	(id);*/   //可以在控制台判断是否能获取到id
		EmployeeDao employeeDao = new EmployeeDao();
		try {
			List employeeOne = (id);
		/*	(());*/
			
			("employeeOne",employeeOne);   //通过employeeOne传到前台
	        ("/employee/").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			();
		}

4.查询所有员工信息的数据都放到employeeList中了,接下来将他显示到jsp页面上就可以啦,查询一条记录也是这样哦
<c:forEach items="${employeeList}" var="i" varStatus="status">
	<TBODY>
	  <TR>
	     <TD>${+1}</TD>
	     <TD>${}</TD>
	     <TD>${}</TD>
	     <TD>${}</TD>
	     <TD>${}</TD>
	     <TD>${}</TD>
	     <TD>${}</TD>
	    <TD>${}</TD>
	</c:forEach>