Tuesday, September 9, 2008

Sunday, April 13, 2008

Java - why object references declared inside method remain uninitialized?

Object reference declared inside class gets initialized to “null”, but same is not true for object reference declared inside method.

Works, ha ha: java variable to be initialized to null

Thursday, March 27, 2008

IE Problems with Javascript

Most faced errors in IE (6 or 7):

1. “Error: Expected identifier, string or number in IE”

If working javascript in firefox start throwing above error, then you have forgotten to remove the comma at end of the function. More info

2. The “setAttribute” function does not work correctly in IE.
Instead of object.setAttribute("size", length), use object.size = length;

3. If object is not already within the table, IE returns 'null' when you call object.insertRow() or object.insertCell()
More info

Sunday, February 3, 2008

Meta refresh redirect - Don't use it

Meta refresh breaks the Back button - http://www.w3.org/QA/Tips/reback

Sample code for Meta refresh redirect:

<html>

<head>
<title> Redirecting to correct page </title>
<META http-equiv="refresh" content="5;URL=http://jhpatel.blogspot.com/"> </head>

<body>
<center>
You will be redirected automatically in 5 seconds. Please bookmark the page at
<a href="http://jhpatel.blogspot.com/" > http://jhpatel.blogspot.com/</a>
</center>
</body>

</html>


Better way :

HTTP 301 Redirect - Permanent redirect (In JSP)

response.setStatus(301);

response.setHeader("Location", "http://www.jhpatel.blogspot.com/");

response.setHeader("Connection", "close");

Saturday, February 2, 2008

Implement browser back button in your page

Add following line in your html page:

<input value="back" onclick="history.go(-1)" type="button">

If you want user to always go back one page, you can use history.back() Instead of history.go(-1).

Monday, January 28, 2008

Write Web application using only one Servlet!

What do you think?...not a good idea? Then what Front Controller Framework like Struts does?

(NOTE: All Jsps in your web app gets converted into Servlets!...but don't worry for that.)

In FC framework, you have only one servlet and all the requests are dispatched to the proper Action Classes based on the mapping in your struts-config.xml.

Let me write my own FrontController..I am not going to use any .xml file..I will be hardcoding :-)

Public class MyFrontControllerServlet{

public void execute()
{
if(requestparam == 'add') {

Call ActionClass1();

}
else if (requestparam == 'delete')
{
Call ActionClass2();
}
.
.
.
.
.
}
}

Very simple!! You can write your application in one servlet + JSPs (JSPs get converted into servlets internally by Jasper engine.)

This is my crazy thinking. Please don’t do this, otherwise you might get fired .Feeling beat up

Difference between N:1 and 1:N association from O/R mapping framwork and database point of view?

Well, you will say it's same and you are right! Still, I want to write something .

Database: 1:N = Inverse (N:1) ?
In database, we can map "One to Many" and "Many to one" relationship between tables by creating Foreign key (FK) column for table on the "Many" side of the relationship.

Say for example, we want to map "One to Many" (1:N) relationship between Customer and Order. "One customer can have multiple Orders. But one Order can have only one Customer associated"

Which table is on the "Many" side of the relationship? - Order table. So we can create Foreign Key(FK) in Order table pointing to Primary key(PK) in Customer table.

You have created both 1:N (From Customer to Order) and N:1 ( From Order to Customer) mapping in your database!!!

Query - "Give me all the order names for the given customer name"?
- Select order_name from Order,Customer where customer_name="jay";

The above query is nothing but just a INNER JOIN. Is there any better way to write same query?--(Have you heard of Lazy Loading?)

OR Mappers : 1:N = Inverse (N:1) ?

Order class: (JPA Annotations used)
@ManyToOne
@JoinColumn(name="customer_id") //customer_id is a FK field in Order table.
public Customer getCustomer();
public void setCustomer(Customer customer);

You have mapped "Many to One" relationship, but not "One to Many" yet.

you have two options to get the data using Order.getCustomer() :: Lazy Loading(On demand) or JOIN(Eager Loading/Pre-Loading/Pre-Fetching)

Customer class: (JPA Annotations used)
@OneToMany(mappedBy = "customer_id") //customer_id is a FK field in Order table.
List getOrders();
void setOrders(List orders);

Now you have mapped "One to Many" relationship. You are ready to traverse in both way now from Customer -> Order and Order -> Customer in your code.

Under the cover, OR mapppers use Primary/Foreign key mechanism to map 1:N and N:1 relationship..but please do not change your annotation blindly in your POJO "@ManyToOne" != "@OneToMany"

Little bit more hammering:>>>>>> If you want to map many-to-many relationship in between two database tables, you will need one Associate table or JOIN table. This JOIN table will have two foreign keys pointing to the primary key of the two entity tables. So you will have Many-to-One mapping from JOIN table to each entity table.

Sunday, January 27, 2008

Convert MyISAM to INNODB: mysqldump

"C:\>mysqldump -h localhost -u username -p databasename > filename.sql"
Enter password: ****

This will dump your mysql database with structure of all the tables and their content.

If you want to convert your tables from MyISAM to INNODB, you can do "find MyISAM and replace with INNODB" in this filename.sql before importing into database again.

Make sure you have INNODB support enabled for your MySQL server 5.0
- Open file MySQL Server 5.0\bin\my.ini and comment out "skip-innodb"
- AND Don't forget to restart your server after the change.

To import your dump file into database:
"C:\> mysql -h localhost -u username -p databasename < filename.sql " Enter password: **** I am not sure if this is a reliable way for the production databases. -:)
More Info on mysqldump: http://www.devshed.com/c/a/MySQL/Backing-up-and-restoring-your-MySQL-Database

Some tools

1. KeyPassword Safe tool: http://keepass.info/
I like It's neat and clean Windows UI. Disadvantage is that it is not online tool, that means you can not access your passwords from anywhere in the world!.

2. Notepad ++ : Nice and fast.

3. Hitask.com and rememberthemilk.com is really cool in managing tasks/calendar events

mysql> show create table 'tablename'

mysql> show create table 'tablename' -- is good query (atleast for me) to know about table and which storage engine/charset it uses (InnoDB or MyISAM).

mysql> show create table task
-> ;
+-------+--------------------------------------------------
-----------------------------------------------------------
-----------------------------------------------------------
-----------------------------------------------------------
-------------------------+
| Table | Create Table



|
+-------+--------------------------------------------------
-----------------------------------------------------------
-----------------------------------------------------------
-----------------------------------------------------------
-------------------------+
| task | CREATE TABLE `task` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(40) default NULL,
`description` varchar(100) default NULL,
`tasktype` int(10) default NULL,
`startdatetime` datetime default NULL,
`enddatetime` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------
-----------------------------------------------------------
-----------------------------------------------------------
-----------------------------------------------------------
-------------------------+
1 row in set (0.00 sec)





I have started using SQuirreL SQL client which is an universal client supporting many database systems like db2, oracle, mysql, sybase, and more..
Give it a try: http://squirrel-sql.sourceforge.net/

Wednesday, January 2, 2008

This is my first try but I am sure it will work .

First: My Intro,
I started working on J2EE mainly on Spring/Struts/Hibernate/JSP/Velocity/AJAX/Javascript and other things from last 2 years . Got lot of experience but never put all together.

My BS and MS is in computer engineering...Got many ideas while doing my projects but never get any push or motivation to work on it..or never anyone commented on those ideas yet.

I think it's enough and it's not much exciting intro. Well, I will keep improving..