javaBean是java的一种软件组件模型,是java语言编写的类。
简单理解:如果在jsp页面中有一个输入表单供用户输入注册信息,输入的注册信息要保存到服务器端的数据库中,保存用户信息的过程可以在java函数中完成,可以使用纯java语言完成,页面只需要调用该函数传给他相应的参数即可,实现这段业务代码的类被称为JavaBean。
利用javabean实现java与HTML页面的分离。用户可以使用javabean将功能,处理,数据库访问和其他任何可以用java代码创造的对象进行打包,然后通过jsp页面调用即可。
jababean必须是公共的,并且具有无参数的构造器!
其实,只需要知道,javabean就是一个普通的java类,即可,在使用时需要用户创建其对象实例!
下面附上作者学习时候练习的代码:
1.最基本的操作,定义使用javabean
首先是定义的java类:
//日期:2019.6.2
//作者:***********
//功能:定义一个javaBean,通过简单的练习了解javabean的使用方法
package com.bean;
import java.util.*;
public class Circle {
private double r; //定义成员变量,半径r
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
//函数实现,计算圆的面积
public double area()
{
double pai = 3.14;
return pai * r * r;
}
}
然后是使用javabean的jsp文件:
<%@ page language="java" pageEncoding="utf-8" %>
<%@ page import="java.util.*" %>
<%@ page import="com.bean.*" %>
<html>
<head>
<title>练习使用javaBean的基本操作</title>
</head>
<body>
<!-- 第一种方式,直接在页面进行创建 -->
<%
Circle obj1 = new Circle();
obj1.setR(5.0);
double area1 = obj1.area();
out.println("圆的面积为: "+area1);
%>
<!-- 第二种方式,使用标签 -->
<jsp:useBean id="obj2" class="com.bean.Circle"/>
<%
obj2.setR(5.0);
double area2 = obj2.area();
out.println("<br>圆的面积为: "+area2);
%>
<!-- 然后就是其属性的访问也可以使用标签 -->
<jsp:useBean id="obj3" class="com.bean.Circle"/>
<jsp:setProperty name="obj3" property="r" value="5.0"/>
<br>get the value of "r":<jsp:getProperty name="obj3" property="r"/>
<%
out.println("<br>圆的面积为: "+obj3.area());
%>
</body>
</html>
2.稍微复杂的:使用javaBean实现一个网页端的数据库分页检索显示功能
首先:定义javabean的java类:这里使用了O/R映射的类,和实现分层数据库操作的DAO类
package com.bean2;
//封装O/R映射的类
public class Student
{
private String id;
private String name;
private String phone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
//日期:2019.6.2
//作者:刘烜杰
//功能:javabean实现数据库操作的DAO层的逻辑
package com.bean2;
import java.util.*;
import java.sql.*;
public class StudentDao {
public static void main(String args[])
{
StudentDao studentDao = new StudentDao();
ArrayList<Student> list = (ArrayList<Student>)studentDao.queryAllStudents(2, 3);
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student.getId());
}
System.out.println(studentDao.getSumPageCount(3));
System.out.print(studentDao.getSumRecord());
}
String driverName = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/school?serverTimezone=GMT%2B8";
String user = "root";
String password = "dba.liuxuanjie";
//封装底层数据库操作,即连接数据库
public Connection getConnection() throws Exception
{
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
}
//封装操作,向数据库添加信息
public void addStudent(String info[])
{
if(info == null) return;
try {
Connection connection = this.getConnection(); //连接数据库
String sql = "insert into students values(?,?,?)"; //构造预编译的sql语句
PreparedStatement preparedStatement = connection.prepareStatement(sql); //预编译sql语句
preparedStatement.setString(1, info[0]);
preparedStatement.setString(2, info[1]);
preparedStatement.setString(3, info[2]);
preparedStatement.executeUpdate();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//封装函数,根据传入的参数,当前页数,每页显示的行数,来查询数据
public List<Student> queryAllStudents(int countPage,int mayPage)
{
int num = mayPage * (countPage - 1);
List<Student> list = new ArrayList<Student>(); //使用集合泛型student
String sql = "select * from students limit ?,?"; //构造sql语句
try {
Connection connection = this.getConnection(); //连接数据库
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, num);
preparedStatement.setInt(2, mayPage);
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next())
{
//得到每一行的信息,映射为student类
Student student = new Student();
student.setId(resultSet.getString(1));
student.setName(resultSet.getString(2));
student.setPhone(resultSet.getString(3));
list.add(student);
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//封装函数,查询总的记录数,也就是所有的信息行数
public int getSumRecord()
{
int sumCount = 0;
String sql = "select * from students";
try {
Connection connection = this.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.last(); //把游标指向结果集的最后一行
sumCount = resultSet.getRow(); //得到当前游标所指向的记录的行数
} catch (Exception e) {
e.printStackTrace();
}
return sumCount;
}
//封装函数,查询总页数,传入参数为每一行的记录数
public int getSumPageCount(int pageCount)
{
int sumRecord = this.getSumRecord(); //得到总的记录数
//思路很简单,总的记录数,除以每一行的记录数,即可
return (sumRecord % pageCount == 0) ?
(sumRecord / pageCount) : (sumRecord / pageCount + 1);
}
//疯转函数,删除学生记录,根据学生的学号(主键)
public void deleteStudent(String id)
{
try {
Connection connection = this.getConnection();
String sql = "delete from students where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id);
preparedStatement.executeUpdate();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后是调用其javabean的jsp页面
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.util.*,java.sql.*"%>
<%@ page import="com.bean2.*" %>
<html>
<head>
<title>我的javaBean实战练习</title>
</head>
<body>
<jsp:useBean id="dao" class="com.bean2.StudentDao"/>
<center>
<table border=1>
<tr>
<td><font color="red">学号</font></td>
<td><font color="red">姓名</font></td>
<td><font color="red">电话</font></td>
<td colspan=2></td>
</tr>
<%
int pageCount = 3; //每页显示的记录数
int rowCount = 0; //总记录数
int curPageCount = 0; //当前的页数
int sumPageCount = 0; //总的页数
String strPage = request.getParameter("page");
if(strPage == null) curPageCount = 1;
else curPageCount = Integer.parseInt(strPage);
rowCount = dao.getSumRecord(); //直接使用javabean的封装的方法
sumPageCount = dao.getSumPageCount(pageCount);
if(curPageCount < 1)
{
curPageCount = 1;
out.print("已经是第一页!");
}
if(curPageCount > sumPageCount)
{
out.print("已经是最后一页!");
curPageCount = sumPageCount;
}
ArrayList<Student> list =
(ArrayList<Student>)dao.queryAllStudents(curPageCount, pageCount);
pageContext.setAttribute("stuList", list);
ArrayList list2 = (ArrayList)pageContext.getAttribute("stuList");
if(list2 != null)
{
for(int i = 0; i < list2.size(); i++)
{
Student student = (Student)list2.get(i);
out.println("<tr><td>"+student.getId()+"</td>"+
"<td>"+student.getName()+"</td>"+
"<td>"+student.getPhone()+"</td></tr>");
}
}
%>
<%
out.println("<tr><td><a href=useBean2.jsp?page=1>第一页</a></td>");
out.println("<td><a href=useBean2.jsp?page="+(curPageCount+1)+">下一页</a></td>");
out.println("<td><a href=useBean2.jsp?page="+(curPageCount-1)+">上一页</a></td>");
out.println("<td><a href=useBean2.jsp?page="+sumPageCount+">最后一页</a></td></tr>");
%>
</table>
</center>
</body>
</html>
显示实现结果: