JavaWeb JavaBean封装

本文介绍了JavaBean的概念,解释了为何在JavaWeb中使用JavaBean以减少代码冗余和提高代码维护性。详细步骤包括创建Java文件、封装数据库连接、创建实体类和操作类,以此实现模糊查询功能。
摘要由CSDN通过智能技术生成

目录

一.什么是JavaBean?

二.为什么需要JavaBean?

三.如何在JavaWeb中创建Java文件

四.完成模糊查询

   1.封装连接数据库语句(放在dao包下方,DBHeper类)

   2.在pojo包下创建一个新闻实体类

   3.在dao包下方,创建操作类

   4.去新闻系统中的新闻首页操作

总结


一.什么是JavaBean?

  JavaBean泛指的就是Java对象

  Java:可以理解为是豆荚    Bean:豆子

二.为什么需要JavaBean?

  • 解决代码重复编写,减少代码冗余
  • 功能区分明确,避免业务逻辑处理与页面显示处理集中在一起造成混乱
  • 提高了代码的维护性

三.如何在JavaWeb中创建Java文件

1.点击mian文件(一般情况下mian文件下方自带一个Java文件),鼠标右键,点击new,点击other,搜索package,新建一个包,在点击package鼠标右键,new---->other--->class,新建一个class。(就和java文件创建的过程一样)

注:如果main下方没有Java文件,可以直接点到src/main/java鼠标右键,新建package,在新建class,就和java建文件一样。

 

四.完成模糊查询

思路:

   1.封装连接数据库语句(放在dao包下方,DBHeper类)

   代码如下:

package ulit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import oracle.jdbc.driver.OracleDriver;

public class DBHeper {
     static{
    	 try {
    		 Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
     }
     
     //定义连接字符创
     public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
     
     //获得连接
     public static Connection getCon() {
    	 try {
    		 return DriverManager.getConnection(URL,"scott","zking123"); 
		} catch (Exception e) {
			e.printStackTrace();
		}
    	  return null;
     }
     
   //关闭资源
     public static void getColse(Connection con,PreparedStatement ps,ResultSet rs) {
    	try {
    	if(con!=null&&con.isClosed())con.close();
       	 if(ps!=null)ps.close();
       	 if(rs!=null)rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	 
     }
     
}

2.在pojo包下创建一个新闻实体类

  代码如下:

package pojo;

import java.io.Serializable;

public class News  implements Serializable{
      
	private int news_id;
	private String news_title;
	private int  news_topic;
	private String news_author;
	private String news_publisher;
	private String news_contet;
	private  String news_cover;
	private int news_count;
	public int getNews_id() {
		return news_id;
	}
	public void setNews_id(int news_id) {
		this.news_id = news_id;
	}
	public String getNews_title() {
		return news_title;
	}
	public void setNews_title(String news_title) {
		this.news_title = news_title;
	}
	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 String getNews_conte
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值