一、项目前准备工作
1、eclipse 或者myeclipse 都可以
2、数据库(Oracle或者mysql或者其他也可以)
3、了解Javaweb相关知识
- web层(action):包含JSP和Servlet等与web相关的内容,负责与浏览器的响应和请求;
- 业务层(service):只关心业务逻辑;
- 数据层(dao):封装了对数据库的访问细节,数据操作类;
注意三层的关系:web层依赖业务层,业务层依赖数据层
二、不说多少,直接上代码,注意我标了一些注意点,是我容易犯错的地方,大家也要多注意。
- 连接数据库
在dao下新建一个DBConn类用来处理对数据进行连接。 我使用的是Oracle数据库
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
private static final String USER = "user";
private static final String PASSWORD = "passwd";
private Connection conn;
public DBConnection() throws Exception{
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL,USER,PASSWORD);
}
public Connection getConnection() {
return this.conn;
}
public void close() throws Exception{
if(this.conn!=null) {
this.conn.close();
}
}
}
- 建立你需要使用到的实体类(实体类中的对象对应数据库表中的字段数据)
public class User {
private int empno;
private String ename;
private String job;
private String mgr;
private String sal;
private String comm;
private String deptno;
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 String getMgr() {
return mgr;
}
public void setMgr(String mgr) {
this.mgr = mgr;
}
public String getSal() {
return sal;
}
public void setSal(String sal) {
this.sal = sal;
}
public String getComm() {
return comm;
}
public void setComm(String comm) {
this.comm = comm;
}
public String getDeptno() {
return deptno;
}
public void setDeptno(String deptno) {
this.deptno = deptno;
}
}
- 实现增删改查(先创建接口UserDao,再使用UserDaoImpl实现接口里的方法)
UserDao接口:
import java.util.List;
import com.dbs.user.User;
public interface UserDao {
//数据库新增数据
boolean addUser(User user) throws Exception;
boolean delete(int empno) throws Exception;
List<User> selectAll() throws Exception;
boolean update(User user) throws Exception;
User selectone(int empno) throws Exception;
}
UserDaoImpl类(实现增删改查的方法):
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.dbs.db.DBConnection;
import com.dbs.user.User;
public class UserDaoImpl implements UserDao{
private Connection conn;
private PreparedStatement pstmt;
public UserDaoImpl() throws Exception{
this.conn = new DBConnection().getConnection();
}
//添加数据
@Override
public boolean addUser(User user) throws Exception {
boolean flag = false;
String sql="insert into emp(empno,ename,job,deptno) values(?,?,?,?)";
this.pstmt = this.conn.prepareStatement(sql);
pstmt.setInt(1, user.getEmpno());
pstmt.setString(2, user.getEname());
pstmt.setString(3, user.getJob());
pstmt.setInt(4, Integer.valueOf(user.getDeptno()));
if(this.pstmt.executeUpdate()>0) {
flag = true;
}
pstmt.close();
conn.close();
return flag;
}
//删除数据
@Override
public boolean delete(int empno) throws Exception {
boolean flag = false;
String sql="delete from emp where empno=?";
this.pstmt = this.conn.prepareStatement(sql);
pstmt.setInt(1, empno);
if(this.pstmt.executeUpdate()>0) {
flag = true;
}
pstmt.close();
conn.close();
return flag;
}
//查询所有数据
@Override
public List<User> selectAll() throws Exception {
String sql = "select * from emp order by empno desc";
this.pstmt = this.conn.prepareStatement(sql);
ResultSet rs1 = pstmt.executeQuery();
List<User> rsList= new ArrayList<User>();
User user1=null;
while(rs1.next()){
user1 = new User();
user1.setEmpno(Integer.valueOf(rs1.getString(1)));
user1.setEname(rs1.getString(2));
user1.setJob(rs1.getString(3));
user1.setMgr(rs1.getString(4));
user1.setSal(rs1.getString(6));
user1.setComm(rs1.getString(7));
user1.setDeptno(rs1.getString(8));
rsList.add(user1);
}
pstmt.close();
conn.close();
return rsList;
}
//更新一条数据
@Override
public boolean update(User user) throws Exception {
boolean flag = false;
String sql="update emp set ename=?,job=?,mgr=?,sal=?,comm=?,deptno=? where empno=?";
this.pstmt = this.conn.prepareStatement(sql);
pstmt.setString(1, user.getEname());
pstmt.setString(2, user.getJob());
pstmt.setInt(3, Integer.valueOf(user.getMgr()));
pstmt.setInt(4, Integer.valueOf(user.getSal()));
pstmt.setInt(5, Integer.valueOf(user.getComm()));
pstmt.setInt(6, Integer.valueOf(user.getDeptno()));
pstmt.setInt(7, user.getEmpno());
if(this.pstmt.executeUpdate()>0) {
flag = true;
}
pstmt.close();
conn.close();
return flag;
}
//查找一条数据
@Override
public User selectone(int empno) throws Exception {
User user1 = new User();
String sql="select * from emp where empno=?";
this.pstmt = this.conn.prepareStatement(sql);
pstmt.setInt(1, empno);
ResultSet rs1 = pstmt.executeQuery();
//为了匹配显示,其实返回一个对象即可
while(rs1.next()){
user1.setEmpno(Integer.valueOf(rs1.getString(1)));
user1.setEname(rs1.getString(2));
user1.setJob(rs1.getString(3));
user1.setMgr(rs1.getString(4));
user1.setSal(rs1.getString(6));
user1.setComm(rs1.getString(7));
user1.setDeptno(rs1.getString(8));
}
pstmt.close();
conn.close();
return user1;
}
}
- 创建UserService接口,并创建UserServiceImpl类来在构造方法中实例化DAO对象,并使用DAO中的方法实现UserService类中的方法。
UserService接口:
import java.util.List;
import com.dbs.user.User;
public interface UserService {
boolean addServiceUser(User user) throws Exception;
List<User> selectAllServiceUser() throws Exception;
boolean deleteServiceUser(int empno) throws Exception;
boolean updateServiceUser(User user) throws Exception;
User selectoneServiceUser(int empno) throws Exception;
}
UserServiceImpl类:
import java.util.List;
import com.dbs.dao.UserDao;
import com.dbs.dao.UserDaoImpl;
import com.dbs.user.User;
public class UserServiceImpl implements UserService{
private UserDao dao;
/**
* 构造方法中
* 实例化DAO对象
*
*/
public UserServiceImpl() throws Exception{
dao = new UserDaoImpl();
}
@Override
public boolean addServiceUser(User user) throws Exception {
return this.dao.addUser(user);
}
@Override
public List<User> selectAllServiceUser() throws Exception {
return this.dao.selectAll();
}
@Override
public boolean deleteServiceUser(int empno) throws Exception {
return this.dao.delete(empno);
}
@Override
public boolean updateServiceUser(User user) throws Exception {
return this.dao.update(user);
}
@Override
public User selectoneServiceUser(int empno) throws Exception {
return this.dao.selectone(empno);
}
}
- 进行最后的数据封装,就是工厂模式
import com.dbs.service.UserService;
import com.dbs.service.UserServiceImpl;
public class ServiceFactory {
public static UserService getUserServicceInstance() throws Exception{
return new UserServiceImpl();
}
}
- 下面就是实现数据在页面上的呈现,把所有的操作呈现给用户
(我用到EL表达式和JSTL表达式 即c标签,大家如果有不理解,可以留言喔,我尽量回复,或者更新相关的表达式)
主界面:
<%@page import="com.dbs.factory.ServiceFactory"%>
<%@page import="com.dbs.user.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<script language="javascript">
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Show All</title>
</head>
<body>
<a href="javascript:window.location.href='Add.jsp'"><button>添加员工</button></a>
<br/><br/>
<form action="SelectOne_deal.jsp" id="search" method="post">
<input type="text" name="empno" placeholder="请输入员工号进行查询">
<input type="submit" value="查询"><br/><br/>
</form>
<c:if test="${requestScope.Msg!=null}">
<c:out value="${requestScope.Msg}"></c:out><br/><br/>
</c:if>
<%List<User> rsList= new ArrayList<User>();%>
<c:choose>
<c:when test="${requestScope.rsUser==null}">
<!-- 默认查询所有 -->
<%rsList = (List<User>)ServiceFactory.getUserServicceInstance().selectAllServiceUser(); %>
</c:when>
<c:otherwise>
<!-- 选择查询某一个 -->
<%rsList.add((User)request.getAttribute("rsUser")); %>
</c:otherwise>
</c:choose>
<table bgcolor="#FAFAFA" border="1" width="760">
<tr>
<th>工号</th>
<th>姓名</th>
<th>职业</th>
<th>领导</th>
<th>工资</th>
<th>奖金</th>
<th>部门</th>
<th>删除操作</th>
<th>修改操作</th>
</tr>
<c:set value="<%=rsList%>" var="rsList" scope="request"></c:set>
<c:if test="${rsList!=null}">
<c:forEach items="${rsList}" var="user">
<tr>
<td><c:out value="${user.getEmpno()}"></c:out></td>
<td><c:out value="${user.getEname()}"></c:out></td>
<td><c:out value="${user.getJob()}"></c:out></td>
<td><c:out value="${user.getMgr()}"></c:out></td>
<td><c:out value="${user.getSal()}"></c:out></td>
<td><c:out value="${user.getComm()}"></c:out></td>
<td><c:out value="${user.getDeptno()}"></c:out></td>
<td>
<a href="Delete_deal.jsp?empnoId=${user.getEmpno()}"><button>删除</button></a>
</td>
<td>
<a href="Update.jsp?empno=${user.getEmpno()}&ename=${user.getEname()}&job=${user.getJob()}&mgr=${user.getMgr()}&sal=${user.getSal()}&comm=${user.getComm()}&deptno=${user.getDeptno()}">
<button>修改</button></a>
</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>
增加界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h><b>新 增 员 工</b></h>
<br/><br/>
<form action="Add_deal.jsp" method="post">
工号:<input type="text" name="empno" ><br>
姓名:<input type="text" name="ename"><br>
职业:<input type="text" name="job"><br>
部门:<input type="text" name="deptno"><br>
<br>
<input type="submit" value="添加">
</form>
</center>
</body>
</html>
增加处理页面,这里不会呈现给用户,只是后台处理数据的一个过程:
<%@page import="com.dbs.factory.ServiceFactory"%>
<%@page import="com.dbs.user.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
User user = new User();
user.setEmpno((Integer.valueOf(request.getParameter("empno"))));
user.setEname(request.getParameter("ename"));
user.setJob(request.getParameter("job"));
user.setDeptno(request.getParameter("deptno"));
boolean flag = ServiceFactory.getUserServicceInstance().addServiceUser(user);
if(flag){
request.setAttribute("Msg", "新增员工"+user.getEmpno()+"成功!");
}else{
request.setAttribute("Msg", "新增员工失败,请重新添加!");
}%>
<jsp:forward page="Show.jsp"></jsp:forward>
</body>
</html>
删除处理界面(这里也不呈现):
<%@page import="com.dbs.factory.ServiceFactory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>delete</title>
</head>
<body>
<%
int empno = (Integer.valueOf(request.getParameter("empnoId")));
boolean flag = ServiceFactory.getUserServicceInstance().deleteServiceUser(empno);
if(flag){
request.setAttribute("Msg", "删除员工"+empno+"成功!");
}else{
request.setAttribute("Msg", "删除员工失败,请重新删除!");
}%>
<jsp:forward page="Show.jsp"></jsp:forward>
</body>
</html>
更新数据页面(这里会呈现,因为需用用户输入相应内容):
<%@page import="com.dbs.user.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Update</title>
</head>
<body>
<%
String empno1= (String)request.getParameter("empno");
String ename1= (String)request.getParameter("ename");
String job1= (String)request.getParameter("job");
String mgr1= (String)request.getParameter("mgr");
String hiredate1= (String)request.getParameter("hiredate");
String sal1= (String)request.getParameter("sal");
String comm1= (String)request.getParameter("comm");
String deptno1= (String)request.getParameter("deptno");
%>
<center>
<h2>员工 <%=ename1%> 的信息修改</h2><br/><br/>
<form action="Update_deal.jsp" method="post">
<input type="hidden" name="inempno" value="<%=empno1%>">
工号:<input type="text" name="empno" value="<%=empno1%>" readonly="readonly"> <span>(不可更改)</span>
<br><br>
姓名:<input type="text" name="ename" value="<%=ename1%>">
<br><br>
职业:<input type="text" name="job" value="<%=job1%>">
<br><br>
领导:<input type="text" name="mgr" value="<%=mgr1%>">
<br><br>
日期:<input type="text" name="hiredate" value="<%=hiredate1%>">
<br><br>
工资:<input type="text" name="sal" value="<%=sal1%>">
<br><br>
奖金:<input type="text" name="comm" value="<%=comm1%>">
<br><br>
部门:<input type="text" name="deptno" value="<%=deptno1%>">
<br><br>
<input type="submit" value="更新修改">
</form>
</center>
</body>
</html>
更新处理界面(这里不呈现给用户):
<%@page import="com.dbs.factory.ServiceFactory"%>
<%@page import="com.dbs.user.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//原 员工号
String inempno= (String)request.getParameter("inempno");
//放进对象处理
User user = new User();
user.setEmpno(Integer.valueOf(request.getParameter("empno")));
user.setEname(request.getParameter("ename"));
user.setJob(request.getParameter("job"));
user.setMgr(request.getParameter("mgr"));
user.setSal(request.getParameter("sal"));
user.setComm(request.getParameter("comm"));
user.setDeptno(request.getParameter("deptno"));
//更新数据
boolean flag = ServiceFactory.getUserServicceInstance().updateServiceUser(user);
if(flag){
request.setAttribute("Msg", "更新员工"+inempno+"成功!");
}else{
request.setAttribute("Msg", "新增员工失败,请重新添加!");
}%>
<jsp:forward page="Show.jsp"></jsp:forward>
%>
</body>
</ht
小注:代码如果不全,请大家下载我上传的代码包,希望大家有意见多提出来,我们可以一起讨论,我可喜欢和别人聊啦!祝大家学习进步!谢谢!