//***************************************************************************
// (c) Copyright IBM Corp. 2007 All rights reserved.
//
// The following sample of source code ("Sample") is owned by International
// Business Machines Corporation or one of its subsidiaries ("IBM") and is
// copyrighted and licensed, not sold. You may use, copy, modify, and
// distribute the Sample in any form without payment to IBM, for the purpose of
// assisting you in the development of your applications.
//
// The Sample code is provided to you on an "AS IS" basis, without warranty of
// any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
// IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
// not allow for the exclusion or limitation of implied warranties, so the above
// limitations or exclusions may not apply to you. IBM shall not be liable for
// any damages you suffer as a result of using, copying, modifying or
// distributing the Sample, even if IBM has been advised of the possibility of
// such damages.
//***************************************************************************
//
// SOURCE FILE NAME: DbAuth.sqlj
//
// SAMPLE: Grant, display or revoke privileges on database
//
// SQL Statements USED:
// GRANT (Database Authorities)
// SELECT
// FETCH
// REVOKE (Database Authorities)
// COMMIT
//
// Classes used from Util.sqlj are:
// Db
// Data
// SqljException
//
//
// Output will vary depending on the JDBC driver connectivity used.
//***************************************************************************
//
// For more information on the sample programs, see the README file.
//
// For information on developing Java applications see the Developing Java Applications book.
//
// For information on using SQL statements, see the SQL Reference.
//
// For the latest information on programming, compiling, and running DB2
// applications, visit the DB2 Information Center at
// http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
//**************************************************************************/
import java.lang.*;
import java.sql.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
#sql iterator DbAuth_Cursor(String, String, String, String,
String, String, String, String);
class DbAuth
{
public static void main(String argv[])
{
try
{
Db db = new Db(argv);
System.out.println();
System.out.print("THIS SAMPLE SHOWS HOW TO GRANT/DISPLAY/REVOKE ");
System.out.println("AUTHORITIES ON DATABASE.");
// connect to the 'sample' database
db.getDefaultContext();
grant();
display();
revoke();
// disconnect from the 'sample' database
db.disconnect();
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // main
// This function shows how to grant user authorities on database
static void grant()
{
System.out.println();
System.out.println(
"----------------------------------------------------------\n" +
"USE THE SQL STATEMENTS:\n" +
" GRANT\n" +
" COMMIT\n" +
"TO GRANT AUTHORITIES AT DATABASE LEVEL.\n");
try
{
System.out.println(
" GRANT CONNECT, CREATETAB, BINDADD\n" +
" ON DATABASE\n" +
" TO USER user1");
#sql {GRANT CONNECT, CREATETAB, BINDADD
ON DATABASE
TO USER user1};
System.out.println();
System.out.println(" COMMIT");
#sql {COMMIT};
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // grant
// helping function: This function displays the authorities for a
// user on a database
static void display()
{
System.out.println();
System.out.println(
"----------------------------------------------------------\n" +
"USE THE SQL STATEMENT:\n" +
" SELECT\n" +
"TO DISPLAY AUTHORITIES FOR ANY USER AT DATABASE LEVEL.");
System.out.println();
System.out.println(
" SELECT granteetype, dbadmauth, createtabauth,\n" +
" bindaddauth, connectauth, nofenceauth,\n" +
" implschemaauth, loadauth\n" +
" FROM syscat.dbauth\n" +
" WHERE grantee = 'USER1'");
// retrieve and display the result from the SELECT statement
try
{
DbAuth_Cursor cur;
String granteetype = null;
String dbadmauth = null;
String createtabauth = null;
String bindaddauth = null;
String connectauth = null;
String nofenceauth = null;
String implschemaauth = null;
String loadauth = null;
#sql cur ={SELECT granteetype, dbadmauth, createtabauth,
bindaddauth, connectauth, nofenceauth,
implschemaauth, loadauth
FROM syscat.dbauth
WHERE grantee = 'USER1'};
#sql {FETCH :cur INTO :granteetype, :dbadmauth, :createtabauth,
:bindaddauth, :connectauth, :nofenceauth,
:implschemaauth, :loadauth};
System.out.println();
System.out.println(
" Grantee Type = " + granteetype + "\n" +
" DBADM auth. = " + dbadmauth + "\n" +
" CREATETAB auth. = " + createtabauth + "\n" +
" BINDADD auth. = " + bindaddauth + "\n" +
" CONNECT auth. = " + connectauth + "\n" +
" NO_FENCE auth. = " + nofenceauth + "\n" +
" IMPL_SCHEMA auth. = " + implschemaauth + "\n" +
" LOAD auth. = " + loadauth);
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // display
// This function shows how to revoke user authorities on a database
static void revoke()
{
System.out.println();
System.out.println(
"----------------------------------------------------------\n" +
"USE THE SQL STATEMENTS:\n" +
" REVOKE\n" +
" COMMIT\n" +
"TO REVOKE AUTHORITIES AT DATABASE LEVEL.");
try
{
System.out.println();
System.out.println(
" REVOKE CONNECT, CREATETAB, BINDADD\n" +
" ON DATABASE\n" +
" FROM USER user1");
#sql {REVOKE CONNECT, CREATETAB, BINDADD
ON DATABASE
FROM USER user1};
System.out.println();
System.out.println(" COMMIT");
#sql {COMMIT};
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // revoke
} // DbAuth