免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2764 | 回复: 0
打印 上一主题 下一主题

用户,组,权限框架的逻辑层,数据层代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-05 01:13 |只看该作者 |倒序浏览
MySql版本
User.class
/*
* Created on 2005-1-9
*/
package com.jilaninfo.user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jilaninfo.app.Const;
import com.jilaninfo.appserver.server.DBUtil;
import com.jilaninfo.commons.DataTable;
import com.jilaninfo.commons.Tools;
/**
* @author elgs
*/
public class User implements DataTable
{
    private int    id;
    private String user;
    private String pass;
    private String name;
    private boolean check() throws SQLException
    {
        Connection conn = null;
        try
        {
            conn = DBUtil.getConnection(Const.DB_CORE);
            PreparedStatement ps = conn
                    .prepareStatement("select * from user where user=?");
            ps.setString(1, getUser());
            ResultSet rs = ps.executeQuery();
            if (rs.next())
            {
                return false;
            }
            return true;
        }
        finally
        {
            if (conn != null)
                conn.close();
        }
    }
    public boolean load(int id) throws SQLException
    {
        Connection conn = null;
        try
        {
            conn = DBUtil.getConnection(Const.DB_CORE);
            PreparedStatement ps = conn
                    .prepareStatement("select * from user where id=?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next())
            {
                setId(rs.getInt("id"));
                setUser(rs.getString("user"));
                pass = rs.getString("pass");
                setName(rs.getString("name"));
                return true;
            }
            return false;
        }
        finally
        {
            if (conn != null)
                conn.close();
        }
    }
    public void create() throws SQLException
    {
        if (!check())
        {
            throw new RuntimeException("User already existed.");
        }
        Connection conn = null;
        try
        {
            conn = DBUtil.getConnection(Const.DB_CORE);
            conn.setAutoCommit(false);
            PreparedStatement ps = conn
                    .prepareStatement("insert into user (user,pass,name) values(?,?,?)");
            ps.setString(1, getUser());
            ps.setString(2, getPass());
            ps.setString(3, getName());
            ps.executeUpdate();
            ps.close();
            ps = conn.prepareStatement("select LAST_INSERT_ID() from user");
            ResultSet rs = ps.executeQuery();
            if (rs.next())
            {
                setId(rs.getInt(1));
            }
            conn.commit();
        }
        catch (SQLException e)
        {
            conn.rollback();
            throw e;
        }
        finally
        {
            if (conn != null)
                conn.close();
        }
    }
    public void edit(int id) throws SQLException
    {
        if (!check())
        {
            throw new RuntimeException("User already existed.");
        }
        Connection conn = null;
        try
        {
            conn = DBUtil.getConnection(Const.DB_CORE);
            PreparedStatement ps = conn
                    .prepareStatement("update user set user=?,pass=?,name=? where id=?");
            ps.setString(1, getUser());
            ps.setString(2, getPass());
            ps.setString(3, getName());
            ps.setInt(4, id);
            ps.executeUpdate();
        }
        finally
        {
            if (conn != null)
                conn.close();
        }
    }
    public void remove(int id) throws SQLException
    {
        Connection conn = null;
        try
        {
            conn = DBUtil.getConnection(Const.DB_CORE);
            PreparedStatement ps = conn
                    .prepareStatement("delete from user where id=?");
            ps.setInt(1, id);
            ps.executeUpdate();
        }
        finally
        {
            if (conn != null)
                conn.close();
        }
    }
    public String getAllRights() throws SQLException
    {
        String sql0 = "select priv.id from priv,userpriv "
                + "where priv.id=userpriv.privid and userid=? "
                + "union select priv.id from priv,usergroups,groupspriv "
                + "where usergroups.userid=? and priv.id=groupspriv.privid "
                + "and usergroups.groupsid=groupspriv.groupsid";
        StringBuffer sb = new StringBuffer();
        sb.append(0);
        sb.append(",");
        Connection con = null;
        int i = 0;
        try
        {
            con = DBUtil.getConnection(Const.DB_CORE);
            PreparedStatement ps = con.prepareStatement(sql0);
            ps.setInt(1, getId());
            ps.setInt(2, getId());
            ResultSet rs = ps.executeQuery();
            while (rs.next())
            {
                sb.append(rs.getInt(1));
                sb.append(",");
            }
            sb.append(0);
            return sb.toString();
        }
        finally
        {
            if (con != null)
            {
                con.close();
            }
        }
    }
    public boolean hasRight(int privno) throws SQLException
    {
        if (privno == 0)
        {
            return true;
        }
        if (privno

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4960/showart_19446.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP