Tuesday, July 5, 2011

Stored Procedures in Oracle


Working with Stored Procedures in Oracle


Procedures are written in Oracle's PL/SQL programming language.
Sample Procedure
In Notepad, type:
CREATE OR REPLACE PROCEDURE sample
IS
BEGIN
  NULL;
END;

Save your file as sample.sql.
Let’s go through the procedure line by line. The lines are numbered for your convenience:
1 CREATE OR REPLACE PROCEDURE sample
2 IS
3 BEGIN
4   NULL;
5 END;

Line 1:
CREATE OR REPLACE PROCEDURE is an SQL statement that instructs Oracle to create a procedure called sample, and to overwrite it if it exists.
Line 2:
The IS keyword signals that a PL/SQL body will follow.
Line 3:
The BEGIN keyword signals the start of a PL/SQL body.
Line 4:
The NULL PL/SQL statement indicates that no action should be performed. We cannot just leave it out, because at least one statement is required in a PL/SQL body.
Line 5:
The END keyword signals the end of the PL/SQL block.
Now let's move on to creating a procedure.


Read more

Sunday, March 13, 2011

JavaScript : Comaprison of Objects


JavaScript has two different ways of comparing the values of objects for equality.
The equals operator
The equals operator consists of two equal signs: ==
JavaScript features weak typing, that means, that the equals operator coerces types in order to compare them.

""           ==   "0"           // false
0            ==   ""            // true
0            ==   "0"           // true
false        ==   "false"       // false
false        ==   "0"           // true
false        ==   undefined     // false
false        ==   null          // false
null         ==   undefined     // true
" \t\r\n"    ==   0             // true
The above table shows the results of the type coercion and it is the main reason why the use of == is widely regarded as bad practice,
it introduces hard to track down bugs due to its complicated conversion rules.
Additionally there is also a performance impact when type coercion is in play;
for example, a string has to be converted to a number before it can be compared to another number.

The strict equals operator
The strict equals operator consists of three equal signs: ===
Other than the normal equals operator, the strict equals operator does not perform type coercion between its operands.


""           ===   "0"           // false
0            ===   ""            // false
0            ===   "0"           // false
false        ===   "false"       // false
false        ===   "0"           // false
false        ===   undefined     // false
false        ===   null          // false
null         ===   undefined     // false
" \t\r\n"    ===   0             // false
The above results are a lot clearer and allow for early breakage of code.
This hardens code to a certain degree and also gives performance improvements in case the operands are of different types.
Comparing objects
While both == and === are stated as equality operators, they behave different when at least one of their operands happens to be an Object.

{} === {};                            // false
new String('foo') === 'foo';  // false
new Number(10) === 10;   // false
var foo = {};foo === foo;    // true
Here both operators compare for identity and not equality; that is, they will compare for the same instance of the object, much like is in Python and a pointer comparison in C do.
Conclusion
It is highly recommended to only use the strict equals operator. In cases where types need to be coerced, it should be done explicitly and not left to the language's complicated coercion rules.

JavaScript Parser - Sometimes Intelligence causes problems...



JavaScript parser automatically inserts semicolon whenever   it encounters a   parse error due to missing semicolons.

var foo = function() {
} // parse error, semicolon expected
test()
Insertion happens, and the parser tries again.
var foo = function() {
}; // no error, parser continues
test()
The automatic insertion of semicolon is considered to be one of biggest design flaws in the language, as it can change the behavior of code.

How it works

The code below has no semicolons in it, so it is up to the parser to decide where to insert them.
(function(window, undefined) {
    function test(options) {
        writeToLog('testing semicolons...!')

        (options.list || []).forEach(function(i) {

        })

        options.value.test(
            'long string to pass here',
            'and another long string to pass'
        )

        return
        {
            foo: function() {}
        }
    }
    window.test = test
})(window)
(function(window) {
    window.someLibrary = {}
})(window)
Below is the result of the parser's "guessing" game.

Thursday, February 17, 2011

Object Life Cycle in Java


 An object typically goes through most of the following states between the time it is allocated and the time its resources are finally returned to the system for reuse.

  1. Created
  2. In use (strongly reachable)
  3. Invisible
  4. Unreachable
  5. Collected
  6. Finalized
  7. Deallocated
Lets understand these in detail :

Monday, February 14, 2011

java SQL basics 3 : Understanding JDBC Drivers


JDBC drivers are divided into four types or levels. 

The different types of jdbc drivers are:
Type 1: JDBC-ODBC Bridge driver (Bridge)
Type 2: Native-API/partly Java driver (Native)
Type 3: AllJava/Net-protocol driver (Middleware)
Type 4: All Java/Native-protocol driver (Pure)

Type 1 JDBC Driver

JDBC-ODBC Bridge driver
The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.



Type 1: JDBC-ODBC Bridge
Advantage
The JDBC-ODBC Bridge allows access to almost any database, since the database's ODBC drivers are already available.
Disadvantages
1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable.
2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types.
3. The client system requires the ODBC Installation to use the driver.
4. Not good for the Web.
let us learn more..

Java SQL basics 2 : Working with ResultSet Objects in JDBC.


IF you come here directly- you are suggested to read the article http://codingbasics.blogspot.com/2011/02/java-sql-basics-1-processing-sql.html

Updating Rows in ResultSet Objects

You cannot update a default ResultSet object, and you can only move its cursor forward.
However, you can create ResultSet objects that can be scrolled (the cursor can move backwards or move to an absolute position) and updated.

The following method, multiplies the PRICE column of each row by the argument percentage:

  public void modifyPrices(float percentage) throws SQLException {
    Statement stmt = null;
    try {
      stmt = con.createStatement();
      stmt = con.createStatement(
        ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      ResultSet uprs = stmt.executeQuery(
        "SELECT * FROM " + dbName + ".COFFEES");


      while (uprs.next()) {
        float f = uprs.getFloat("PRICE");
        uprs.updateFloat("PRICE", f * percentage);
        uprs.updateRow();
      }


    } catch (SQLException e ) {
      JDBCTutorialUtilities.printSQLException(e);
    } finally {
      stmt.close();
    }
  }
The field ResultSet.TYPE_SCROLL_SENSITIVE creates a ResultSet object whose cursor can move both forward and backward relative to the current position
 and to an absolute position. The field ResultSet.CONCUR_UPDATABLE creates a ResultSet object that can be updated.

The method ResultSet.updateFloat updates the specified column (in this example, PRICE with the specified float value in the row where the cursor is positioned.
ResultSet contains various updater methods that enable you to update column values of various data types.
However, none of these updater methods modifies the database; you must call the method ResultSet.updateRow to update the database.
Learn about Batch Updates using Statement Objects

Java SQL basics 1 : Processing SQL statements with JDBC


In general, to process any SQL statement with JDBC, you follow these steps:

  • Establishing a connection.
  • Create a statement.
  • Execute the query.
  • Process the ResultSet object.
  • Close the connection.

  public static void viewTable(Connection con, String dbName) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from " + dbName + ".COFFEES";
    try {
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery(query);
      while (rs.next()) {
        String coffeeName = rs.getString("COF_NAME");
        int supplierID = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");
        int sales = rs.getInt("SALES");
        int total = rs.getInt("TOTAL");
        System.out.println(coffeeName + "\t" + supplierID + "\t" + price + "\t" + sales + "\t" + total);
      }
    } catch (SQLException e ) {
      JDBCTutorialUtilities.printSQLException(e);
    } finally {
      stmt.close();
    }


Lets break each step and understand the code

subversion video