Wednesday, December 1, 2010

Taglib Directive

Syntax:
<%@ taglib {uri="URI" | tagdir="/WEB-INF/tags[/subdir]+"} prefix="tagPrefix" %>

Example:
<%@ taglib uri="http://www.jspcentral.com/tags" prefix="public" %>
<public:loop>
...
</public:loop>

If the URI is a URL or URN, then the TLD is located by consulting the mapping indicated in web.xml extended using the implicit maps in the packaged tag libraries. If URI is pathname, it is interpreted relative to the root of the web application and should resolve to a TLD file directly, or to a JAR file that has a TLD file at location META-INF/taglib.tld.

http://java.sun.com/products/jsp/syntax/2.0/syntaxref2012.html

Tuesday, November 30, 2010

How to create zip file from command line

This would be useful if you have a list of files name (with their path) to be zipped but these files are present across multiple folders. By adding one file at a time from user interface, it would take a lot of time. Think if you have to create this file again if any error occur.

Winzip has a command line utility for this but requires 12.x version or above. I used WinRar, created a .rar file from command line and then created a zip file from that. Of course there would be other ways of achieving it and some might feel this as an overhead but this was the best for my current machine without having to install any new software.

Steps to do
  1. Save the name of files (with their path) to be zipped into a text file and save it as files.lst or any other name. If you want to give relative path rather than absolute path, just make sure that in such case, this file is present at the root of your relative path.
  2. From command prompt, go to relative path's root folder and run rar a backup @files.lst where backup is name of generated rar file. If you get error, perhaps WinRar is not in your Path. In such case run "C:\Program Files\WinRAR\rar" a backup @files.lst
  3. For list of other options you can refer to "Console Rar Manual" file that is located in your Start menu itself under WinRAR group.
  4. Once you have taken out your required files into a rar file, you can use any format like 7z or zip etc. to store these files.

Tuesday, August 31, 2010

Oracle tips

'Select Into' using dynamic sql
execute immediate 'select count(*) from dual' INTO l_cnt;
OR
sql_stmt := 'SELECT COUNT(*) FROM BACKUP_TABLE_NAME WHERE BACKUP_ID = :1';
EXECUTE IMMEDIATE sql_stmt INTO total USING state_id;

Using ampersand without variable substitution
set define off
OR
insert into companies values ('Bob &' || ' Sons');
OR
set scan off

User and Tablespace mapping
select USERNAME, DEFAULT_TABLESPACE, TEMPORARY_TABLESPACE
from dba_users
order by USERNAME


Monday, August 30, 2010

Oracle: Fetch list of Foreign Keys of other tables pertaining to current table

SELECT c.table_name table_name, c.constraint_name FOREIGN_KEY_NAME
FROM user_constraints c, user_constraints p
WHERE c.r_constraint_name = p.constraint_name
AND c.r_owner = p.owner
AND c.constraint_type='R'
AND p.table_name = 'YOUR_TABLE_NAME';

If you want to know which column in other table is referring to current table

SELECT c.table_name table_name, c.constraint_name FOREIGN_KEY_NAME, a.column_name
FROM user_constraints c, user_constraints p, SYS.ALL_CONS_COLUMNS a
WHERE c.r_constraint_name = p.constraint_name
AND a.constraint_name = c.constraint_name
AND c.r_owner = p.owner
AND c.owner = a.owner
AND c.constraint_type='R'
AND p.table_name = 'YOUR_TABLE_NAME';

Thursday, March 4, 2010

How to create and use User-Defined exceptions in Oracle

To handle exceptions in Oracle, either use OTHERS handler or create your own exception.

To create your own exception, you need to define it first. After defining your exception, you initialize your exception by using pragma EXCEPTION_INIT. This tells the compiler to associate an exception name with an Oracle error number. That lets you refer to any internal exception by name and to write a specific handler for it.

Note that a pragma is a compiler directive that is processed at compile time, not at run time.

You code the pragma EXCEPTION_INIT in the declarative part of a PL/SQL block, subprogram, or package using the syntax

PRAGMA EXCEPTION_INIT(exception_name, -Oracle_error_number);
where exception_name is the name of a previously declared exception and the number is a negative value corresponding to an ORA- error number.


DECLARE
deadlock_detected EXCEPTION; -- DECLARED AN EXCEPTION
PRAGMA EXCEPTION_INIT(deadlock_detected, -60); -- INITIALIZE THE EXCEPTION
BEGIN
null; -- Some operation that causes an ORA-00060 error
EXCEPTION
WHEN deadlock_detected THEN -- REFER USER DEFINED EXCEPTION
null; -- handle the error
END;
/

The following sample helps you understand it better


declare
index_not_exists exception;
pragma exception_init(index_not_exists, -01418);
begin
execute immediate 'drop index rhnChecksum_chsum_idx';
execute immediate 'alter table rhnChecksum add constraint rhnChecksum_chsum_uq
unique (checksum, checksum_type_id)
using index tablespace [[32m_tbs]]';
exception
when index_not_exists then
null; -- index was already dropped
end;
/


References:
  1. http://download.oracle.com/docs/cd/B14117_01/appdev.101/b10807/07_errs.htm
  2. http://git.fedorahosted.org/git/?p=spacewalk.git;a=commitdiff;h=e6aba2e66d3e645936800b233f7802b2c6b29e88