//
// Source File Name: ColPriv.java 1.3
//
// Licensed Materials -- Property of IBM
//
// (c) Copyright International Business Machines Corporation, 1999.
// All Rights Reserved.
//
// US Government Users Restricted Rights -
// Use, duplication or disclosure restricted by
// GSA ADP Schedule Contract with IBM Corp.
//
// PURPOSE :
// Uses getColumnPrivileges() to get detailed privileges of all columns
// For more information about this sample, refer to the README file.
// For more information on Programming in Java, refer to the
// "Programming in Java" section of the Application Development Guide.
// For more information on building and running Java programs for DB2,
// refer to the "Building Java Applets and Applications" section of the
// Application Building Guide.
// For more information on the SQL language, refer to the SQL Reference.
import java.io.*;
import java.lang.*;
import java.sql.*;
class ColPriv {
static {
try {
// register the driver with DriverManager
// The newInstance() call is needed for the sample to work with
// JDK 1.1.1 on OS/2, where the Class.forName() method does not
// run the static initializer. For other JDKs, the newInstance
// call can be omitted.
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String[] argv) {
Connection con = null;
try {
// connect to server
if (argv.length == 3)
{ // connect using command line arguments
con = Tools.DBConnect(argv[0], argv[1], argv[2]);
}
else
{ // prompt user for database name, user ID, password
con = Tools.DBConnect();
}
con.setAutoCommit (false);
System.out.println ("Enter Table Schema Name:");
String schemaName = Tools.readString().toUpperCase();
System.out.println ("Enter Table Name:");
String tableName = Tools.readString().toUpperCase();
System.out.println ("Enter Search Pattern for Column Name:");
String colSearchPat = Tools.readString().toUpperCase();
// pass returned information to listColPrivileges method
listColPrivileges (con, schemaName, tableName, colSearchPat);
// disconnect and commit transactions
System.out.println ("\n>Disconnecting...");
con.commit();
con.close();
} catch (Exception e) { e.printStackTrace(); }
}
//
// listColPrivileges
// - schemaName and tableName must be exact values NOT search patterns
// - use getColumnPrivileges() to list column privileges that match search
//
public static void listColPrivileges (Connection con, String schemaName, String tableName, String colSearchPat) {
try {
// use getColumnPrivileges to return detailed information about columns
DatabaseMetaData dmd = con.getMetaData();
ResultSet rs = dmd.getColumnPrivileges (null, schemaName, tableName, colSearchPat);
String table_Schema = new String("");
String table_Name = new String("");
String col_Name = new String("");
String schemaOld = new String("");
String tableOld = new String("");
String colOld = new String("");
String grantor = new String("");
String grantee = new String("");
String privilege = new String("");
String isGrantable = new String("");
while (rs.next()) {
// format and read in data for output
table_Schema = rs.getString (2);
table_Name = rs.getString (3);
col_Name = rs.getString (4);
if ( !table_Schema.equals(schemaOld) || !table_Name.equals(tableOld) || !col_Name.equals(colOld) ) {
System.out.println ("\n" + table_Schema + "." + table_Name + "." + col_Name);
System.out.println (" Grantor Grantee Privilege Grantable");
System.out.println (" --------------- --------------- ---------- ---");
schemaOld = table_Schema;
tableOld = table_Name;
colOld = col_Name;
}
grantor = rs.getString (5);
grantee = rs.getString (6);
privilege = rs.getString (7);
isGrantable = rs.getString (8);
System.out.println (" " + Tools.padLength(grantor, 15) + " " + Tools.padLength(grantee, 15) + " " +
Tools.padLength(privilege, 10) + " " + isGrantable);
}
rs.close();
} catch (Exception e) { e.printStackTrace(); }
}
}