Install MySQL on Ubuntu

Install MySQL (5.6) on Ubuntu (14.04)

Install MySQL

sudo apt-get install mysql-server-5.6

During the installation system will ask for root user password.

Check the service after installing mysql

sudo netstat -tap | grep mysql

mysql-service-working

If database installed cloud/network or not localhost then you need to change bind address to connect from your pc, to change bind address

nano /etc/mysql/my.cnf

bind-address            = 192.168.0.6

To get effect need restart service

sudo service mysql restart

Now use ssh or MySQL Workbench as client of mysql database.

mysql-connection

Yet, connection error from MySQL Workbench

Failed to Connect to MySQL at xxx.xx.xxx.xxx:3306 with user root

Host ‘xxx.xx.xxx.xxx’ is not allowed to connect to this MySQL server

mysql-connection-error

To resolve this we need to create user and grant privilege to user

CREATE USER 'user'@'XXX.XXX.XXX.XXX' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'XXX.XXX.XXX.XXX' WITH GRANT OPTION;
CREATE USER 'user'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Install Pig on Ubuntu

Install Pig (0.15.0) on Ubuntu (14.04)

To know how to install Hadoop on Ubuntu please visit Hadoop 2.7 installation on Ubuntu 14.04 (Single Node)

Download pig to server:

wget http://apache.mirrorcatalogs.com/pig/pig-0.15.0/pig-0.15.0.tar.gz

Unzip & installation pig:

    tar xvzf pig-0.15.0.tar.gz
    mv pig-0.15.0 pig
    ls
    sudo mv pig /usr/local/
    cd /usr/local/
    sudo chown -R hduser:hadoop pig
    export PATH=/usr/local/pig/bin:$PATH

Pig generates lot of log for itself. If we do not provide log4j properties file then it shows error message, to avoid this we need to add properties file. Pig provides few log4j properties file

hduser@myserver:/usr/local/pig/conf$ ls
log4j.properties.template  pig.properties  test-log4j.properties
hduser@myserver:/usr/local/pig/conf$ cp test-log4j.properties log4j.properties

Need to let pig know about log4j.properties file by updating pig.properties file

Open file by nano command and set log4jconf value

nano /usr/local/pig/conf/pig.properties
log4jconf=./conf/log4j.properties

All are set…..

hduser@myserver:/usr/local$ cd /usr/local/pig/conf/
hduser@myserver:/usr/local/pig/conf$ pig -version
Apache Pig version 0.15.0 (r1682971)
compiled Jun 01 2015, 11:44:35
hduser@myserver:/usr/local/pig/conf$

Improve performance of stored procedure called by java application

Improve performance of stored procedure called by java application

We have a stored procedure being called from our java web service.  It takes 1 sec from MS SQL management studio but takes 32 sec from the client of web service.

Java code:

long start = System.currentTimeMillis();
Query query = getSessionFactory().getCurrentSession().createSQLQuery("{call sp_GetNextNumber}");
String result = (String)query.uniqueResult();
logger.debug("sp_GetNextNumber took time:{}",(System.currentTimeMillis()-start));

Stored Procedure:

USE [database]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_GetNextNumber]     
AS

SET NOCOUNT ON
.
.
.

Few setting which will improve performance of stored procedure

set implicit_transactions off
set transaction isolation level READ COMMITTED

So new stored procedure will be

USE [database]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_GetNextNumber]     
AS

SET NOCOUNT ON
set implicit_transactions off
set transaction isolation level READ COMMITTED
.
.
.

Hadoop 2.7 installation on Ubuntu 14.04 (Single Node)

Hadoop installation on Ubuntu

Installation Ubuntu on virtual machine

Here I am showing hadoop installation Ubuntu using VM player. Here is the vm setting of ubuntu

vm player settings

VM memory should be 25% – 35% of hosting machine.

Prerequisites

Before starting installation lets update all applications of software.

sudo apt-get update

Installing Java

if java is not installed, then use following command for installing java

sudo apt-get install openjdk-7-jdk

Or use ubuntu software center for installing jdk 6 or 7. Now check is it installed correctly

java-version

Adding a dedicated Hadoop system user

sudo addgroup hadoop
sudo adduser --ingroup hadoop hduser

Installing SSH

sudo apt-get install ssh

Generate an SSH key for the hduser user

ssh-keygen -t rsa -P ""

Here P “” indicates an empty password. Now needs to enable SSH access to local machine with this newly created key.

cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys

Test ssh

ssh-test

Disabling IPv6

We need to disable IPv6 because Ubuntu is using 0.0.0.0 IP for different Hadoop configurations. To do that open /etc/sysctl.conf file by gedit or nano and add following 3 lines at the end

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

Need to reboot system to get this effect.

Install Hadoop

wget http://apache.arvixe.com/hadoop/common/hadoop-2.7.1/hadoop-2.7.1.tar.gz
tar xvzf hadoop-2.7.1.tar.gz
mv hadoop-2.7.1 hadoop
sudo mv hadoop /usr/local/
sudo chown -R hduser:hadoop hadoop

The following files are needed to update single node Hadoop cluster

  1. core-site.xml
  2. mapred-site.xml
  3. hdfs-site.xml
  4. Update $HOME/.bashrc
  5. hadoop-env.sh

1.core-site.xml

The /usr/local/hadoop/etc/hadoop/core-site.xml file contains configuration properties which is used by Hadoop. Need to add following property to this file

<configuration>
 <property>
  <name>hadoop.tmp.dir</name>
  <value>/usr/local/hadoop/tmp</value>
  <description>temporary directories.</description>
 </property>

 <property>
  <name>fs.default.name</name>
  <value>hdfs://localhost:54310</value>
  <description>The name of the default file system.</description>
 </property>
</configuration>

Now lets create the directory and set the required ownership and permissions

sudo mkdir -p /usr/local/hadoop/tmp
sudo chown hduser:hadoop /usr/local/hadoop/tmp

2. mapred-site.xml

By default, the /usr/local/hadoop/etc/hadoop/ folder contains
mapred-site.xml.template and need to rename as mapred-site.xml and enter following content

<configuration>
 <property>
  <name>mapred.job.tracker</name>
  <value>localhost:54311</value>
  <description>The host and port that the MapReduce job tracker runs
  at.  If "local", then jobs are run in-process as a single map
  and reduce task.
  </description>
 </property>
</configuration>

3. hdfs-site.xml

Here we will specify namenode and datanode directories, to do that we need to create folders

sudo mkdir -p /usr/local/hdfs/namenode
sudo mkdir -p /usr/local/hdfs/datanode
sudo chown -R hduser:hadoop /usr/local/hdfs

to add properties we can use vi or nano,

nano /usr/local/hadoop/etc/hadoop/hdfs-site.xml
<configuration>
 <property>
  <name>dfs.replication</name>
  <value>1</value>
 </property>
 <property>
   <name>dfs.namenode.name.dir</name>
   <value>file:/usr/local/hdfs/namenode</value>
 </property>
 <property>
   <name>dfs.datanode.data.dir</name>
   <value>file:/usr/local/hdfs/datanode</value>
 </property>
</configuration>

4. Update $HOME/.bashrc

nano-bash

add following values at the end of file

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386
export HADOOP_INSTALL=/usr/local/hadoop
export PATH=$PATH:$HADOOP_INSTALL/bin
export PATH=$PATH:$HADOOP_INSTALL/sbin
export HADOOP_MAPRED_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_HOME=$HADOOP_INSTALL
export HADOOP_HDFS_HOME=$HADOOP_INSTALL
export YARN_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_INSTALL/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_INSTALL/lib"

5.hadoop-env.sh

Set JAVA_HOME by modifying /usr/local/hadoop/etc/hadoop/hadoop-env.sh file.

# The java implementation to use.
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386

Formatting the HDFS filesystem

Switch to hduser

swithc-user

Format command:

hadoop namenode -format

Start Hadoop

start-hadoop

Hadoop Web Interfaces

http://localhost:50070/

web

Stop Hadoop

stop-all.sh

Enjoy!!! big data!!!

Hibernate cascade inverse example (none, save-update, delete and delete-orphan)

Hibernate cascade example with inverse

We use parent child relation in our most of application. To solve this we use hibernate cascade in application. It is very important to understand how hibernate cascade and inverse works. In this example I use One to Many relation where a parent table as Parent and child table as Child. We need to understand why and when 3 queries will be fired by hibernate for all cases

Query 1: it is for parent itself

Query 2: need to update,insert/update/delete, child based on cascade value. It will fire all values of cascade except none. It also fire if manually we try to save like session.save(child).

Query 3: need to update child only if relation owner is parent that means inverse = false and when a new parent is inserting or deleting a parent or a new child is adding to parent. It wont fire when inverse = true.

I am trying to show all possible examples on hibernate cascade that will help you to know hibernate cascade, inverse and difference between them.

cascade

This keyword use in relation and let hibernate know what should do for child. Based on this value hibernate fires query to update child and intermediate table. This keyword help you reduce additional work for relation. For example if you need to do anything for child just use this keyword hibernate will take care of everything.

inverse

In fact inverse =  owner of relation.  when inverse = true then relation owner = child otherwise parent. By default inverse = false that means parent is relation owner.

Example of hibernate cascade with inverse


When value of hibernate cascade =  “none” : Hibernate ignores the association.

                //main method
                Parent parent = new Parent();
		parent.setName("Parent 1");
		Child child = new Child();
		child.setName("Child 1");
		child.setParent(parent);        
		parent.addChild(child);
		session.save(parent);
<set name="children" cascade="none" table="child" inverse="false">
 <key>
   <column name="parentid" not-null="true" />
 </key>
 <one-to-many class="com.javaforloop.hibernate.cascade.example.dvo.Child" />
</set>

Cascade = none inverse= false; relation owner = Parent

Q1:Hibernate: insert into parent (NAME) values (?)
Q3:Hibernate: update child set id=? Where id=?
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.javaforloop.hibernate.cascade.example.dvo.Child

Here cascade = none and inverse = true so parent is relation owner. Q1 is fired for parent itself. Since cascade=none and child is not manually saved so no query (Q2 query) will be fired for child. Since parent is owner of relation it needs to update (Q3 query) child record. Since child is not save manually and child object is in hibernate memory, transient, but it is not going to database as a result hibernate throw unsaved instance

Cascade = none inverse = true; relation owner = Child

Q1 : Hibernate: insert into parent (NAME) values (?)

Here cascade = none and inverse = true so Child is relation owner and Parent does not need to update Child (Q3). Since cascade=none and child is not manually saved so no query (Q2) will be fired for child.

                //main method
		session.save(parent);
		session.save(child);

Cascade = none inverse = false; relation owner=Parent

Q1: Hibernate: insert into parent (NAME) values (?)
Q2: Hibernate: insert into Child (NAME, parentid) values (?, ?)
Q3: Hibernate: update Child set parentid=? where id=?

Q1: for parent itself. Q2: cascade = none but manually session.save() exists. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = none inverse = true; relation owner = Child

Q1:Hibernate: insert into parent (NAME) values (?)
Q2:Hibernate: insert into Child (NAME, parentid) values (?, ?)

Q1: for parent itself. Q2: cascade = none but manually session.save() exists. Q3:wont fire because inverse = true; relation owner = Child. Child does not need to update Parent.


when value of hibernate cascade =  “save-update” : Let hibernate to save a new instance or changes persistence object of parent and also association object(s).

Cascade = save-update inverse = false; relation owner = Parent

Q1:Hibernate: insert into parent (NAME) values (?)
Q2:Hibernate: insert into Child (NAME, parentid) values (?, ?)
Q3:Hibernate: update Child set parentid=? where id=?

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: fired because inverse = false; relation owner = Parent. Parent needs to update Parent.

Cascade = save-update inverse = true; relation owner =Child

Q1:Hibernate: insert into parent (NAME) values (?)
Q2:Hibernate: insert into Child (NAME, parentid) values (?, ?)

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: wont fire because inverse = ture;

Cascade = save-update inverse = true; relation owner =Child

Q1:Hibernate: insert into parent (NAME) values (?)
Q2:Hibernate: insert into Child (NAME, parentid) values (?, ?)

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: wont fire because inverse = true;

                //main method
Parent parent = (Parent) session.get(Parent.class, 14);
parent.setName("Name is updated");
// in this example parent has only one child
for(Child c : parent.getChildren()){
   c.setName("Name is updated");
   session.update(c);
}
session.update(parent);

Cascade = save-update inverse = false; relation owner=Parent

Q1: Hibernate: update parent set NAME=? where id=?
Q2+Q3:Hibernate: update Child set NAME=?, parentid=? where id=?

Q1: for parent itself. Q2: cascade = save-update and manually session.update() exists. Q3: wont fire when parent is updating.

                //main method
Parent parent = (Parent) session.get(Parent.class, 14);
Child c = new Child();
c.setName("new C1");
c.setParent(parent);
parent.addChild(c);
session.update(parent);

Cascade = save-update inverse = false; relation owner=Parent

Q2:Hibernate: insert into Child (NAME, parentid) values (?, ?)
Q3:Hibernate: update Child set parentid=? where id=?

Q1: wont fire because nothing change for parent.Q2: cascade = save-update. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = save-update inverse = true; relation owner=Child

Q2:Hibernate: insert into Child (NAME, parentid) values (?, ?)

Q1: wont fire because nothing change for parent.Q2: cascade = save-update. Q3: inverse = true; relation owner = Child.


when value of hibernate cascade= “delete-orphan”: Let hibernate deletes persistent object that has been removed from the association.

                //main method
Parent parent = (Parent) session.get(Parent.class, 14);
session.delete(parent);

Cascade = delete-orphan inverse = false; relation owner=Parent

Q3:Hibernate: update Child set parentid=null where parentid=?
Q2:Hibernate: delete from Child where id=?
Q1:Hibernate: delete from parent where id=?

Q1: for parent itself. Q2: cascade = delete-orphan. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = delete-orphan inverse = true; relation owner=Child

Q2:Hibernate: delete from Child where id=?
Q1:Hibernate: delete from parent where id=?

Q1: for parent itself. Q2: cascade = delete-orphan. Q3: wont fire because inverse = true.

                //main method
Parent parent = (Parent) session.get(Parent.class,22);
Iterator<Child> it = parent.getChildren().iterator();
int i =0;
while(it.hasNext()){
  Child c = it.next();
  //delete first one
  if(i++ == 0){
    session.delete(c);
  }
}

Cascade = delete-orphan inverse = false; relation owner=Parent

Q2:Hibernate: delete from Child where id=?

Q1: nothing update for parent. Q2: cascade = delete-orphan or manually delete. Q3: no insert or delete for parent.

Cascade = delete-orphan inverse = true; relation owner=Child

Q2:Hibernate: delete from Child where id=?

Q1: nothing update for parent. Q2: cascade = delete-orphan or manually delete. Q3: wont fire because there is no insert or delete for parent.

Download of Hibernate Cascade project

To get complete project of Hibernate cascade please click here.

Jdbc Connection

Jdbc Connection Using Oracle, SQL Server, My Sql

A jdbc connection is the object where commands are sent to the database and responses are returned to the program. Jdbc connection is that enables Java programs to execute SQL statements. The JDBC API consists of a set of interfaces and classes written in the Java programming language.

All databases provide their driver that enable users to make JDBC connections to databases. The two most common methods of connecting to Oracle databases  are Oracle Thin JDBC driver and the Oracle OCI JDBC driver.

For mssql server there are two ways we can make jdbc connection, using windows authentication or SQL authentication. For this there are two commonly used drivers JDBC and JTDS.

Steps of making Jdbc Connection

  • Load the JDBC driver
  • Define the connection url
  • Establish the connection using connection parameters

Examples of JDBC driver

Oracle :

Class.forName("oracle.jdbc.driver.OracleDriver");
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.3.0</version>
</dependency>

jar file:  ojdbc14-10.2.0.3.0.jar

MSSQL :

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
<dependency>
   <groupId>com.microsoft.sqlserver</groupId>
   <artifactId>sqljdbc4</artifactId>
   <version>4.0</version>
</dependency>

jar files : sqljdbc.jar   sqljdbc4-4.0.jar 

My Sql:

Class.forName("com.mysql.jdbc.Driver");
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.6</version>
</dependency>

jar file – mysql-connector-java-5.1.6.jar

Examples of Connection Url

Oracle :

using a service name
jdbc:oracle:thin:@//<host>:<port>/<service_name>
example: String url = "jdbc:oracle:thin:@//192.168.10.10:1521/ORDER";
using a tnsname
jdbc:oracle:thin:@<tnsname>
example: String url = "jdbc:oracle:thin:@sales";
To test tnsname, use tnsping, example: TNSPING sales

MSSQL :

using windows authentication 
String url ="jdbc:sqlserver://PC01\inst01;databaseName=DB01;integratedSecurity=true";
using SQL authentication 
String url ="jdbc:sqlserver://PC01\inst01;databaseName=DB01";

My SQL :

jdbc:mysql://[server]:3306/[database]
example: String url = "jdbc:mysql://localhost:3306/db01";

Examples of Jdbc Connection parameters

Oracle/MSSQL/My Sql :

Connection conn = DriverManager.getConnection(url, userName, password);

jdbc connection – common errors

MSSQL:

  • Error: “java.net.SocketTimeoutException: Receive timed out”. Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.
  • com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host  has failed. java.net.ConnectException: Connection refused: connect

Cause : 1. protocol (TCP/IP) is not enabled  2.Port is incorrect  3. missing of sqljdbc_auth.dll

Solutions:

1. protocol (TCP/IP) is not enabled :

a) Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager.  > Protocols for inst01, instance name, under SQL Server Network Configuration. Right-click on TCP/IP and choose Properties. Set Enabled = YES.  [this should be done by admin user]

Jdbc Connection - SQL Server Configuration Manager

Jdbc Connection – SQL Server Configuration Manager

2. Port is incorrect : Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager  > SQL Server Network Configuration >Protocols for inst01, instance name,  > Properties of TCP/IP  > IP Address tab and set TCP Port: 1433 , under IP All [this should be done by admin user]

3. missing of sqljdbc_auth.dll –

a) Download latest MSSQL JDBC driver from here: link1 , link2

b) take sqljdbc_auth.dll from x86/x64 folder.

c) copy the sqljdbc_auth.dll to your JDK directory bin and windowssystem32 folder. (you do not need restart your machine)

Download

To get a complete project of jdbc connection with MSSQL, click here.

Java classes of a project by reflection

Java classes by reflection

Today I am gonna show you all java classes of a project. Some times we get a requirement that we have to show all java classes that are using in a project. These classes those are written by developer of project. Basically this post is showing a recursion java example to get all java classes of a project. Using javadoc we can see in html page but this post java classes will give you a spread sheet file which contains of all java classes. In this example there is a interface, called service, it has method that’s input/output class contains different class. In these input/output class contains some domain class.  Here I am using Member, PersonalDetails, Address etc class as domain class. Here is one example of Member class

package com.javaforloop.visitallclass.domain;

public class Member {
	private String id;
	private MemberStatus status;
	private PersonalDetails personalDetails;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public PersonalDetails getPersonalDetails() {
		return personalDetails;
	}

	public void setPersonalDetails(PersonalDetails personalDetails) {
		this.personalDetails = personalDetails;
	}

	public MemberStatus getStatus() {
		return status;
	}

	public void setStatus(MemberStatus status) {
		this.status = status;
	}
}

Service Interface

package com.javaforloop.visitallclass.service;

import com.javaforloop.visitallclass.input.CreateMemberInput;
import com.javaforloop.visitallclass.output.CreateMemberOutput;

public interface Service {
	public CreateMemberOutput createMember(CreateMemberInput request);
}

CreateMemberInput class

package com.javaforloop.visitallclass.input;

import com.javaforloop.visitallclass.domain.Member;
public class CreateMemberInput {
	Member [] members;
	public Member[] getMembers() {
		return members;
	}

	public void setMembers(Member[] members) {
		this.members = members;
	}
}

Our target output like

all java classes of output

all java classes of domain

To get all java classes

Step of to get all java classes:

Step 1: getAllServiceNames(): read service interface

private Set<String> getAllServiceNames(String serviceClassName) {
		Set<String> classes = Sets.newHashSet();
		Map<String, List<String>> inputFields = new HashMap<String, List<String>>();
		Map<String, List<String>> outputFields = new HashMap<String, List<String>>();
		List<String[]> result = getServiceDetails(serviceClassName, "METHOD");
		// if you have any filter list, in this example we don't have any
		List<String> filterList = Lists.newArrayList("createMember");
		// print(result);
		for (String[] strings : result) {
			String service = strings[0];
			if (!filterList.contains(service)) {
				continue;
			}
			List<String> inputList = getAllFields(strings[1], false);
			inputFields.put(strings[0], inputList);
			// printList(inputList);
			List<String> inputDomainClassess = getAllFields(strings[1], true);
			classes.addAll(inputDomainClassess);

			List<String> outputList = getAllFields(strings[2], false);
			outputFields.put(strings[0], outputList);
			// printList(outputList);
			List<String> outputDomainClassess = getAllFields(strings[2], true);
			classes.addAll(outputDomainClassess);
		}
		String fileName = "./services.csv";
		writeFile(fileName, getFileContent(inputFields, outputFields));
		return classes;
	}

Step 2: getServiceDetails(): Make a list of service input and output

private List<String[]> getServiceDetails(String className, String type) {
		List<String[]> result = new ArrayList<String[]>();
		try {
			Class<?> c = Class.forName(className);
			// System.out.println(className);
			switch (ClassMember.valueOf(type)) {
			case METHOD:
				result = getMemberDetails(c.getMethods());
				break;
			case CLASS:
				// TODO
				break;
			case ALL:
				// TODO
				break;
			}
		} catch (ClassNotFoundException cnfx) {
			cnfx.printStackTrace();
		}
		return result;
	}

Step 3: getAllFields(): read a class and return a list of attributes. Please see ReflectionHelper class. You will see download link at bottom.
Step 4: writeSpredSheet : write all results to spread sheet file. Please see ReflectionHelper class.

Now lets see visitAllClass() method of ReflectionHelper class, this method is visiting recursively to all child java classes according to all parent class. Here is this method

	/**
	 * @param className
	 * @param domainOnly
	 * @return
	 */
	public List<String> visitAllClass(String className, boolean domainOnly) {
		// System.out.println("class name :"+className);
		List<String> list = new ArrayList<String>();
		list.add(className);
		try {
			Class<?> c = Class.forName(className);
			Field fields[] = null;
			try {
				fields = c.getDeclaredFields();
			} catch (Exception ex) {
				System.out.println(className);
				ex.printStackTrace();
			}
			for (Field field : fields) {
				String targetValue = "";
				targetValue = field.getType().getSimpleName() + " "
						+ field.getName();
				if (domainOnly) {
					if (!field.getType().getName().contains("javaforloop")) {
						continue;
					} else {
						targetValue = field.getType().getName();
						if (field.getType().isArray()) {
							targetValue = targetValue.substring(2,
									targetValue.length() - 1);
						}
						// System.out.println(targetValue);
						if (!targetValue.trim().equals(className)) {
							list.addAll(visitAllClass(targetValue, true));
						}
					}
				}
				list.add(targetValue);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return list;
	}

In top method, I am using java reflection at line 11 and getting class details. In deed, this we are passing parent class name as a parameter of this method. At line 14, we are taking all attributes of this class. At 19, we are checking all fields so that we can take a decision is it java.lang object or our domain object. If it is our domain object then it calls itself see line 34.

If any class has same class reference then you will get java.lang.reflect.InvocationTargetException , to rid off from this error you should check, see line 33, the same class name should not pass as argument of visitAllClass method.

I am using google guava collection framework though you can remove this jar from project. It is a maven project you can remove from decency from pom file. To know maven please visit here. Another post of this blog is describing about maven.

Download

Click here to download this project.

Jasper Reports Example

Jasper Reports example – Create Pie chart using iReport

In this blog I will show a jasper reports example using iRport designer. In this post I will show how to create a pie chart.  This is a very simple jasper reports example. I have one table called category and it has different categoryno with category name. Each category has subcategory. I want to show how many categories are there by pie chart.  Category table structure link will give you the table structure and some sample data. I used MySql database for this report. It supports couple of databases.

Jasper Reports example

The JasperReports api is the most popular open source reporting engine. It can be used in java enabled applications, including J2EE or web application to producing dynamic content. It is a Java based project. It reads its instructions from an XML or .jasper file. The tool iReport is use to create report template, design of report,  setting value of parameter and query etc. In this Jasper reports example  we will feed data from any kind of data sources.  It produce different kind of report like HTML, PDF, EXCEL, XML etc. To integrate a j2ee application, you need to add jar file to class path.  JasperReports reports are defined in an XML file format. This XML file is called JRXML file. So its file extension is .jrxml.  This file is readable, any one can easily modify it. iReport designer tool generates this jrxml file. We can create sub-report using iReport tool. In master details report we need sub-report. iReport is a graphical representation of jrxml. It is very useful tool for designing the entire report. After compiling a jrxml file iReport produces a file is called jasper file. Its extension is .jasper. A jasper file is used at runtime to produce reports.

If we need format any data we can add additional java code to report using iRerport. It supports all java codes. It is another beauty of iReport. For example currency, date format we can use it. Even column can show/hide based on value. That means we can add if / else logic. iReport designer runs reports using all data sources and also apply all internal java logics.

I have showed other jasper reports example in my other post.

Jasper Reports example – Steps for iReport designer

To get more details on Jasper reports example and documents click here and iReport is available at here.

  • Create New document from iReport. use this band value title = 30, columnHeader = 15, detail = 15 and summary = 300 and others = 0.
  • Add Query:     select category “Cat No”, subcat_name “category Name”, count(category) total from category
    group by category order by 1
  • Add 3 fields – Cat No, Category Name, Total to Detail band.    Jasper Reports Example - detail-band
  • Click the char button and select the Pie 3D. I put its width is 268 and height is 203.   Jasper Reports Example - ui2
  • right click on chart and select cart properties. select cart data tab, select the Details tab. and put the value as follows –  for Key Expression = $F{category Name} ,  Value Expression = $F{total},  Label Expression = $F{category Name}
  • click close.
  • b4 execute you must select your database connection. now click Execute with data connection. the output file will looks like as follows

jasper reports example: output-pie-report

Javascript validate number

Javascript validate number

this example allow only number character in you text field. This javascript validate number code can be tested at here .

—- JavaScript validate number code

function numeralsOnly(thisComp, e, isDecimal) {
	var key;
	var keychar;
	if (window.event) {
		key = window.event.keyCode;
	} else if (e) {
		key = e.which;
	} else {
		return true;
	}

	// check double period/decimal/point
	if (((thisComp.value).indexOf('.') > -1) && key == 46) {
		alert("Double period is not allow for this field.");
		return false;
	}

	var str = '0123456789';
	str += isDecimal ? '.' : '';
	keychar = String.fromCharCode(key);
	if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13)
			|| (key == 27)) {
		return true;
	} else if (((str).indexOf(keychar) > -1)) {
		return true;
	}

	alert("Enter numerals only in this field.");
	thisComp.focus();
	// thisComp.select();
	return false;
}

——- html code

<input type="text" id="txtSkuPrice"  onkeypress="return numeralsOnly(this,event,true);"/>

numeralsOnly(this,event,true) -> for Double number use true and for integer number use false.

 

javascript convert string to number

Javascript convert string to number

Some time we need to get number value from currency format string . This Javascript convert string to number post will show how we get the exact number value. This javascript convert string to number code can be tested at here .

Lets consider we have a string its value like $1,221.22. So the actual number is 1221.22 after eliminate the Dollar sign and comma. The javascript code is –

function getNumberFromCurrencyString(currencyString){
var result = currencyString.replace('$',''); //remove dollar
result = result.replace(/,/g,''); //remove comma
return result;
}