Monday, 23 November 2015

MySQL import and export commands

MySQL is an open-source relational database management system (RDBMS). World's second most widely used open-source client–server RDBMS. The MySQL development project has made its source code available under the terms of the GNU General Public License and owned by Oracle Corporation.
MySQL import and export commands

Now lets see simple command line examples for exporting and importing MySQL database(s). All examples are shown under Windows machines. So when we try in different OS only path(s) will change rather than commands shown below.

Export Commands:

Open command prompt and goto MySQL installed bin folder as shown below location. NOTE: Installation directory path may change from system to system.

C:\Program Files\MySQL\MySQL Server 5.6\bin>

1. Export all databases

> mysqldump -u{username} -p{password} -h{host} --all-databases > C:\Backup\all-databases.sql

* username - MySQL username
* password - MySQL password
* host - MySQL server host. If MySQL running in local then please mention as 'localhost' else give remote MySQL server IP address. When we give remote server IP, need to make sure that we have access to remote MySQL server.

2. Export single database

> mysqldump -u{username} -p{password} -h{host} {database_name} > C:\Backup\database.sql

* database_name - MySQL database name to export to file.

3. Export more than 1 database

> mysqldump -u{username} -p{password} -h{host} --databases {database_name_1} {database_name_2} > C:\Backup\databases_1_2.sql

4. Export only specific tables from database

> mysqldump -u{username} -p{password} -h{host} {database_name} {table_name_1} {table_name_2} > C:\Backup\database_table_1_2.sql

mysqldump supports the various options, which can be specified on the command. For more informations on options please refer to MySQL site link under Table 4:11

Import Command:

Similar to mysqldump command please goto MySQL installed bin folder [C:\Program Files\MySQL\MySQL Server 5.6\bin>].

1. Import all databases

mysql -u{username} -p{password} -h{host} < C:\Backup\all-databases.sql

2. Import particular database

> mysql -u{username} -p{password} -h{host} {database_name} < C:\Backup\database.sql

* NOTE: Database with name mentioned must exist already, else create empty database with the name mentioned before importing.

3. Import tables into particular database

> mysql -u{username} -p{password} -h{host} {database_name} < C:\Backup\database_table_1_2.sql

* NOTE: Database with name mentioned must exist already, else create empty database with the name mentioned before importing.

read more.. "MySQL import and export commands"

Friday, 20 November 2015

Custom Annotation with Example

Annotations are only metadata and they do not contain any business logic. Prior to annotation introduced XML were used for metadata and somehow XML maintenance was getting troublesome. So something we need very loosely coupled from code and then annotations came into picture.
Custom Annotation with Example

Java by default provides built-in annotations like @Override, @Deprecated, @SuppressWarnings, @SafeVarargs and @FunctionalInterface.

  • @Override  - Used when overriding a method from super class. 
  • @Deprecated - Used to indicate a method is deprecated. When we use this annotation recommendedto provide information for why particular method deprecated and alternative solution to use it. 
  • @SuppressWarnings - Used to ignore specific warnings shown in code. 
  • @SafeVarargs - Suppress warnings for all callers of a method or constructor with a generics varargs parameter, since Java 7.
  • @FunctionalInterface - Specifies that the type declaration is intended to be a functional interface, since Java 8.

Now lets see simple example to write custom annotation which is as simple as writing interfaces. Just we need to prefix @ symbol to "interface" keyword. Some of the properties of annotation class must follow are

There a 5 types of meta annotations which gives information about the annotation which we create and they are @Documented, @Target, @Inherited, @Retention and @Repeatable

  • @Retention - Specifies how the marked annotation is stored—Whether in code only, compiled into the class, or available at run-time through reflection. Various Retention policies like
    1. RetentionPolicy.CLASS (Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior.)
    2. RetentionPolicy.RUNTIME (Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.)
    3. RetentionPolicy.SOURCE (Annotations are to be discarded by the compiler.)
  • @Documented - Marks another annotation for inclusion in the documentation.
  • @Target - Marks another annotation to restrict what kind of Java elements the annotation may be applied to. Various ElementTypes like 
    1. ElementType.TYPE (class, interface, enum)
    2. ElementType.TYPE_USE (Since 1.8)
    3. ElementType.TYPE_PARAMETER (Since 1.8)
    4. ElementType.FIELD (instance variable)
    5. ElementType.METHOD
    6. ElementType.PARAMETER
    7. ElementType.CONSTRUCTOR
    8. ElementType.LOCAL_VARIABLE
    9. ElementType.ANNOTATION_TYPE (on another annotation)
    10. ElementType.PACKAGE 
  • @Inherited - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited to subclasses).
  • @Repeatable - Specifies that the annotation can be applied more than once to the same declaration, since Java 8.

Simple annotation example:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public @interface MethodInfo{
    String createdBy() default "Anand";
    String createdDate();
    String comments();

public class AnnotationTest {

 @MethodInfo(createdBy = "Anand", createdDate = "20th Nov 2015", comments = "Just saying Hello :)")
 public void sayHello(String name){
  System.out.println("Hello "+name);
 public static void main(String[] args) {

  new AnnotationTest().sayHello("Java Discover");

read more.. "Custom Annotation with Example "