Monday, April 23, 2007

Php Sessions

In Php we start session by using session_start(); and we destroy session by session_destroy().
The syntax for storing a value in session is
$_SESSION['variable']="value";

For example , to store username and password
$_SESSION["username"]="XXX";
$_SESSION["password"]="YYYY";

Be careful with this statement as it is case sensitive.Instead of using "$_SESSION" , if you use "$_session" , it is taken as another variable rather than session.

Quote for the Day

The reason why worry kills more people than work is that more people worry than work.

-Robert Frost

Lawyers

What's the difference between a good lawyer and a great lawyer?
A good lawyer knows the law. A great lawyer knows the judge.


A new client had just come in to see a famous lawyer.

Client : Can you tell me how much you charge?.
Lawyer : Of course, I charge $200 to answer three questions!.
Client : Well that's a bit steep, isn't it?
Lawyer :Yes it is. And what's your third question?....
Client : :(....

Saturday, April 21, 2007

Indians are too Intelligent

After digging to a depth of 100 metres last year,Russian scientists found traces of copper wire dating back 1000 years, and came to the conclusion that their ancestors already had a telephone network one thousand years ago.

So, not to be outdone, in the weeks that followed,American scientists dug 200 metres and headlines in the US papers read:"US scientists have found traces of 2000 year oldoptical fibres, and haveconcluded that their ancestors already had advanced high-tech digital telephone 1000 years earlier than the Russians."

One week later, the Indian newspapers reported thefollowing:"After digging as deep as 500 metres, Indian scientists have found absolutely nothing. They have concluded that 5000 years ago, their ancestors were already using wireless technology .

Friday, April 20, 2007

Doesnt Matter where you are

An old man lived alone in a village. He wanted to spade his potato
garden, but it was very hard work. His only son, who would have helped
him, was in prison.

The old man wrote a letter to his son and mentioned his situation:


Dear Son,


I am feeling pretty bad because it looks like I won't be able to plant
my potato garden this year. I hate to miss doing the garden, because
your mother always loved planting time. I'm just getting too old to be
digging up a garden plot. If you were here, all my troubles would be
over. I know you would dig the plot for me, if you weren't in prison.


Love,

Dad


Shortly, the old man received this telegram: "For Heaven's sake, Dad,
don't dig up the garden!! That's where I buried the GUNS!!"

At 4 a.m. the next morning, a dozen FBI agents and local police
officersshowed up and dug up the entire garden without finding any guns.
Confused, the old man wrote another note to his son telling him what
happened, and asked him what to do next.


His son's reply was: "Go ahead and plant your potatoes, Dad.. It's the
best I could do for you from here."

Moral:NO MATTER WHERE YOU ARE IN THE WORLD, IF YOU HAVE DECIDED TO DO SOMETHING DEEP FROM YOUR HEART YOU CAN DO IT. IT IS THE THOUGHT THAT MATTERS NOT WHERE YOU ARE OR WHERE THE PERSON IS.

15 Simple Changes that will energize your life

1. Stop drinking soft drinks and start drinking water.
2. When you feel stressed take 10 deep breaths. Focus on your breathing.
3. Exercise at least 30 minutes a day. It's as easy as a walk around the block.
4. Drink Green Tea instead of Coffee.
5. Sleep at least 8 hours a night (This is difficult for us, but let’s tryJ).
6. Stay away from Hydrogenated Oils. Read the ingredients on the back of products.
7. Surround yourself with positive and supportive people.
8. Do one thing special for yourself every day
9. Eat fruits with your breakfast and eat vegetables with your lunch and dinner.
10. Say a prayer or affirmation before you go to bed and when you wake up
11. Read one inspirational book a month.
12. Listen to your favorite song whenever you need a pick-me-up.
13. Eat breakfast. It will increase your energy and productivity at work.
14. Don't be too busy for lunch. Researchers agree that performance scores plunge when people miss lunch.
15. Take short breaks throughout the day. Get up from your chair and take a little walk. Stop looking at the computer screen. Stretch. Go get a cold drink of water. Short breaks help you refocus and reenergize.

Wednesday, April 18, 2007

Quote for the Day

A group of donkeys lead by a lion will easily win over a group of lions lead by a donkey.

- Socrates

PHP (HypertextPreProcessor)

It is another server-side scripting language used to develop web applications.It is pretty easy to learn and develop web application using php.Being a java programmer i felt the difference while using php.The syntax is very simple as well as the development and the deployment.The database connectivity and session management are just two lines in code.Like java, there is no need to import lots and lots of packages , no need to search for a better framework to implement , no need to choose between application servers ,etc . So its quiet easy to develop and deploy using php.Its better to take of the word deployment. There is no need for deployment.So in order to develop a web application you just need a server that supports php and a database for back-end.Thats it . The most frequently used framework is XAMP.

X - stands for OS (Linux,windows ,etc)
A - stands for Apache Server
M - stands for MySql(Database)
and
P - Php.

Thursday, April 12, 2007

Application/Service for Blackberry

I just tried my hands on building a sample RssReader for Blackberry.It was quite a tedious process at first.I downloaded and installed BlackBerry MDS studio.I tried to develop a sample application as given in the quick start guide.

There are three approaches :

1.Quick start approach
2.Bottom up approach
3.Top Down approach

I used Bottom Up approach.The Blackberry takes a WSDL(Web Service Descrption Language) file as input.The WSDL file specifies the methods,parameters and the URL through which the service can be accessed.Then we need to place the necessary components on the screen and bind them to the appropriate methods.This is something similar to visual basic.Here everything is Drag and Drop.The step by step tutorial to do these are given in the 'Developers guide tutorial ' in the BlackBerry site.

Finally you can test the application with the simulator.This looks pretty simple na? I too felt the same way.But when i started doing my RssReader application , i came to know that there are several tasks to be done in the back end.

Life Cycle :

1.Create a WSDL file :

In order to create a WSDL file , you need to download and install axis2 engine.You also need to have apache ant installed. Then you have to set the classpath to apache ant and jdk.Then create a interface file in java.Its jus t another class file. Then using java2wsdl converter in axis2 generate the WSDL file.For example , in command prompt

%AXIS2_HOME%/bin/java2wsdl -cp . -cn samples.quickstart.service.pojo.StockQuoteService -of StockQuoteService.wsdl

For reference Click here

when you do this you will get a WSDL file.

2.Build service from WSDL file:

In order to build service , use wsdl2java converter of axis2.For example , in command prompt

%AXIS2_HOME%/bin/WSDL2Java -uri resources/META-INF/StockQuoteService.wsdl -p samples.quickstart.service.xmlbeans -d xmlbeans -s -ss -sd -ssi -o build/service

For reference Click here

When you do this you will get a 'build' folder structure , that contains several files and folder.In that open the corresponding skeleton(java) file in the xmlbean directory , inside build directory and implement your logic in that file.

Then use the command "ant jar.server" in the comand prompt , to build the service.If build successfull , you will get a ".aar" file inside that build directory.

3.Implementing the service:

To test it locally , get tomcat installed. Put axis2.war file within the webapps directory of tomcat and it will automatically explode into axis2 directory structure when the tomcat server is started.Then place the .aar file into the service folder within the axis2 directory of the tomcat.

Now restart the tomcat , and the generated WSDL file is given as input to blackberry.This is the overall view of developing a service or application for blackberry.

Wednesday, April 11, 2007

Joins

Types of Joins :

EQUIJOINS or INNERJOIN
NATURAL JOINS
NON-EQUIJOINS
OUTER JOINS
SELF JOINS


EQUIJOINS or INNERJOIN :
The EQUIJOIN joins two tables with a common column in which each is usually the primary key.

The syntax for an EQUIJOIN is

SELECT TABLE1.COLUMN1, TABLE2.COLUMN2...FROM TABLE1, TABLE2 [, TABLE3 ]WHERE TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME[ AND TABLE1.COLUMN_NAME = TABLE3.COLUMN_NAME ]

NATURAL JOINS :

A NATURAL JOIN is nearly the same as the EQUIJOIN; however, the NATURAL JOIN differs from the EQUIJOIN by eliminating duplicate columns in the joining columns. The JOIN condition is the same, but the columns selected differ.

The syntax is as follows:

SELECT TABLE1.*, TABLE2.COLUMN_NAME [ TABLE3.COLUMN_NAME ]FROM TABLE1, TABLE2 [ TABLE3 ]WHERE TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME[ AND TABLE1.COLUMN_NAME = TABLE3.COLUMN ]

NON-EQUIJOINS :

NON-EQUIJOIN joins two or more tables based on a specified column value not equaling a specified column value in another table.

The syntax for the NON-EQUIJOIN is

SELECT TABLE1.*,TABLE2.COLUMN_NAME FROM TABLE1, TABLE2 [, TABLE3 ]WHERE TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME[ AND TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME ]

OUTER JOINS :

An OUTER JOIN is used to return all rows that exist in one table, even though corresponding rows do not exist in the joined table.In many implementations, the OUTER JOIN is broken down into joins called

LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.

LEFT OUTER JOIN

It returns all rows that exist in the table that is to the left of the join, even though corresponding rows do not exist in the joined table that is to the right of the join.

RIGHT OUTER JOIN

It returns all rows that exist in the table that is to the right of the join, even though corresponding rows do not exist in the joined table that is to the left of the join.

FULL OUTER JOIN

It returns only the rows that has the corresponding match in both tables.

SELF JOINS

The SELF JOIN is used to join a table to itself, as if the table were two tables, temporarily renaming at least one table in the SQL statement. Self joins are useful when all of the data you want to retrieve resides in one table, but you must somehow compare records in the table to other records in the table.

The syntax is as follows:

SELECT A.LAST_NAME, B.LAST_NAME, A.FIRST_NAMEFROM EMPLOYEE_TBL A, EMPLOYEE_TBL BWHERE A.LAST_NAME = B.LAST_NAME;

To understand with examples Click Here

categories of the data integrity

Entity Integrity ensures that there are no duplicate rows in a table.

Domain Integrity enforces valid entries for a given column by restricting the type, the format, or the range of possible values.

Referential integrity ensures that rows cannot be deleted, which are used by other records (for example, corresponding data values between tables will be vital).

User-Defined Integrity enforces some specific business rules that do not fall into entity, domain, or referential integrity categories.

Difference between Primary Key and Unique Key

A primary key is a special case of unique keys. The major difference is that for unique keys the implicit NOT NULL constraint is not automatically enforced, while for primary keys it is. Thus, the values in a unique key columns may or may not be NULL.

Point to Remember :

Cannot set primary key constraint to a nullable column.
Cannot drop a column when it has the primary key constraint.
A table cannot have more than one primary key.

Constraints

A constraint is a property assigned to a column or the set of columns in a table that prevents certain types of inconsistent data values from being placed in the column(s). Constraints are used to enforce the data integrity. This ensures the accuracy and reliability of the data in the database.

Some of the constraints are

A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table. The primary key constraints are used to enforce entity integrity.

create table table_name(column_name1 type not null primary key);

A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints.

create table table_name(column_name1 type unique);

A FOREIGN KEY constraint prevents any actions that would destroy link between tables with the corresponding data values. A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce
referential integrity.

create table table_name(column_name1 type not null foreign key references reference_table_name(primary_column_name));

A CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity.

create table table_name(column_name1 type constraint check_columnname check (column_name > 10));

A NOT NULL constraint enforces that the column will not accept null values. The not null constraints are used to enforce domain integrity, as the check constraints.

create table table_name(column_name1 type not null);

A Candidate key uniquely identifies rows in a table. Any of the identified candidate keys can be used as the table's primary key. Any of the candidate keys that is not part of the primary key is called an Alternate key. One can describe a Candidate Key as a Super Key that contains only the minimum number of columns necessary to determine uniqueness.

Saturday, April 7, 2007

Thread synchronization

Consider a scenario ,where two different threads having access to a single instance of a class , and both threads invoke methods on that object and those methods modify the state of the object.

For example , consider a ATM transaction scenario. Suppose there a two people say 'A' and 'B' sharing the same account and each have their own card to withdraw money from ATM.Each goes through a process of checking the available balance and then withdrawing the cash.

Consider 'A' as ThreadA and 'B' as ThreadB.

1.ThreadA checks the balance (balance=Rs.500), and then sleeps for some time.
2.In the mean time , ThreadB checks the balance (balance=Rs.500) and withdraws some cash(withdrawal=200).
3.Now ThreadA wakesup and tries to withdraw the cash of Rs.500 and finds "Not enough money available in your account".

This problem is known as a "race condition," where multiple threads can access the same resource (typically an object's instance variables), and can produce corrupted data if one thread "races in" too quickly before an operation that should be "atomic" has completed.

In this case 'balance' is the variable accessed and modified by both threads , through the methods of checking and withdrawing.

In order to avoid this problem , we implement the concept of synchronization by
1.Marking the variable(balance) private
2.Synchronize the code that modifies the variables.

Synchronization guarants that , when ThreadA has entered the operation of checking and withdrawing , no other thread is allowed to acces or modify the variable(balance) even if ThreadA falls asleep. Other Threads can access and modify , only if ThreadA completes the operation and exits the run method.

sample for the above scenario :




public class AccountDanger implements Runnable
{ 
 private Account acct = new Account();
 public static void main (String [] args) 
 { 
  AccountDanger r = new AccountDanger(); 
  Thread ThreadA = new Thread(r); 
  Thread ThreadB = new Thread(r); 
  ThreadA.setName("ThreadA"); 
  ThreadB.setName("ThreadB");
  ThreadA.start(); 
  ThreadB.start(); 
 } 
 public void run() 
 { 
  for (int x = 0; x <>= amt) 
  { 
   System.out.println(Thread.currentThread().getName()+ " is going to withdraw"); 
   try { Thread.sleep(500); }
   catch(InterruptedException ex) { } 
   acct.withdraw(amt); 
   System.out.println(Thread.currentThread( ).getName( )+ " completes the withdrawal");
   } 
  else 
  { 
   System.out.println("Not enough in account for "+ Thread.currentThread().getName() + " to withdraw "+ acct.getBalance());
  } 
 }
}
class Account 
{ 
 private int balance = 50;
 public int getBalance() { return balance; }
 public void withdraw(int amount) { balance = balance - amount; }
}


In the above sample just add the word "synchronized" to the makeWithdrawal method like "private synchronized void makeWithdrawal" , because it is in this method we call another method that access and modify the variable balance.

Else if you have wrote these operations within the run() method , just put those operation code within the synchronized block like this.



public void run()

{
synchronized(this)
{
//code for the operation goes here
}
}


Points to remember :

1.Synchronization is a proccess of locking the object , so that others dont access it.
2.Others can access only , if the thread releases that object
3.Only methods (or blocks) can be synchronized, not variables or classes
4.It is not necessary that all methods should be synchronized
5.The thread doesnot releases the object even it goes to sleep state.
6.
Each object just has one lock.
7.A thread can acquire more than one lock , when it invokes a synchronized method of another object.

An Inspiring Letter

A soldier stationed in Afghanistan recently received a letter from his girlfriend back home. It read as follows:

Dear Ricky,
I can no longer continue our relationship. The distance between us is just too great. I'm sorry.
Please return the picture of me that I sent to you.
Love, Becky…………..

The soldier, with hurt feelings, asked his fellow soldiers for any snapshots they could spare of their girlfriends, sisters or ex-girlfriends.

In addition to the picture of Becky, Ricky included all the other pictures of the pretty gals he had collected from his buddies. There were 57 photos in that envelope….along with this note:

Dear Becky,
I'm so sorry, but I can't quite remember who the hell you are.
Please take your picture from the pile, and send the rest back to me.
Take Care,Ricky

Moral :If u cant change ur fate, change ur attitude