JavaBean实操代码

Java分为2大类:

a.封装业务逻辑的JavaBean (LoginDao.java封装了登录逻辑) 逻辑

可以将jsp中的JDBC代码,封装到Login.java类中 (LoginDao.java)

package dao;
import java.sql.*;

import entity.Login;

public class LoginDao {
    public int login(Login login) {
        String URL = "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=GMT%2B8";
        String USERNAME = "root";
        String PWD = "password";
        Connection connection = null;
        Statement stmt = null;
        ResultSet rs = null;
        {
            try {
                // a.导入驱动,加载具体的驱动类
                Class.forName("com.mysql.cj.jdbc.Driver");// 加载具体的驱动类
                // b.与数据库建立连接
                connection = DriverManager.getConnection(URL, USERNAME, PWD);
                // c.发送sql,执行(增删改、【查】)
                stmt = connection.createStatement();
                // String sql = "select stuno,stuname from student";

                
                String sql = "select count(*) from login where uname='" + login.getUname() + "' and upwd ='" + login.getUpwd() + "' ";
                // String sql = "select * from student where stuname like '%"+name+"%'";
                // 执行SQL(增删改executeUpdate(),查询executeQuery())
                rs = stmt.executeQuery(sql); // 返回值表示 增删改 几条数据
                // d.处理结果
                int count = -1;
                if (rs.next()) {
                    count = rs.getInt(1);
                }
//        if (count > 0) {
//            out.println("登陆成功!");
//        } else {
//            out.println("登陆失败!");
//        }
                return count;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return -1;
            } catch (SQLException e) {
                e.printStackTrace();
                return -1;
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            } finally {
                try {
                    if (rs != null)
                        rs.close();
                    if (stmt != null)
                        stmt.close();// 对象.方法
                    if (connection != null)
                        connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

 

b.封装数据的JavaBean (实体类,Student.java Person.java ) 数据

对应于数据库中的一张表

package entity;

public class Login {
    private int id;
    private String uname;
    private String upwd;

    public int getId() {
        return id;
    }

    public Login() {

    }

    public Login( String uname, String upwd) {

        
        this.uname = uname;
        this.upwd = upwd;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

}

 

Login login = new Login(uname,upwd) ;//即用Login对象 封装了2个数据(用户名 和密码)

 

封装数据的JavaBean 对应于数据库中的一张表 (Login(name,pwd))

封装业务逻辑的JavaBean 用于操作 一个封装数据的JavaBean

可以发现,JavaBean可以简化 代码(jsp->jsp+java)、提供代码复用(LoginDao.java)

posted @ 2020-02-04 19:42  你的深渊  阅读(474)  评论(0编辑  收藏  举报