Saturday, December 17, 2011

Understanding the XML Namespace

If your a java programmer the probability that you have run into XML configuration is pretty high. Many frameworks require XML based configuration and even when they offer alternatives (usually Annotations) they usually recommend using XML.

Whether you like it or not XML is everywhere.

So what exactly is the XML namespace. Many a times it just some boiler plate that's added to the beginning of a config document.


Use Spring? Here is what you might have,

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <bean id="xyz" class="blogspot.javachats.XYZ"></bean>

    <util:properties id="someConfiguration" 
         location="classpath:blogspot/javachats/some.properties"/>
</beans>

So what is the XML namespace?

All the tags that start with xmlns form the XMLNameSpace tags.

In this example we have three namespaces defined - 1 is implicitly used (referred as Namespace Defaulting) and 2 are explicitly named (referred as Qualified Names). Were you able to identify which is which?

  1.  Namespace Defaulting
    The namespace xmlns="http://www.springframework.org/schema/beans" defines a default namespace. It means that a Non-Qualified tag against which it is written and any Non-Qualified child tags would by in that namespace. In case of the spring example above - the <beans> and <bean> element belong to the namespace http://www.springframework.org/schema/beans
     
  2.  Qualified Names
    The namespaces xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" and xmlns:util="http://www.springframework.org/schema/util" are qualified namespaces. It means any element that are prefix with xsi or util would be part of that respective namespace!
Did you spot where the namespace xsi is used in the example above?
Why do they matter?

In pretty simple terms the name of any XML element (referred as QName) comprises of the XML NameSpace + Local Name.
e.g. QName(Beans) = http://www.springframework.org/schema/beans/ + beans
This means two different XML element may have the same name in different Namespaces.

Namespaces in XML are the Java Equivalent of Packages!

That's XML Namespaces in nutshell1. Next time you look at some boiler plate XML - be sure to give it a second glance.

Foot Notes
  1. The XML Name Reference at w3.org

Monday, December 12, 2011

Tip 1 - Exception Logging in Java

TIP #1 - Always Use The toString() Method Of The Exception Class Instead Of getMessage().

One of the common patterns in exception logging that I see a lot (and I've used it myself) is

catch(Exception e)
{
    logger.error ("Exception Message : " + e.getMessage());
}

The problem with e.getMessage() is that for lot of Exceptions (including many thrown by in build java classes) its value is either null or a cryptic string. Without any information on the exception type it makes it very difficult to determine what actually went wrong.

Consider these rather simple example

  • when a null pointer exceptions is thrown

    //Exception Message : null
    System.out.println("Exception Message : " + e.getMessage());
    
    //Exception Message : java.lang.NullPointerException
    System.out.println("Exception Message : " + e.toString());
    

  • when an Number Format Exception is thrown

    //Exception Message : For input string: "x"
    System.out.println("Exception Message : " + e.getMessage());
    
    //Exception Message : java.lang.NumberFormatException: 
    For input string: "x"
    System.out.println("Exception Message : " + e.toString());
    
Which of the two messages would you rather see in your logs?

These example are typical of what e.getMessage() returns and without the Exception type the message-text is just a cryptic text value.

Thursday, December 1, 2011

Advance Database Part 1 - Joins Revisited.

There is a lot more to SQL & Oracle Queries1 than just Select From Where clause(s) which we generally use to write most queries. In this series of post I would be revisiting some of the advance SQL & Database concepts.

Joins - join's are pretty simple & relatively well know concept. Its something most of us use day in and day out. However there different type of joins with stubble difference among them - This post should serve as a quick view of the different types of joins we could encounter.

Employee Table
LastName DepartmentID
Rafferty 31
Jones 33
Steinberg 33
John NULL
Department Table
DepartmentID DepartmentName
31 Sales
33 Engineering
34 Clerical
35 Marketing

  1. Join or Inner Join - The usual joins which we deal with everyday are Inner Joins or simply called Joins. There are three kinds of inner joins
  • Equi-join - This are the most common type of inner join used. Two tables are combined based on an equality condition, usually a foreign key in the first table is equated with a primary key in the second table.

    Usual way to write the query,
    SELECT *
    FROM   employee, department 
    WHERE  employee.DepartmentID = department.DepartmentID;
    

    SQL Syntax
    SELECT *
    FROM   employee JOIN department 
           ON employee.DepartmentID = department.DepartmentID;
    

    Alternate SQL Syntax
    SELECT *
    FROM   employee JOIN department 
           USING (DepartmentID);
    

    Are all the 3 syntax equivalent? No really, there are difference in the Column that show up2 in the end result. However all three produce the same rows.

    Results
    Employee-Department Table
    LastName Employee.DepartmentID Department .DepartmentID DepartmentName
    Rafferty 31 31 Sales
    Jones 33 33 Engineering
    Steinberg 33 33 Engineering

    1. The employee John who has not been allocated a department is not included in the result set.
    2. The departments Clerical and Marketing which have no employees are not included in the result set.

    Hot Tip
    Equi-Joins require a matching row to be present on both sides of the table for the join to happen.

  • Natural Join - A join on the two tables automatically using  the column names that match between the two tables. This isn't usually used, nor are they recommended. The danger comes from inadvertently adding a new column with a name that matches the other table. This means that any existing natural join will start comparing rows with different criteria than before.

    SQL Syntax
    SELECT *
    FROM   employee NATURAL JOIN department; 

    Results
    Same as the Equi-Join since in this example both the table have only 1 common column name - DepartmentID. The result set would contain only one DepartmentId column without any prefix table name.

  1. Outer Join - Unlike an inner join, outer join does not require each row in the table to match with an row in the other table.
    Hot Tip
    Outer joins are classified depending on the table who's rows are included in the result even when there are no matching rows in the other table.
  • Left Outer Join - In a left outer join - all the rows in the left table would always be present in the output even if the matching row is not present in the right hand table. When a matching right hand row is not present, the row in the left hand table is represented with all null values for the right hand table.

    SQL Syntax
    SELECT *  
    FROM   employee  LEFT OUTER JOIN department  
              ON employee.DepartmentID = department.DepartmentID;
    

    Result
    Employee-Department Table
    LastName Employee.DepartmentID DepartmentName Department.DepartmentID
    Rafferty 31 Sales 31
    Jones 33 Engineering 33
    Steinberg 33 Engineering 33
    John NULL NULL NULL

  • Right Outer Join - A right outer join is similar to a left outer join except that all the rows in the right table would always be present in the output. For most practical cases the right outer join isn't used and instead replaced by an equivalent Left Outer Join by interchanging the left and right table.
     
  • Full Outer Join - In a full outer join all rows from both the tables are present in the result-set. Row which do not have a matching condition are represented once with null values filled in for the other table.

    SQL Syntax
    SELECT *  
    FROM   employee 
           FULL OUTER JOIN department 
              ON employee.DepartmentID = department.DepartmentID;
    

    Result
    Employee-Department Table
    LastName Employee.DepartmentID DepartmentName Department.DepartmentID
    Rafferty 31 Sales 31
    Jones 33 Engineering 33
    Steinberg 33 Engineering 33
    John NULL NULL NULL
    NULL NULL Clerical 34
    NULL NULL Marketing 35

  1. Cross Joins - Returns the Cartesian product of rows from tables involved in the join. Rarely useful.

    Usual way of writing the query,
    SELECT *
    FROM   employee, department;

    SQL Syntax
    SELECT *
    FROM   employee 
    CROSS JOIN department
    

    Results
    This would result in 16 rows - each row in the left hand table would be joined with each single row in the right hand table,
    Employee Table
    LastName Employee.DepartmentID Department .DepartmentID DepartmentName
    Rafferty 31 31 Sales
    Rafferty 31 33 Engineering
    Rafferty 31 34 Clerical
    Rafferty 31 35 Marketing
    ...
    John NULL 31 Sales
    John NULL 33 Engineering
    John NULL 34 Clerical
    John NULL 35 Marketing


  1. Self Joins - When both tables involved in the join is the same table - the join is said to be a self join. A self join may either be a inner join, outer join or a Cartesian product.
Foot Notes
  1. Oracle Queries = That's the database I use day in and day out. Oracle is what I'm interested in, thought most of this is pure SQL. 
  2. Reference - The post is based heavily on the wikipedia article on SQL Joins.

Sunday, November 27, 2011

Wikipedia and the small donation!

Been a heavy wikipedia user for a long long time now. So it was time to give back in a small small way - a 300 buck donation.

I always loved the idea of wikipedia - their goal "Imagine a world in which every single person on the planet is given free access to the sum of all human knowledge." isn't that far from what they have managed to achieve.

I always been a great fan & most importantly of what they have been able to achieve. Its been an inspiration of what out-of-the-box thinking can get you!

Wishing them many great years ahead!

The banner below takes you to the wikipedia donate page. Go on, give it a gentle click :)!

Support Wikipedia

Saturday, November 26, 2011

Let's Talk

Its been over one years since I wrote my last blog post on twisters a java puzzle site I started some time back. It was a lot of fun while it lasted, and I'm really sad that it had to end. All things need an end - I only wish I could have ended it on a high note - and not so arbitrarily.

This blogs a bit different on few accounts. Its a mixed blog. Of course there going to tons on Java and frameworks but also the occasional (and maybe a bit more) personal post.

The one thing I hope this blog helps me do is connect with like minded people. I want to to do more than just write about technology. I really want to connect, hear opinion, get corrected, discuss ... you get the drift.

I'm hoping to have as much fun writing this time around - as much as I had with twisters!! Maybe a lot more - after all I've learned a lot since then!

Would love to hear from you too ....