javarevisited

javarevisited


What is Referential Integrity in Database or SQL - MySQL Example Tutorial

Posted: 27 Dec 2012 09:00 PM PST

Referential Integrity is set of constraints applied to foreign key which prevents entering a row in child table (where you have foreign key) for which you don't have any corresponding row in parent table i.e. entering NULL or invalid foreign keys. Referential Integrity prevents your table from having  incorrect or incomplete relationship e.g. If you have two tables Order and Customer where Customer is parent table with primary key customer_id and Order is child table with foreign key customer_id. Since as per business rules you can not have an Order without a Customer and this business rule can be implemented using referential integrity in SQL on relational database. Referential Integrity will cause failure on any INSERT or UPDATE SQL statement changing value of customer_id in child table, If value of customer_id is not present in Customer table. By the way What is Referential Integrity in SQL is also an important SQL question similar to finding second highest salary in SQL or difference between truncate and delete  and should be prepared well before going for any job interview, where knowledge of SQL is one of the requirement.
Read more »

How to create thread safe Singleton in Java - Java Singleton Example

Posted: 27 Dec 2012 07:18 PM PST

Thread safe Singleton means a Singleton class which returns exactly same instance even if exposed to multiple threads. Singleton in Java has been a classical design pattern like Factory method pattern or Decorator design pattern and has been used a lot even inside JDK like java.lang.Runtime is an example of Singleton class. Singleton pattern ensures that exactly one instance of class will remain in Java program at any time. In our last post 10 Interview questions on Singleton in Java we have discussed many different questions asked on Singleton pattern, One of them was writing Thread safe singleton in Java. Prior to Java 5 double checked locking mechanism is used to create thread-safe singleton in Java which breaks if one Thread doesn't see instance created by other thread at same time and eventually you will end up with more than one instance of Singleton class. From Java 5 onwards volatile variable guarantee can be used to write thread safe singleton by using double checked locking pattern. I personally  don't prefer that way as there are many other simpler alternatives to write thread-safe singleton is available available like using static field to initialize Singleton instance or using Enum as Singleton in Java. Let's see example of both ways to create thread-safe Singleton in Java.
Read more »

Oracle Pagination SQL query example for Java programmer

Posted: 27 Dec 2012 07:00 AM PST

Many time we need SQL query which return data page by page i.e. 30 or 40 records at a time, which can be specified as page size. In fact Database pagination is common requirement of Java web developers, especially dealing with largest data sets.  In this article we will see how to query Oracle 10g database for pagination or how to retrieve data using paging from Oracle. Many Java programmer also use display tag for paging in JSP which supports both internal and external paging. In case of internal paging all data is loaded in to memory in one shot and display tag handles pagination based upon page size but it only suitable for small data where you can afford those many objects in memory. If you have hundreds of row to display than its best to use external pagination by asking database to do pagination. In pagination, ordering is another important aspect which can not be missed. It's virtually impossible to sort large collection in Java using Comparator or Comparable because of limited memory available to Java program, sorting data in database using ORDER BY clause itself is good solution while doing paging in web application.
Read more »

How to convert milliseconds to Date in Java - tutorial example

Posted: 27 Dec 2012 05:52 AM PST

Do you want to convert milliseconds to Date in Java ? Actually java.util.Date is internally specified in milliseconds from epoch. So any date is number of millisecond passed since January 1, 1970, 00:00:00 GMT and Date provides constructor which can be used to create Date from milliseconds. Knowing the fact that Date is internally maintained in milliseconds allows you to store date in form of millisecond in Server or in your Class because that can be effectively expressed with a long value. In fact many experienced Java programmer store Date as long value while writing Immutable class which requires Date, one of the reason for that is Date being mutable and long value of Date can be very handy. By the ways this is next in Date related article, we have already discussed How to convert String to Date and How to get current month, year and day of week from Date in Java. If you haven't read them already, you may find them useful. In this Java tutorial we will see example of converting millisecond into Date in Java.
Read more »

3 Example to print array values in Java - toString and deepToString from Arrays

Posted: 27 Dec 2012 01:32 AM PST

Printing array values in Java or values of array element in Java would have been much easier if  arrays are allowed to directly prints its values whenever used inside System.out.println() or format and printf method, Similar to various classes in Java do this by overriding toString() method. Despite being an object, array in Java doesn't print any meaningful representation of its content when passed to System.out.println() or any other print methods. If you are using array in method argument or any other prominent place in code and actually interested in values of array then you don't have much choice than for loop until Java 1.4. Things has been changed since Java 5 because it introduced two extremely convenient methods for printing values of both primitive and object arrays in Java. Arrays.toString(array) and Arrays.deepToString(twoDimensionArray) can print values of any array. Main difference between Arrays.toString() and Arrays.deepToString  is that deepToString is used to print values of multidimensional array which is far more convenient than nesting of multiple for loops. In this Java tutorial we will see 3 different ways of printing array values in Java or value of element from Array in Java.
Read more »


Post a Comment