Java Bean 还是 POJO, 究竟用哪个好?

Java Bean和POJO都可以用于业务逻辑和显示逻辑解耦合的作用,Java Bean遵循一些标准化的约定,有利于重用和互用,但是也牺牲了个性,对于比较小型的项目,POJO更加简洁灵活。


一般在做JSP网站的时候,比如像连接读写数据库的操作比较繁琐,这时候使用Java Bean或者POJO做成类,就可以使得JSP通过分离后端操作和重用类来简化JSP内的JAVA代码,使之简明清晰。



package db;

import java.beans.*;
import java.io.Serializable;

/**
 *
 * @author Administrator
 */
public class DB implements Serializable {
    
    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
    
    private String sampleProperty;
    
    private PropertyChangeSupport propertySupport;
    
    public DB() {
        propertySupport = new PropertyChangeSupport(this);
    }
    
    public String getSampleProperty() {
        return sampleProperty;
    }
    
    public void setSampleProperty(String value) {
        String oldValue = sampleProperty;
        sampleProperty = value;
        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
    }
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
    
}

package db;

import java.beans.*;
import java.io.Serializable;

/**
 *
 * @author Administrator
 */
public class DB1 implements Serializable {
    
    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
    
    private String sampleProperty;
    
    private PropertyChangeSupport propertySupport;
    
    public DB1() {
        propertySupport = new PropertyChangeSupport(this);
    }
    
    public String getSampleProperty() {
        return sampleProperty+"987654321";
    }
    
    public void setSampleProperty(String value) {
        String oldValue = sampleProperty;
        sampleProperty = value+"123456789";
        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
    }
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
    
}


package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class DBLink {
    
    Connection conn;
    Statement stmt;
    String url = "jdbc:mysql://localhost:3306/users?user=root&password=admin&useUnicode=true&charaterEncoding=UTF8";
    String sql = "";
    ResultSet rs;
    String rsString="";
    public String dbValueOfField(String field,String table,String conditions){
        try {
            //注意要是的数据库能够正常连接上必须在库中添加mysql-connector-java的jar包
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url);
            sql="SELECT "+field+" FROM "+table+" WHERE "+conditions;
            stmt=conn.createStatement();
            rs=stmt.executeQuery(sql);
            if(rs.next()){
                rsString+=rs.getString(1);
            }            
            return "Hello "+rsString+"!";
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DBLink.class.getName()).log(Level.SEVERE, null, ex);
        }       
        
        return "Hello "+rsString+"!";      
        
    }
    
}

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="db.DB"%>
<%@page import="db.DBLink"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>三种使用Java Bean和POJO的小例子</h1>

        <%
            DB db = new DB();
            db.setSampleProperty("hello my bean class 111");
            String dbrs = db.getSampleProperty();
        %>

        <%= dbrs%>

        <hr/>

        <jsp:useBean id="db1" class="db.DB1" />
        <jsp:setProperty name="db1" property="sampleProperty" value="hello db1 bean" />
        <jsp:getProperty name="db1" property="sampleProperty" />     

        <hr/>
        
        <%
            DBLink dblink = new DBLink();
            String welcome = dblink.dbValueOfField("nickname", "userinformation", "username='111@111.com'");
        %>

        <%= welcome%>

    </body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值