javaWeb application&javaBean封装

目录

application(应用级存储)

简介

案例:统计在线人数

javaBean

简介

实现javaBean三部曲

实体类(封装对象所有属性)

 功能实现类

帮助类(用于实现数据库与java连接与关闭)

                                                                                              你一定要走,走到灯火通明

                                                                                                                   ——2022.4.6


application(应用级存储)

简介

和Sesseion,Cookie一样,Application也是一种存储方式但它属于应用级存储(建的每一个项目就相当于一个应用),application的主要功能就是数据共享,相当于全局变量,但它的共享是用于整个项目

案例:统计在线人数

doLoing.jsp

<%@page import="oracle.jdbc.driver.OracleDriver" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="java.util.Arrays" %>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    request.setCharacterEncoding("utf-8");
    String yh = request.getParameter("yh");
    String mm = request.getParameter("mm");
    try {
        //加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //定义连接字符串
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        //获得连接
        Connection con = DriverManager.getConnection(url, "ZYH", "zkingedu");
        //获得执行对象
        PreparedStatement ps = con.prepareStatement("select * from login where zh=? and pwd=?");
        ps.setString(1, yh);
        ps.setString(2, mm);
        //获得结果集
        ResultSet rs = ps.executeQuery();
        //判断结果
        if (rs.next()) {
            // localhost:8080/当前项目/news/index.jsp
             
                     Object obj= application.getAttribute("count");
               if(obj==null){
                   obj=0;
               }
               Integer count=(Integer)obj;
               count++;
               application.setAttribute("count", count);
               
               response.sendRedirect("news/index.jsp");
           // request.getRequestDispatcher("/news/index.jsp").forward(request, response);
        } else {
            //重定向 客户端
            response.sendRedirect("login.jsp");
        }
        //资源关闭
        if (!con.isClosed()) {
            con.close();
        }
        ps.close();
        rs.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
%> 

javaBean

简介

javaBean:泛指java对象

主要功能:可以减少代码量,相当于封装java方法 

实现javaBean三部曲

必须有三个类:

实体类:pojo

功能实现类:dao

帮助类:DBHelper

实体类(封装对象所有属性)


package web_06.com.pojo;
import java.io.Serializable;
public class News implements Serializable{//实现序列化接口,以防之后报错
	private int news_Id;
	private String news_titile;
	private int news_topic;
	private String news_author;
	private String news_publisher;
	private int news_click;
	private int news_maker;
	private String news_content;
	private String news_cover;
	public int getNews_Id() {
		return news_Id;
	}
	public void setNews_Id(int news_Id) {
		this.news_Id = news_Id;
	}
	public String getNews_titile() {
		return news_titile;
	}
	public void setNews_titile(String news_titile) {
		this.news_titile = news_titile;
	}
	public int getNews_topic() {
		return news_topic;
	}
	public void setNews_topic(int news_topic) {
		this.news_topic = news_topic;
	}
	public String getNews_author() {
		return news_author;
	}
	public void setNews_author(String news_author) {
		this.news_author = news_author;
	}
	public String getNews_publisher() {
		return news_publisher;
	}
	public void setNews_publisher(String news_publisher) {
		this.news_publisher = news_publisher;
	}
	public int getNews_click() {
		return news_click;
	}
	public void setNews_click(int news_click) {
		this.news_click = news_click;
	}
	public int getNews_maker() {
		return news_maker;
	}
	public void setNews_maker(int news_maker) {
		this.news_maker = news_maker;
	}
	public String getNews_content() {
		return news_content;
	}
	public void setNews_content(String news_content) {
		this.news_content = news_content;
	}
	public String getNews_cover() {
		return news_cover;
	}
	public void setNews_cover(String news_cover) {
		this.news_cover = news_cover;
	}
	@Override
	public String toString() {
		return "News [news_Id=" + news_Id + ", news_titile=" + news_titile + ", news_topic=" + news_topic
				+ ", news_author=" + news_author + ", news_publisher=" + news_publisher + ", news_click=" + news_click
				+ ", news_maker=" + news_maker + ", news_content=" + news_content + ", news_cover=" + news_cover + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + news_Id;
		result = prime * result + ((news_author == null) ? 0 : news_author.hashCode());
		result = prime * result + news_click;
		result = prime * result + ((news_content == null) ? 0 : news_content.hashCode());
		result = prime * result + ((news_cover == null) ? 0 : news_cover.hashCode());
		result = prime * result + news_maker;
		result = prime * result + ((news_publisher == null) ? 0 : news_publisher.hashCode());
		result = prime * result + ((news_titile == null) ? 0 : news_titile.hashCode());
		result = prime * result + news_topic;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		News other = (News) obj;
		if (news_Id != other.news_Id)
			return false;
		if (news_author == null) {
			if (other.news_author != null)
				return false;
		} else if (!news_author.equals(other.news_author))
			return false;
		if (news_click != other.news_click)
			return false;
		if (news_content == null) {
			if (other.news_content != null)
				return false;
		} else if (!news_content.equals(other.news_content))
			return false;
		if (news_cover == null) {
			if (other.news_cover != null)
				return false;
		} else if (!news_cover.equals(other.news_cover))
			return false;
		if (news_maker != other.news_maker)
			return false;
		if (news_publisher == null) {
			if (other.news_publisher != null)
				return false;
		} else if (!news_publisher.equals(other.news_publisher))
			return false;
		if (news_titile == null) {
			if (other.news_titile != null)
				return false;
		} else if (!news_titile.equals(other.news_titile))
			return false;
		if (news_topic != other.news_topic)
			return false;
		return true;
	}	
}

 功能实现类


package web_06.com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import web_06.com.pojo.News;
import web_06.com.util.DBHelper;
public class NewsDao {
	private Connection con;
	private PreparedStatement ps;
	private ResultSet rs;
	public List<News> queryByName(String newName) {
		List<News> list=new ArrayList<News>();
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("select * from t2_news where news_title like ?");
			//占位符赋值
            ps.setString(1,"%"+newName+"%");
            //得到结果集
			rs=ps.executeQuery();
			while(rs.next()) {
				News news=new News();
				//给新闻对象的属性赋值
				news.setNews_Id(rs.getInt(1));
				news.setNews_titile(rs.getString(2));
				news.setNews_topic(rs.getInt(3));
				news.setNews_author(rs.getString(4));
				news.setNews_publisher(rs.getString(5));
				news.setNews_click(rs.getInt(6));
				news.setNews_maker(rs.getInt(7));
				news.setNews_content(rs.getString(8));
				news.setNews_cover(rs.getString(9));
				//将新闻对象添加到集合中
				list.add(news);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.close(con, ps, rs);
		}
		return list;
	}

}

帮助类(用于实现数据库与java连接与关闭)

package web_06.com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleDriver;
public class DBHelper {
	//加载驱动
	static {
		//OracleDriver
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	//定义连接字符串
	private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
	//获得连接
	public static Connection getCon() {
		try {
			return DriverManager.getConnection(URL,"scott","123");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	//关闭资源
	public static void close(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if (!con.isClosed()) {
		         con.close();
		     }
		     ps.close();
		     rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

                                                                                              你一定要走,走到灯火通明

                                                                                                                   ——2022.4.6

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值