本文主要是介绍JAVA WEB 使用dao方式实现增加雇员、修改雇员信息,显示雇员图片等功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
整个项目的目录架构如下:
废话不多说,直接上代码:
Emp.java
package dao;
import java.util.Date;import org.eclipse.jdt.internal.compiler.ast.ThisReference;import com.mysql.jdbc.Blob;public class Emp {private int empno;private String ename;private String job;private Date hiredate;private float sal;private float comm;private String image;public int getEmpno() {return empno;}public void setEmpno(int empno) {this.empno = empno;}public String getEname() {return ename;}public void setEname(String ename) {this.ename = ename;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}public Date getHiredate() {return hiredate;}public void setHiredate(Date hiredate) {this.hiredate = hiredate;}public float getSal() {return sal;}public void setSal(float sal) {this.sal = sal;}public float getComm() {return comm;}public void setComm(float comm) {this.comm = comm;}public String getImage() {return image;}public void setImage(String image) {this.image=image;}}
DatabaseConnection.java
package dao;
import java.sql.Connection;
import java.sql.DriverManager;//定义数据库连接类
public class DatabaseConnection {public static final String DBDRIVER = "com.mysql.jdbc.Driver";public static final String DBURL = "jdbc:mysql://localhost:3306/demo";public static final String DBUSER = "root";public static final String DBPASS = "root";private Connection conn = null;public DatabaseConnection() throws Exception {Class.forName(DBDRIVER);conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);}public Connection getConnection() {return this.conn;}public void close() throws Exception {if (this.conn != null) {try {this.conn.close();} catch (Exception e) {throw e;}}}
}
IEmpDAO.java
package dao;
//定义数据库操作的接口import java.util.List;public interface IEmpDAO {public boolean doCreate(Emp emp) throws Exception;public List<Emp> findALL(String keyWord) throws Exception;public Emp findByid(int empno) throws Exception;public boolean doUpdate(Emp emp) throws Exception;}
EmpDAOImpl.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;public class EmpDAOImpl implements IEmpDAO {private Connection conn = null;private PreparedStatement pstmt = null;public EmpDAOImpl(Connection conn) {this.conn = conn;}public boolean doCreate(Emp emp) throws Exception {boolean flag = false;String sql = "insert into emp(empno,ename,job,hiredate,sal,comm,image) values(?,?,?,?,?,?,?)";this.pstmt = this.conn.prepareStatement(sql);this.pstmt.setInt(1, emp.getEmpno());this.pstmt.setString(2, emp.getEname());this.pstmt.setString(3, emp.getJob());this.pstmt.setDate(4, new java.sql.Date(emp.getHiredate().getTime()));this.pstmt.setFloat(5, emp.getSal());this.pstmt.setFloat(6, emp.getComm());this.pstmt.setString(7, emp.getImage());if (this.pstmt.executeUpdate() > 0) {flag = true;}this.pstmt.close();return flag;}public List<Emp> findALL(String keyWord) throws Exception {List<Emp> all = new ArrayList<Emp>();String sql = "select empno,ename,job,hiredate,sal,comm,image From emp where ename like ? or job like ?";this.pstmt=this.conn.prepareStatement(sql);this.pstmt.setString(1, "%" + keyWord + "%");this.pstmt.setString(2, "%" + keyWord + "%");ResultSet rs = this.pstmt.executeQuery();Emp emp = null;while (rs.next()) {emp = new Emp();emp.setEmpno(rs.getInt(1));emp.setEname(rs.getString(2));emp.setJob(rs.getString(3));emp.setHiredate(rs.getDate(4));emp.setSal(rs.getFloat(5));emp.setComm(rs.getFloat(6));emp.setImage(rs.getString(7));all.add(emp);}this.pstmt.close();return all;}public Emp findByid(int empno) throws Exception {Emp emp = null;String sql = "select empno,ename,job,hiredate,sal,comm,image from emp where empno=?";this.pstmt = this.conn.prepareStatement(sql);this.pstmt.setInt(1, empno);ResultSet rs = this.pstmt.executeQuery();if (rs.next()) {emp = new Emp();emp.setEmpno((rs.getInt(1)));emp.setEname(rs.getString(2));emp.setJob(rs.getString(3));emp.setHiredate(rs.getDate(4));emp.setSal(rs.getFloat(5));emp.setComm(rs.getFloat(6));emp.setImage(rs.getString(7));}this.pstmt.close();return emp;}public boolean doUpdate(Emp emp) throws Exception {boolean flag = false;String sql = "update emp set ename=?,job=?,hiredate=?,sal=?,comm=?,image=? where empno=?";this.pstmt = this.conn.prepareStatement(sql);this.pstmt.setString(1, emp.getEname());this.pstmt.setString(2, emp.getJob
这篇关于JAVA WEB 使用dao方式实现增加雇员、修改雇员信息,显示雇员图片等功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!