Wednesday, November 11, 2009

Why Prepared Statements are important and how to use them "properly"

http://www.theserverside.com/tt/articles/article.tss?l=Prepared-Statments
.....
How does a database execute a statement?
Obviously, don't expect alot of detail here; we'll only examine the aspects important to this article. When a database receives a statement, the database engine first parses the statement and looks for syntax errors. Once the statement is parsed, the database needs to figure out the most efficient way to execute the statement. This can be computationally quite expensive. The database checks what indexes, if any, can help, or whether it should do a full read of all rows in a table. Databases use statistics on the data to figure out what is the best way. Once the query plan is created then it can be executed by the database engine.

It takes CPU power to do the access plan generation. Ideally, if we send the same statement to the database twice, then we'd like the database to reuse the access plan for the first statement. This uses less CPU than if it regenerated the plan a second time.
....
When the J2EE server gives your application a connection, it isn't giving you the actual connection; you're getting a wrapper. You can verify this by looking at the name of the class for the connection you are given. It won't be a database JDBC connection, it'll be a class created by your application server. Normally, if you called close on a connection then the jdbc driver closes the connection. We want the connection to be returned to the pool when close is called by a J2EE application. We do this by making a proxy jdbc connection class that looks like a real connection. It has a reference to the actual connection. When we invoke any method on the connection then the proxy forwards the call to the real connection. But, when we call methods such as close instead of calling close on the real connection, it simply returns the connection to the connection pool and then marks the proxy connection as invalid so that if it is used again by the application we'll get an exception.

Wednesday, November 04, 2009

Import/Export keys preferences in Eclipse

Export preferences using : File -> Export -> Preferences ->"Choose Specific Preferences to Export" & choose "Keys Preferences"

You import the preferences file into your new workspace using File->Import -> Preferences.

See "Save your Shortcuts" section - http://robertmarkbramprogrammer.blogspot.com/2007/07/eclipse-shortcuts.html

Tuesday, November 03, 2009

Generate Java Beans from Database

http://wiki.netbeans.org/DevFaqAppClientOnNbPlatformTut

1) Create an Enterprise Application Project (with Application Client and EJB module) in Netbeans
2) Right click on ejb project in Project tab and select New -> Entity Classes From Database. In wizard chose as datasource jdbc/sample datasource and select the tables that need the entity beans.

Using the Spring Framework with the Teradata Plug-in for Eclipse

http://developer.teradata.com/tools/articles/using-the-spring-framework-with-the-teradata-plug-in-for-eclipse

This will be a series of articles explaining how the Spring framework can be used with the Teradata Plug-in for Eclipse to create a data access layer for your business services. The Java Bean Wrapper wizard allows the user to quickly generate a Java Bean class for a given SQL statement or stored procedure. Also the Wizard has an option to generate a Java Bean which
can be run with the Spring Data Access Object (DAO) framework. By calling a generated Java Bean from a DAO, the Bean will have access to the Spring transaction management. Also the Java Wrapper Beans are reusable components which could be used in different DAOs. The Bean Helper Classes can be used as Spring domain objects. Also the Bean will facilitate the support of OUT
parameters for stored procedures and multiple result sets inside of a Spring DAO.
This article will show how to setup a project that uses Spring DAOs with the Teradata Plug-in for Eclipse. This article will also show how to create Java Bean Wrapper classes that can be called from a Spring DAO using the Java Bean Wrapper Wizard.



http://developer.teradata.com/tools/articles/getting-started-with-teradata-plug-in-for-eclipse

Wednesday, October 28, 2009

Registry Problems - Double clicking a folder opens in new window

Why drive opens in new window?
It happens when a program or virus tries to edit the File Folder or Drive shell entries to add more context menu items and set them as default for double click action.

Read more: http://www.troublefixers.com/drive-opens-in-new-window-on-double-click-in-windows-xp/#ixzz0VIcluuP9

Lets see how we can fix this.


Fix:

First Method:
1. Open Start >> Run and type regsvr32 /i shell32.dll
2. press ok
3. You will see a message DllRegisterServer and DllInstall in shell32.dll succeeded
4. that’s it

Second Method:
1. Open Start >> Run and type regedit
2. Navigate to HKEY_CLASSES_ROOT/Directory/Shell
3. Double click the default key type none and press ok.
4. now navigate to HKEY_CLASSES_ROOT/Drive/Shell.
5. Double click the default key type none and press ok.

Third Method:
If you don’t want to play with the registry ,you can run the following command instead at
Run Prompt: ( Open Start >> Run )
“reg add hkcr\drive\shell /ve /d none /f” (without double quotes)
We hope at least one of the above method may work for you to fix the trouble, if not please let us know.

Just compiled a registry tweak, so if none of the above given methods works for you, then try downloading this registry file from here(http://troublefixers.com/wp-content/uploads/Miscellaneous/Open_in_Same_Window.reg) and merge it into your registry by double clicking the file.
After merging the registry file you may need to restart your system, so that the changes can reflect
Note: If still after following the above you are not able to solve the problem then update your antivirus and run a complete scan and make your system virus free. Click here to scan with


Read more: http://www.troublefixers.com/drive-opens-in-new-window-on-double-click-in-windows-xp/#ixzz0VIdEgVTR

Architecture Diagrams (Oracle vs Microsoft)

Oracle Server Architecture -





Microsoft Longhorn Architecture -

Wednesday, October 21, 2009

Intellij Open Sourced

Intellij is now open sourced - http://www.jetbrains.com/idea/nextversion/free_java_ide.html

Of course we need to understand the difference between the community edition & the regular edition - http://www.jetbrains.com/idea/nextversion/editions_comparison_matrix.html

Tuesday, October 06, 2009

Wicket & Eclipse (using Maven)

http://www.ralfebert.de/blog/wicket/wicket_eclipse_setup/

Also see - http://m2eclipse.sonatype.org/

Excellent Article on HashMaps (Oracle Website)

Excellent article on Hashmaps (and implementation)
http://www.oracle.com/technology/pub/articles/maps1.html



Hashing Internals: Hash Mapping Technique
Almost all the general purpose Maps use hash mapping. This is quite a simple mechanism for mapping elements into an array, and it is worth understanding how hash mapping works so that you can get the best out of your Maps.


Hash map structures consist of an internal array where elements are stored. Since the internal
storage is an array, clearly there must be a mechanism for determining an index into the array for an arbitrary key. In fact, the mechanism needs to give an integer index value which is smaller than the size of the array. This mechanism is called the hash function. In Java hashed based Maps, the hash function converts any object into an integer that fits into the internal array. You don't have to look hard to find an easily available hash function: every object has a
hashCode() method which returns an integer value. To map that value into any array, it is sufficient to convert it to a positive value and take the remainder after dividing by the array size. So here is a simple hash function for Java that works for any object


int hashvalue = Maths.abs(key.hashCode()) % table.length;
(The % binary operator, known as modulo, returns the remainder as an integer after dividing the left hand side by the right hand side.)
In fact, until the 1.4 release, this was exactly the hash function that was used by the various hash based Map classes. Though if you look in the code you will see


int hashvalue = (key.hashCode() & 0x7FFFFFFF) % table.length;
which is essentially the same function, using a faster mechanism to get a positive
value. From the 1.4 release, the HashMap class implementation uses a different
and more complex hash function, based on Doug Lea's util.concurrent packages
(I'll talk about Doug Lea's classes again in a little more detail later).




Figure 3: How hashing works
So that gives the basic groundwork for hash mapping, but we haven't quite taken care of everything. Our hash function maps an arbitrary object into an array location, but what happens if two different keys map to the same location? Nothing prevents that from occurring. In the parlance of hash mapping, this is called a collision. The way the maps deal with these collisions is to insert a linked list at the index location, and simply add elements to the linked list. So a basic put() method for a hash based Map could look like this public Object put(Object key, Object value) {
//Our internal array is an array of Entry objects
//Entry[] table;
//Get the hashcode, and map to an index
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) %
table.length;
//Loop through the linked list at table[index] to see if
//we already have this key entry — and if so overwrite it
for (Entry e = table[index] ; e != null ; e = e.next) {
//Must check that the key is equal, since the hash could be the same for different key objects


if ((e.hash == hash) && e.key.equals(key)) {
//This is the same key, overwrite the value and keep the old value to return from the method
Object old = e.value;
e.value = value;
return
old;
}
}
//Still here, so it's a new key, just add a new Entry An Entry object has Object "key" and value",


//an int "hash", and an Entry "next" to point to the next Entry in the list
//Create a new Entry pointing to the start of the previous
//list, and insert that new Entry into the table
Entry e = new Entry(hash, key, value, table[index]);
table[index] = e;
return null;
}
If you look at the source for various hash based Map's you'll see this is pretty much how they work. There are some further considerations, like handling null keys and values, and re-sizing the internal array. The put() method defined here also includes the algorithm for the corresponding get(), since the insertion includes searching the entries at the mapped index to see if the key is already present. (I.e. the get() method is the same algorithm as the put(), but without the insertion and overwite code.) Using a linked list is not the only way to handle collisions, and some of the hash maps use another "open addressing" scheme which I won't go into here.


Using Load Factors
In order to decide when to re-size, rather than keep a count of the depth of linked list in each bucket, the hash based Maps use an extra parameter and a rough calculation of how densely packed the buckets are. The Maps use a parameter called "load factor" which indicates how much "load" the Map will take, i.e. how full it will get, before it resizes.



The relationship between load factor, number of entries (map size), and the
capacity is straightforward:
When (load factor) x (capacity) > (map size), then the map will be re-sized
So, for example, if the default load factor is 0.75, and the default capacity is 11, then 11 x 0.75 = 8.25, which is rounded down to 8 elements. So when we add the eighth entry to this Map, the Map will re-size itself to a larger value. Conversely, to calculate what initial capacity you need in order to avoid re-sizing, divide the number of entries you will make by the load factor and round up, e.g.
For 100 entries with load factor of 0.75, then capacity should be set to 100/0.75 = 133.33, rounded up to 134 (or 135 to use an odd number)
Buckets sizes that are odd numbers should let the map perform more efficiently by reducing the number of collisions. Ideally prime numbers should be used for capacities, though tests I've made don't show prime numbers producing consistently better times (
Test4 in the associated files). Some Maps since the 1.4 release (e.g. HashMap and LinkedHashMap but not Hashtable or
IdentityHashMap) use a hash function that needs powers-of-two capacities, but the next highest power-of-two capacity is automatically calculated by those Maps so you don't need to try and calculate it yourself. The load factor itself is a tuning tradeoff between space and time. Smaller load factors will take more space but will reduce the likelihood of collisions, thus making access and updates faster. Load factors above 0.75 are probably unwise, and above 1.0 are definitely unwise since that guarantees at least one collision. Load factors below 0.50 will give you diminishing returns, but there should be no performance cost to small load factors as long as you size the map effectively, only a memory cost. But smaller load factors will imply more frequent resizing if you don't pre-size the Map, and that will incur a performance penalty, so do bear
that in mind if you are tuning the load factor.

Setting up Wicket in Netbeans 6.7

Wednesday, September 16, 2009

Migrating to Apache Wicket – GIDS presentation slides

http://ptrthomas.files.wordpress.com/2008/05/peter_thomas_migrating_to_apache_wicket.pdf

A very interesting presentation. I liked Thomas style of explanaing things with concrete examples and the comparison of other technologies with Wicket.

Wicket with Spring