Java简化代码(封装)

以前写的新闻发布系统代码重复且亢长,使用JavaBean封装后可以把javajsp分离,使整个界面看上去更精简,也更方便。


  一.What?什么是 JavaBean?

1.JavaBean 是一种组件技术。

   javabean 技术有助于将JSP页面中的处理业务的逻辑代码展示页面效果的显示代码分离

2.JavaBean 规则。

   1)有无参的构造函数

   2)成员属性私有化

   3)封装的属性如果需要被外所操作,必须编写public类型的settergetter方法

3.JavaBean 分类。

   1)封装数据

   2)封装业务逻辑

二.Why? 为什么要用到 JavaBean? 

1. 使用 javabean 的好处就是:封装重用 , 可读

2.JavaBean 优点 。

   1)减少代码冗余,相同功能的代码不必重复编写

   2)功能区分明确,相同功能写在一个中。相似功能放在同一个

   3)提高代码的可维护性

三.部分封装代码展示 

 JavaBean封装java代码:

 其实就是把jsp页面的java代码封装到JavaResourcessrc里面;

 在JavaResources-src里建立三个包,分别是Dao包、entity包、util包

注:分别放数据库访问类实体类DBHelper类。

Java简化代码(封装)_第1张图片 

1. DBHelper类

package com.news.untils;
/**
 * 数据库帮助类
 * @author zjjt
 *
 */
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
public class DBHelper {
 
	// 加载驱动
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	// 建立连接
	public static Connection getConn() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
 
	// 关闭连接
	public static void myClose(Connection conn, PreparedStatement ps, ResultSet rs) {
		try {
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}
			if (ps != null) {
				ps.close();
			}
			if (rs != null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
}

 2.用户实体类(entity)

package com.news.entity;
 
/**
 * 用户实体类
 * 
 * @author zjjt
 *
 */
 
public class Users {
 
	private int userid;
	private String username;
	private String password;
	private String sex;
	private String hobby;
	private String address;
	private int urole;
	private String udate;
 
	public Users() {
		// TODO Auto-generated constructor stub
	}
 
	public Users(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
 
	public Users(String username, String password, String sex, String hobby, String address, int urole, String udate) {
		super();
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.hobby = hobby;
		this.address = address;
		this.urole = urole;
		this.udate = udate;
	}
 
	public Users(int userid, String username, String password, String sex, String hobby, String address, int urole,
			String udate) {
		super();
		this.userid = userid;
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.hobby = hobby;
		this.address = address;
		this.urole = urole;
		this.udate = udate;
	}
 
	public int getUserid() {
		return userid;
	}
 
	public void setUserid(int userid) {
		this.userid = userid;
	}
 
	public String getUsername() {
		return username;
	}
 
	public void setUsername(String username) {
		this.username = username;
	}
 
	public String getPassword() {
		return password;
	}
 
	public void setPassword(String password) {
		this.password = password;
	}
 
	public String getSex() {
		return sex;
	}
 
	public void setSex(String sex) {
		this.sex = sex;
	}
 
	public String getHobby() {
		return hobby;
	}
 
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
 
	public String getAddress() {
		return address;
	}
 
	public void setAddress(String address) {
		this.address = address;
	}
 
	public int getUrole() {
		return urole;
	}
 
	public void setUrole(int urole) {
		this.urole = urole;
	}
 
	public String getUdate() {
		return udate;
	}
 
	public void setUdate(String udate) {
		this.udate = udate;
	}
 
	@Override
	public String toString() {
		return "Users [userid=" + userid + ", username=" + username + ", password=" + password + ", sex=" + sex
				+ ", hobby=" + hobby + ", address=" + address + ", urole=" + urole + ", udate=" + udate + "]";
	}
 
}

 3.Dao 类

package com.news.dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.news.dao.IUsersDao;
import com.news.entity.Users;
import com.news.untils.DBHelper;
 
/**
 * 用户模块的DAO接口
 * 
 * @author Administrator
 *
 */
public class UsersDaoImpl implements IUsersDao {
 
	@Override
	public Users adminUsersLogin(Users users) {
		// 1.定义所需的对象及变量
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from tb_news_users where username = ? and password = ?";
		Users u = null;
		// 2.赋值
		try {
			// 获取数据库连接
			conn = DBHelper.getConn();
			// 将定义的sql语句传入方法返回执行对象
			ps = conn.prepareStatement(sql);
			// 给占位符赋值
			ps.setString(1, users.getUsername());
			ps.setString(2, users.getPassword());
			// 返回结果集对象
			rs = ps.executeQuery();
			// 判断
			if (rs.next()) {
				u = new Users(rs.getInt("userid"), rs.getString("username"), rs.getString("password"),
						rs.getString("sex"), rs.getString("hobby"), rs.getString("address"), rs.getInt("urole"),
						rs.getString("udate"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.myClose(conn, ps, rs);
		}
 
		// 返回
		return u;
	}
 
	public static void main(String[] args) {
		Users adminUsersLogin = new UsersDaoImpl().adminUsersLogin(new Users("admin", "123"));
		System.out.println(adminUsersLogin);
	}
 
	@Override
	public int addUsersLogin(Users users) {
		// TODO Auto-generated method stub
		return 0;
	}
 
	@Override
	public int setUsersPassword(Users users, String newPassword) {
		// TODO Auto-generated method stub
		return 0;
	}
 
}

许多时候,我们执着于梦想,以为远方才有风景。但其实,最美的风景,往往就在身边。要明白,岁月易蹉跎,但难回头。

你可能感兴趣的:(eclipse,java,java-ee)