【E家园项目】

登录

        1、utils类中连接数据库,创建BaseDao

        2、entity类写入用户实体:

        3、Dao类编写实现用户登录的方法

        4、用户接口biz层:IUserBiz . java

        5、在index.jsp界面中编写登录的方法

实现用户登录需运用到三层构架:utils类、entity类、dao类、biz层

登录
1、utils类中连接数据库,创建BaseDao

package com.zking.utils;
 
/*
 * 数据库帮助类
 */
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 {
            String url = "jdbc:oracle:thin:@localhost:1521:orc";
            conn = DriverManager.getConnection(url, "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();
        }
 
    }

 
BaseDao . java

package com.zking.utils;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
/**
 * 增删改查通用类
 * 
 * @author ling
 *
 */
public class BaseDao {
    protected ResultSet rs = null;
    protected Connection conn = null;
    protected PreparedStatement ps = null;
 
    /**
     * 方法功能:通用增删改
     * 
     */
    public int executeUpdate(String sql, Object... objects) {
        int n = 0;
        try {
            conn = DBHelper.getConn();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < objects.length; i++) {
                ps.setObject(i + 1, objects[i]);
            }
            n = ps.executeUpdate();
        } catch (Exception e) {
            System.out.println("DAO增删改异常");
            e.printStackTrace();
        } finally {
            DBHelper.myClose(conn, ps, null);
        }
        return n;
    }
 
    /**
     * 方法功能:通用查询
     * 
     */
    public ResultSet executeQuery(String sql, Object... objects) {
        try {
            conn = DBHelper.getConn();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < objects.length; i++) {
                ps.setObject(i + 1, objects[i]);
            }
            rs = ps.executeQuery();
        } catch (Exception e) {
            System.out.println("DAO查询异常");
            e.printStackTrace();
        }
        return rs;
    }
 
    /**
     * 方法功能:获取指定表的最大ID
     * 
     */
    public int getMaxID(String colID, String tableName) {
        String sql = "select nvl(max(" + colID + "),0) from " + tableName;
        ResultSet rs = null;
        try {
            rs = this.executeQuery(sql);
            if (rs.next()) {
                return rs.getInt(1) + 1;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return 0;
    }
 
    /**
     * 方法功能:获取任意表格的总记录数
     * 
     * @param 表名
     */
    public int getTableCount(String tableName) {
        int count = 0;
        String sql = "select count(*) from " + tableName;
 
        ResultSet rs = this.executeQuery(sql);
        try {
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBHelper.myClose(this.conn, this.ps, rs);
        }
        return count;
    }

<!-- 登录 -->
                  

  <c:if test="${empty users }">
                        <form action="dologin.jsp" method="post">
                            <table style="margin-left: 5px;">
                                <tr>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td>登录名:<input name="username" style="width: 110px;" />
                                </tr>
                                <tr>
                                    <td>密码 :<input name="password" style="width: 110px;" /></td>
                                </tr>
                                <tr>
                                    <td><input type="image" src="image/button01.gif" /> <img
                                        src="image/button02.gif" οnclick="register();" /></td>
                                </tr>
                            </table>
                        </form>
                    </c:if>
                    <c:if test="${not empty users }">
                        <div style="text-align: center;">
                            <p>欢迎${users.username }回来!</p>
                            <!-- <h5></h5> -->
                            <button>修改密码</button>
                            <button>个人中心</button>
                        </div>
                    </c:if>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值