Chinaunix首页 | 论坛 | 博客
  • 博客访问: 628288
  • 博文数量: 125
  • 博客积分: 8703
  • 博客等级: 中将
  • 技术积分: 1102
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-10 17:48
文章分类

全部博文(125)

文章存档

2012年(2)

2011年(3)

2010年(11)

2009年(1)

2008年(12)

2007年(58)

2006年(38)

分类: Java

2006-11-21 11:30:25

SQL Server 2000中系统表的作用

sysaltfiles    主数据库               保存数据库的文件
syscharsets    主数据库               字符集与排序顺序
sysconfigures  主数据库               配置选项
syscurconfigs  主数据库               当前配置选项
sysdatabases   主数据库               服务器中的数据库
syslanguages   主数据库               语言
syslogins      主数据库               登陆帐号信息
sysoledbusers  主数据库               链接服务器登陆信息
sysprocesses   主数据库               进程
sysremotelogins主数据库               远程登录帐号
syscolumns     每个数据库             列
sysconstrains  每个数据库             限制
sysfilegroups  每个数据库             文件组
sysfiles       每个数据库             文件
sysforeignkeys 每个数据库             外部关键字
sysindexs      每个数据库             索引
sysmenbers     每个数据库             角色成员
sysobjects     每个数据库             所有数据库对象
syspermissions 每个数据库             权限
systypes       每个数据库             用户定义数据类型
sysusers       每个数据库             用户


简单的方法可以使用
sp_tables  @table_type = "'TABLE'"

但是为了熟悉系统表的使用所以使用以下方式:

使用到的表:
sysusers、sysobjects、syspermissions

获取思路:
获取用户的UID。
select uid from dbo.sysusers where  name ='zhang';
获取该用户自己所拥有的表的ID:uid由上边获得
select id from dbo.sysobjects where uid = '5';
获取其他用户授权给它的表:
select id from dbo.syspermissions where grantee = '5';
这样就获取了所有的能访问到的表。
(sa用户特殊用户不适合该方法)

使用SQL实现(可能有更合理的实现方式,请高手指教)
declare @uid char (30);
select @uid = uid  from dbo.sysusers where name='zhangyx';
select o.name,u.name from dbo.sysobjects o,dbo.sysusers u where ((o.id in (select id from dbo.syspermissions where grantee = @uid)) or (o.uid = @uid) )and u.uid = o.uid;

也可以写成一条语句
select o.name,u.name from dbo.sysobjects o,dbo.sysusers u where ((o.id in (select id from dbo.syspermissions where grantee = (select  uid  from dbo.sysusers where name='zhangyx'))) or (o.uid = (select  uid  from dbo.sysusers where name='zhangyx')) )and u.uid = o.uid and o.xtype = 'U';

通过java实现:

可以使用上面的语句实现

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;

public class Tables {

    private Connection conn = null;

    /**
     * 连接数据库用户名和密码
     */
    private String username = "zhangyx";

    private String password = "zhangyx";

    public Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("找不到数据库需要的类!");
            System.exit(1);
        }
        try {

            String dbUrl = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test";
            conn = DriverManager.getConnection(dbUrl, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void close(ResultSet rs, Statement st, Connection conn) {
        try {
            if (rs != null)
                rs.close();
        } catch (Exception e) {
        }
        try {
            if (st != null)
                st.close();
        } catch (Exception e) {
        }
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
    }

    public String getUsername() {
        return this.username;
    }

    public static void main(String[] args) {

        Tables table = new Tables();
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String username = table.getUsername();

        try {
            String sql = "select o.name,u.name from dbo.sysobjects o,dbo.sysusers u where ((o.id in (select id from dbo.syspermissions where grantee = (select  uid  from dbo.sysusers where name='"
                    + username
                    + "'))) or (o.uid = (select  uid  from dbo.sysusers where name='"
                    + username + "')) )and u.uid = o.uid
and o.xtype = 'U';";
            conn = table.getConnection();
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            System.out.println("table_name   | user");
            while (rs.next()) {
                System.out.print(rs.getString(1));
                System.out.print("   | ");
                System.out.println(rs.getString(2));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            table.close(rs, pst, conn);
        }

    }

}



阅读(3056) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~