HashMap:
- hashmap is not support synchronized
- allow null value
- cannot be shared with multiple thread
HashTable:
- Support synchronized
- not allow null value
- share multiple thread
Saturday, October 5, 2013
Thursday, October 3, 2013
Exception PL/SQL
PL/SQL error management features
1- Defining exceptions
2- Raising exceptions
3- Handing exceptions
We will explain every topic individually
Has just two attributes: code and message.
You can RAISE and handle an exception, but it cannot be passed as an argument in a program.
syntax
a-RAISE raises the specified exception by name.
RAISE; re-raises current exception. Callable only within the exception section. b-RAISE_APPLICATION_ERROR
Communicates an application specific error back to a non-PL/SQL host environment.
Error numbers restricted to the -20,999 - -20,000 range.
Syntax :-
RAISE_APPLICATION_ERROR (num binary_integer, msg varchar2,
Example :
RAISE_APPLICATION_ERROR (-20070, ‘Employee must be more than 18 years old.’);
b-We can use the following functions at exception section:
–SQLCODE --> get error number.
–SQLERRM--> get error message.
–DBMS_UTILITY.FORMAT_ERROR_STACK
Returns exception in more details.
–DBMS_UTILITY.FORMAT_ERROR_BACKTRACE
Returns the full stack of errors with line number information.number that raised the error.
This stack is available only if you the error is unhandled.
c-The DBMS_ERRLOG package
–Quick and easy logging of DML errors
d- We can use The AFTER SERVERERROR trigger to log any error have been raised at database.
1- Defining exceptions
2- Raising exceptions
3- Handing exceptions
We will explain every topic individually
1-Defining exceptions
The EXCEPTION is a limited type of data. Has just two attributes: code and message.
You can RAISE and handle an exception, but it cannot be passed as an argument in a program.
syntax
pragma exception_init(error_name,-error_number);
example :-declare
bulk_erros exception
pragma exception_init(bulk_erros,-20006);
begin
...
end;
2-Raising exceptions a-RAISE raises the specified exception by name.
RAISE; re-raises current exception. Callable only within the exception section. b-RAISE_APPLICATION_ERROR
Communicates an application specific error back to a non-PL/SQL host environment.
Error numbers restricted to the -20,999 - -20,000 range.
Syntax :-
RAISE_APPLICATION_ERROR (num binary_integer, msg varchar2,
keeperrorstack boolean default FALSE);
Example :
RAISE_APPLICATION_ERROR (-20070, ‘Employee must be more than 18 years old.’);
3-Handing exceptions
a-The EXCEPTION section consolidates all error handling logic in a block. b-We can use the following functions at exception section:
–SQLCODE --> get error number.
–SQLERRM--> get error message.
–DBMS_UTILITY.FORMAT_ERROR_STACK
Returns exception in more details.
–DBMS_UTILITY.FORMAT_ERROR_BACKTRACE
Returns the full stack of errors with line number information.number that raised the error.
This stack is available only if you the error is unhandled.
c-The DBMS_ERRLOG package
–Quick and easy logging of DML errors
d- We can use The AFTER SERVERERROR trigger to log any error have been raised at database.
Calling Stored Procedures or Functions from Code Java
To invoke a stored procedure or function from within an entity object or an application module, you need to follow below steps:
1. Create a CallableStatement with the PL/SQL block containing the stored procedure or function invocation
2. Bind any parameters ( IN or OUT )
3. Execute the statement.
4. Optionally retrieve the values of any OUT parameters or return value of function
5. Close the statement.
I will illustrate previous steps in below examples
1-Stored Procedure with OUT parameter
2- Stored Function
1. Create a CallableStatement with the PL/SQL block containing the stored procedure or function invocation
2. Bind any parameters ( IN or OUT )
3. Execute the statement.
4. Optionally retrieve the values of any OUT parameters or return value of function
5. Close the statement.
I will illustrate previous steps in below examples
1-Stored Procedure with OUT parameter
public void testStoredProcedure() {
OADBTransaction txn = getDBTransaction();
CallableStatement callableStatement =
txn.createCallableStatement("begin xxx_procedure(:1, :2); end;",OADBTransaction.DEFAULT);
try {
callableStatement.registerOutParameter(2, Types.VARCHAR);
callableStatement.setString(1, "mahmoud");
String outParamValue = null;
callableStatement.execute();
outParamValue = callableStatement.getString(1);
callableStatement.close();
} catch (SQLException sqle) {
callableStatement.close();
}
}
2- Stored Function
public void testFunction() {
OADBTransaction txn = getDBTransaction();
CallableStatement callableStatement =
txn.createCallableStatement("begin :1 := xx_function(:2); end;",OADBTransaction.DEFAULT);
try {
callableStatement.registerOutParameter(1, Types.VARCHAR);
callableStatement.setString(1, "mahmoud");
String outParamValue = null;
callableStatement.execute();
outParamValue = callableStatement.getString(1);
callableStatement.close();
} catch (SQLException sqle) {
callableStatement.close();
}
}
Thinking in ROWID
We hear ‘ROWID’ many times from database developers, So we should think about what purpose it serves.
Oracle says, ‘They are the fastest way to access a single row’
Oracle says, ‘They are the fastest way to access a single row’
ROWID gives us the location of the data for a given row in a table. The datafile – then the position of the row in the block, then the data block in the datafile.
As you can imagine, this data is UNIQUE. So, each row in your table will have a unique ROWID.
This is only because the data for 2 rows can’t exist in the same space.
It’s not uncommon for a table to lack any unique columns or PKs or unique indexes. But, if you know the ROWID, then you will have at least 1 unique differentiator for that record.
It’s also important to remember that a record’s ROWID is not guaranteed to remain unchanged.
Since it’s a PSEUDO column, it’s not actually stored in the database. That means we can’t INSERT, UPDATE, DELETE ROWID. But, we can use it in a SELECT or WHERE.
Oracle is saying ROWIDs aren’t stored in a table. They can and WILL be stored in an INDEX. Indexes allow us to quickly jump to the records we are looking for. However, the ROWID is dynamically assembled as you retrieve that data.
Main majors use of ROWIDs
1. Use it to delete duplicate rows from your table.
2. Use it to edit SELECT results in Toad for Oracle
2. Use it to edit SELECT results in Toad for Oracle
Deleting Records
Add ‘ROWID’ to your SELECT string. Once you’ve identified the records you want to delete, just record those values. Then issue your ‘DELETE FROM TABLE WHERE ROWID in (…).
Add ‘ROWID’ to your SELECT string. Once you’ve identified the records you want to delete, just record those values. Then issue your ‘DELETE FROM TABLE WHERE ROWID in (…).
select emp.ROWID, empno , ename
from scott.emp;
If you query a view and include ROWID, you may get a
ORA-01445: cannot select ROWID from, or sample, a join view without a key-preserved table
Your view could be pulling data from multiple source tables. These tables need a PK apparently. This Oracle’s way of saying that it can’t determine the the ROWID based on what you are querying. Also, your row could be pulling from multiple sources, so there would be theoretically multiple ROWIDs
Subscribe to:
Posts (Atom)