Saturday, 27 February 2016

How to Copy Non Empty Directory with Files in Java 7 - Example Tutorial

It's easy to copy a file or empty directory in Java as you can use Files.copy(fromPath, toPath) from Java 7, but, unfortunately, it's not as easy to copy a non-empty directory with all its files and subdirectories in Java, much like deleting a non-empty directory. There is no method in Java IO API which copies everything inside one directory to another. The copy(source, target, CopyOption...) method can copy directories, but files inside the directories are not copied. So the new directory will be empty even if the original directory contains files and folders. Similarly, the copy fails if target directory already exists, unless the REPLACE_EXISTING copy option is specified. No doubt that NIO 2 of Java 7 has made life easier for Java programmers and provides useful tools to deal with files and directories, the onus is now on Java developers to learn and make the best use of it. If you to learn more about NIO 2 features, see here.
Read more »

Friday, 26 February 2016

How to connect to Microsoft SQL Server database using Eclipse

Though I prefer SQL Server Management Studio to access and work with Microsoft SQL Server database, Sometimes, it's better to connect MSSQL database directly from Eclipse. This will save a lot of time which is wasted on switching between two applications, Eclipse and SSMS. It will also keep your PC fast enough because less application means less overhead. Eclipse IDE allows you to connect to almost all the database you have heard e.g. Oracle, MySQL, PostgreSQL, DB2 etc. All the steps are pretty much same, so once you know how to connect SQL Server database from Eclipse, you can connect Oracle or MySQL by yourself. Since Eclipse connects to the database using JDBC, you need to deploy JDBC drivers in your classpath. This is done part of New Database Connection Profile, one of the steps, which we will see later. In this article, I'll show you step by step guide to connect to Microsoft SQL Server database using Eclipse.
Read more »

Thursday, 25 February 2016

Data Access Object (DAO) design pattern in Java - Tutorial Example

Data Access Object or DAO design pattern is a popular design pattern to implement persistence layer of Java application. DAO pattern is based on abstraction and encapsulation design principles and shields rest of application from any change in the persistence layer e.g. change of database from Oracle to MySQL, change of persistence technology e.g. from File System to Database. For example, if you are authenticating the user using a relational database and later your company wants to use LDAP to perform authentication. If you are using DAO design pattern to access database, it would be relatively safe as you only need to make a change on Data Access Layer. DAO design pattern also keeps coupling low between different parts of an application. By using DAO design pattern your View Layer is completely independent of DAO layer and only Service layer has the dependency on it which is also abstracted by using DAO interface.
Read more »

Monday, 22 February 2016

3 Examples to Get the Id of an Element in jQuery

As a page author, the id of an HTML element is something you should know, but there are scenarios when the page is automatically generated or may be some form elements are generated by using frameworks like JSF. In that case, what do you do if you want to find id attribute of some elements in the page programmatically? How do you find those ids using jQuery? There are several ways to solve this problem, a couple of them we'll see in this article itself.  The jQuery API provides many methods to get any attribute or property including the id of an HTML element programmatically. In this article, I am going to share three easy ways to get the id of an element using jQuery.
Read more »

Saturday, 20 February 2016

2 ways to Split String with Dot (.) in Java using Regular Expression

You can use the split() method of java.lang.String class to split a String based on the dot. Unlike comma, colon, or whitespace, a dot is not a common delimiter to join String, and that's why beginner often struggles to split a String by dot. One more reason for this struggle is the dot being a special character in the regular expression. If you want to split String on the dot you need to escape dot as \\. instead of just passing "." to  the split() method. Alternatively, you can also use the regular expression [.] to split the String by a dot in Java. The dot is mostly used to get the file extension as shown in our example. The logic and method are exactly same as earlier examples of split string on space and split the string on a comma, the only difference is the regular expression. Once you know how to write the regular expression, all these examples will be same for you. Java's regular expression is inspired by Perl.
Read more »

Friday, 19 February 2016

How to add Columns to an Existing table in SQL Server

Adding a new column to an existing table with data is always tricky. You need to know what data is there, how much data is there, to gauge how long your query is gonna take to complete in production. Also, you cannot add NOT NULL columns into an existing table if they are not empty and you don't have a default value specified. If you know SQL then you probably know that you can add columns to an existing table in SQL Server using ALTER command. It not only allows you to add a column but to drop columns as well. You can also add or drop constraints using ALTER command. Btw, you need to be careful while doing anything with existing tables because of data inside, which presents some challenges while adding new columns or dropping existing ones.
Read more »

Wednesday, 17 February 2016

How does Enhanced for loop works in Java?

It's a long time since JDK 1.5 introduced the enhanced for loop, almost 12 years ago in the year 2004, but still many Java developers don't know basics of enhanced for loop, also known as for each loop in Java. The enhanced for loop provides the cleanest way to loop through an array or collection in Java, as you don't need to keep track of counter or you don't need to call the hasNext() method to check whether there are more elements left. The key point, which many Java developer doesn't know about the enhanced for loop is that you can use any Object which implements the Iterable interface on for (: ) construct, you can even use enhanced for loop to loop through the array. Since JDK 8 also introduced a forEach() function to provide looping functionality, don't confuse when I say for each loop with that. In this article, we'll see how enhanced for loop works and couple of important points related to it.
Read more »