81,114
社区成员
发帖
与我相关
我的任务
分享use info;
create table employee (
id int not null primary key AUTO_INCREMENT,
name char(20) not null,
wkno char(20) not null,
department char(50),
birthday datetime ,
salary decimal(7,2)
)/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.fuyou.struts.form;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 04-10-2008
*
* XDoclet definition:
* @struts.form name="regForm"
*/
public class RegForm extends ActionForm {
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
private String name;
private String wkno;
private String department;
private String birthday;
private double salary = 0.0;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWkno() {
return wkno;
}
public void setWkno(String wkno) {
this.wkno = wkno;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
}:/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.fuyou.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.fuyou.beans.Employee;
import com.fuyou.dao.EmployeeDao;
import com.fuyou.struts.form.RegForm;
/**
* MyEclipse Struts
* Creation date: 04-10-2008
*
* XDoclet definition:
* @struts.action path="/reg" name="regForm" input="/reg.jsp" scope="request" validate="true"
*/
public class RegAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String name;
String wkno;
String department;
String date;
double salary = 0.0;
RegForm regForm = (RegForm) form;
name = regForm.getName();
wkno = regForm.getWkno();
department = regForm.getDepartment();
date = regForm.getBirthday();
salary = regForm.getSalary();
java.sql.Date birthday = java.sql.Date.valueOf(date);
Employee employee = new Employee(name,wkno,department,birthday,salary);
EmployeeDao employeeDao = new EmployeeDao();
employeeDao.save(employee);
return mapping.findForward("success");
}
}/**
* 员工的javabean
*/
package com.fuyou.beans;
import java.sql.Date;
/**
* @author fuyubao
*/
public class Employee {
private int id ;
private String name;
private String wkno;
private String department;
private Date birthday;
private double salary = 0.0;
public Employee() {
}
public Employee( String name, String wkno, String department,
Date birthday, double salary) {
this.name = name;
this.wkno = wkno;
this.department = department;
this.birthday = birthday;
this.salary = salary;
}
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 getWkno() {
return wkno;
}
public void setWkno(String wkno) {
this.wkno = wkno;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
/**
*
*/
package com.fuyou.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.fuyou.beans.Employee;
import com.fuyou.utis.DBUtils;
/**
* @author fuyubao 操作员工信息的DAO类
*/
public class EmployeeDao {
private List<Employee> list = null;
private DBUtils dbUtils = null;
public EmployeeDao() {
dbUtils = new DBUtils();
list = new ArrayList<Employee>();
}
/*
* @return返回所有的员工信息
*/
public List<Employee> getAll() {
Connection conn = null;
Statement stmt = null;
String sql = "select * from employee";
// System.out.println(sql);
conn = dbUtils.getConnection();
ResultSet rs = null;
Employee employee = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs != null && rs.next()) {
employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setWkno(rs.getString("wkno"));
employee.setDepartment(rs.getString("Department"));
employee.setBirthday(rs.getDate("birthday"));
employee.setSalary(rs.getDouble("salary"));
list.add(employee);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closeResult(rs);
this.dbUtils.closeStatemnet(stmt);
this.dbUtils.closeConnection(conn);
}
return list;
}
/*
* @return返回与ID关联的员工信息 @param id根据ID回
*/
public Employee getOne(int id) {
Connection conn = null;
Statement stmt = null;
String sql = "select * from employee where id = '" + id+"'";
// System.out.println(sql);
conn = dbUtils.getConnection();
ResultSet rs = null;
Employee employee = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs != null && rs.next()) {
employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setWkno(rs.getString("wkno"));
employee.setDepartment(rs.getString("Department"));
employee.setBirthday(rs.getDate("birthday"));
employee.setSalary(rs.getDouble("salary"));
System.out.println("getOne");
return employee;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closeResult(rs);
this.dbUtils.closeStatemnet(stmt);
this.dbUtils.closeConnection(conn);
}
return null;
}
/*
* @param employee参数是员工信息 增加新一个名员工
*/
public void save(Employee employee) {
Connection conn = null;
PreparedStatement preStmt = null;
String sql = "insert into employee values(?,?,?,?,?)";
conn = dbUtils.getConnection();
try {
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, employee.getName());
preStmt.setString(2, employee.getWkno());
preStmt.setString(3, employee.getDepartment());
preStmt.setDate(4, (Date) employee.getBirthday());
preStmt.setDouble(5, employee.getSalary());
preStmt.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closePreStmt(preStmt);
this.dbUtils.closeConnection(conn);
}
}
/*
* @param id根据ID主键更新数据 ,@return 更新成功,返回true,否则返回false
*/
public boolean update(Employee employee, int id) {
Connection conn = null;
PreparedStatement preStmt = null;
String sql = "update employee set name=?,wkno=?,department=?,birthday=?,salary=? where id='"
+ id + "'";
conn = dbUtils.getConnection();
try {
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, employee.getName());
preStmt.setString(2, employee.getWkno());
preStmt.setString(3, employee.getDepartment());
preStmt.setDate(4, (Date) employee.getBirthday());
preStmt.setDouble(5, employee.getSalary());
preStmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closePreStmt(preStmt);
this.dbUtils.closeConnection(conn);
}
return false;
}
/*
* @param id根据ID主键删除数据, @return 删除成功,返回true,否则返回false
*/
public boolean delete(int id) {
Connection conn = null;
PreparedStatement preStmt = null;
String sql = "delete from employee where id = '" + id + " '";
conn = dbUtils.getConnection();
try {
preStmt = conn.prepareStatement(sql);
preStmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closePreStmt(preStmt);
this.dbUtils.closeConnection(conn);
}
return false;
}
}