一、JavaBean简介
JavaBean是使用Java语言开发的一个可重用的组件,在JSP的开发中可以使用JavaBean减少重复代码,使整个JSP代码的开发更简洁。JSP搭配JavaBean来使用,优点也不少,最主要的好处还是将HTML的代码与Java代码分开,方便管理和维护。
二、JavaBean开发要求
1.JavaBean本身就是一个类,属于Java的面向对象编程。
2.在JSP中如果要应用JSP提供的Javabean的标签来操作简单类的话,则此类必须满足如下的开发要求:
(1)所有的类需要放在一个包中;
(2)所有的类必须声明为public class,这样才能够被外部所访问;
(3)类中所有的属性都必须封装,即:使用private声明;
(4)封装的属性如果需要被外部所操作,则必须编写对应的setter、getter方法;
(5)一个JavaBean中至少存在一个无参构造方法,此为JSP中的标签所使用。
三、关于<jsp:useBean>指令的使用
除了使用import的语句之外,也可以使用JSP中提供的:<jsp:useBean>指令完成操作,在使用jsp:useBean/指令时,实际上会默认调用自己写的类中的无参构造方法进行对象实例化,所以类中存在无参构造方法是个使用前提,当修改类文件后,如果要想让其起作用,则需要重新启动服务器。
指令的操作语法如下所示:
1.<jsp:useBean id=“实例化对象名称” scope=“保存范围” class=“包.类名称“/>
2.主要属性:
(1)id:表示实例化对象的名称
(2)scope:表示此对象保存的范围,一共有四种属性范围:page、request、session、application
(3)class:对象所对应的包.类名称
四、结合数据库,综合举例使用JavaBean:
数据库管理的Javabean类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectionMgr {
public static Connection getConnection() throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
return DriverManager.getConnection(url,"username","password");
}
public static void freeResource(Connection con,Statement st,ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
if(st!=null) {
st.close();
}
if(con!=null) {
con.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
构造对象的JavaBean类:
public class UserInfo {
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;
}
}
封装数据库增删改查操作的JavaBean类:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class UserListBean {
ConnectionMgr mgr = new ConnectionMgr();
@SuppressWarnings("static-access")
public List<UserInfo> findAllUserinfo(){
Connection con = null;
Statement st = null;
ResultSet rs1 = null;
List<UserInfo> list = null;
try {
con = mgr.getConnection();
st = con.createStatement();
list = new ArrayList<UserInfo>();
rs1 = st.executeQuery("select * from emp");
while(rs1.next()){
UserInfo user1 = new UserInfo();
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));
list.add(user1);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
mgr.freeResource(con, st, rs1);
}
return list;
}
//添加一条
public boolean addUser(UserInfo user) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs1 = null;
boolean flag = false;
try {
conn = mgr.getConnection();
String sql="insert into emp(empno,ename,job,deptno) values(?,?,?,?)";
pstmt = 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(pstmt.executeUpdate()>0) {
flag = true;
}
}catch (Exception e) {
e.printStackTrace();
}finally {
mgr.freeResource(conn, pstmt, rs1);
}
return flag;
}
//删除
public boolean delete(int empno) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs1 = null;
boolean flag = false;
try {
conn = mgr.getConnection();
String sql="delete from emp where empno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, empno);
if(pstmt.executeUpdate()>0) {
flag = true;
}
}catch (Exception e) {
e.printStackTrace();
}finally {
mgr.freeResource(conn, pstmt, rs1);
}
return flag;
}
//更新一条
public boolean update(UserInfo user) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs1 = null;
boolean flag = false;
try {
conn = mgr.getConnection();
String sql="update emp set ename=?,job=?,mgr=?,sal=?,comm=?,deptno=? where empno=?";
pstmt = 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(pstmt.executeUpdate()>0) {
flag = true;
}
}catch (Exception e) {
e.printStackTrace();
}finally {
mgr.freeResource(conn, pstmt, rs1);
}
return flag;
}
//查找一个
public UserInfo selectone(int empno) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs1 = null;
UserInfo user1 = new UserInfo();
try {
String sql="select * from emp where empno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, empno);
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));
}
}catch (Exception e) {
e.printStackTrace();
}finally {
mgr.freeResource(conn, pstmt, rs1);
}
return user1;
}
}
调用JavaBean的JSP页面:
这里用到 import 导入类包 和 <jsp:useBean>
<%@page import="com.dbs.javabean.UserListBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="user" scope="request" class="com.dbs.javabean.UserInfo"/>
<!DOCTYPE html>
<%@ 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>
<head>
<meta 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<UserInfo> rsList= new ArrayList<UserInfo>();%>
<c:choose>
<c:when test="${requestScope.rsUser==null}">
<!-- 默认查询所有 -->
<%rsList = new UserListBean().findAllUserinfo(); %>
</c:when>
<c:otherwise>
<!-- 选择查询某一个 -->
<%rsList.add((UserInfo)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 import="com.dbs.javabean.UserListBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="user" scope="request" class="com.dbs.javabean.UserInfo"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int empno = Integer.valueOf(request.getParameter("empno"));
user = new UserListBean().selectone(empno);
if(user.getEmpno()!=0){
request.setAttribute("rsUser", user);
request.setAttribute("Msg", "查找员工"+empno+"成功!");
}else{
request.setAttribute("Msg", "查找员工失败,请重新查找!");
}%>
<jsp:forward page="show.jsp"></jsp:forward>
</body>
</html>
🐖小注:如果大家有什么问题,可以私信我喔!