首页>代码>基于JavaWeb的酒店管理系统>/源代码/HotelSys/src/com/inks/hb/authinfo/dao/AuthInfoDao.java
package com.inks.hb.authinfo.dao;

import com.inks.hb.authinfo.pojo.AuthInfo;
import com.inks.hb.common.CommonDao;
import com.inks.hb.common.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AuthInfoDao implements CommonDao {

    @Override
    public void insertData(Object o) throws SQLException {
        AuthInfo authInfo = (AuthInfo) o;

        Connection conn = DBUtil.getConnection();

        String sql = "insert into authInfo (authItem, isRead, isWrite, isChange, isDelete) values (?,?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, authInfo.getAuthItem());
        pstmt.setString(2, authInfo.getIsRead());
        pstmt.setString(3, authInfo.getIsWrite());
        pstmt.setString(4, authInfo.getIsChange());
        pstmt.setString(5, authInfo.getIsDelete());

        pstmt.executeUpdate();
        pstmt.close();
    }

    @Override
    public void deleteData(Object o) throws SQLException {
        AuthInfo authInfo = (AuthInfo) o;

        Connection conn = DBUtil.getConnection();

        String sql = "DELETE FROM authInfo WHERE authId = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, authInfo.getAuthId());

        pstmt.executeUpdate();
        pstmt.close();
    }

    @Override
    public void updateData(Object o) throws SQLException {
        AuthInfo authInfo = (AuthInfo) o;

        Connection conn = DBUtil.getConnection();

        String sql = "UPDATE authInfo SET authItem = ? ,isRead = ?,isWrite = ?,isChange = ?,isDelete = ? WHERE authId = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, authInfo.getAuthItem());
        pstmt.setString(2, authInfo.getIsRead());
        pstmt.setString(3, authInfo.getIsWrite());
        pstmt.setString(4, authInfo.getIsChange());
        pstmt.setString(5, authInfo.getIsDelete());
        pstmt.setInt(6, authInfo.getAuthId());

        pstmt.executeUpdate();
        pstmt.close();
    }

    @Override
    public int queryDataNum() throws SQLException {

        Connection conn = DBUtil.getConnection();

        String sql = "select count(*) from authInfo;";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();

        int num;
        if (rs.next()) num = rs.getInt("count(*)");
        else num = 0;

        rs.close();
        pstmt.close();

        return num;
    }

    @Override
    public ArrayList<AuthInfo> query(int start, int length) throws SQLException {

        Connection conn = DBUtil.getConnection();

        String sql = "select * from authInfo limit ?, ?;";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, start - 1);
        pstmt.setInt(2, length);
        ResultSet rs = pstmt.executeQuery();

        ArrayList<AuthInfo> list = new ArrayList<>();
        AuthInfo authInfo;

        while (rs.next()) {
            authInfo = new AuthInfo(rs.getInt(1), rs.getString(2), rs.getString(3)
                    , rs.getString(4), rs.getString(5), rs.getString(6));
            list.add(authInfo);
        }

        rs.close();
        pstmt.close();

        return list;
    }

    @Override
    public Object query(Object o) throws SQLException {
        AuthInfo authInfoQuery = (AuthInfo) o;

        Connection conn = DBUtil.getConnection();

        //存在两种查:查ID和查NAME
        String sql = "";
        boolean isQueryId = false;
        if (authInfoQuery.getAuthId() == 0)
            sql = "SELECT * FROM authInfo WHERE authItem = ?";
        else if (authInfoQuery.getAuthItem() == null) {
            sql = "SELECT * FROM authInfo WHERE authId = ?";
            isQueryId = true;
        }

        PreparedStatement pstmt = conn.prepareStatement(sql);
        if (isQueryId) pstmt.setInt(1, authInfoQuery.getAuthId());
        else pstmt.setString(1, authInfoQuery.getAuthItem());
        ResultSet rs = pstmt.executeQuery();

        AuthInfo authInfo = null;
        while (rs.next()) {
            authInfo = new AuthInfo(rs.getInt(1), rs.getString(2), rs.getString(3)
                    , rs.getString(4), rs.getString(5), rs.getString(6));
        }

        if (authInfo == null)
            authInfo = new AuthInfo();

        rs.close();
        pstmt.close();

        return authInfo;
    }

    /**
     * 查询名称
     *
     * @param authInfoQuery 待查询对象
     * @return 查询结果对象
     * @throws SQLException 数据库
     */
    public AuthInfo queryName(AuthInfo authInfoQuery) throws SQLException {
        Connection conn = DBUtil.getConnection();

        String sql = "SELECT * FROM authInfo WHERE authItem = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, authInfoQuery.getAuthItem());
        ResultSet rs = pstmt.executeQuery();

        AuthInfo authInfo = null;
        while (rs.next()) {
            authInfo = new AuthInfo(rs.getInt(1), rs.getString(2), rs.getString(3)
                    , rs.getString(4), rs.getString(5), rs.getString(6));
        }

        if (authInfo == null)
            authInfo = new AuthInfo();

        rs.close();
        pstmt.close();

        return authInfo;
    }
}
最近下载更多
15953970869  LV6 11月9日
张泽帅  LV6 10月18日
angaochong  LV5 10月10日
半夏bx  LV14 9月24日
864216741  LV2 8月25日
odasdas  LV1 6月23日
赵鑫cdsaljkdfsa  LV11 6月18日
yuquanzuo  LV2 5月15日
qiang123  LV8 4月13日
qq1357574774  LV2 3月28日
最近浏览更多
驱蚊器我 11月18日
暂无贡献等级
15953970869  LV6 11月9日
ABU5225  LV1 10月29日
shunlun8855  LV1 10月28日
张泽帅  LV6 10月17日
cuiiii 10月12日
暂无贡献等级
angaochong  LV5 10月10日
暂无贡献等级
半夏bx  LV14 9月24日
zvcbnlk  LV2 9月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友