Posts

Showing posts from 2013

SuggestBox in GWT - The correct way to create and populate.

Hi everyone, its been a while. Here, in this post, I will be discussing about creating, initializing and populating a suggestbox in gwt. Suggestbox is a text box or text area which displays a pre-configured set of selections that match users input. Suggestbox is similar to an autocomplete search box. Each Suggestbox is associated with a single SuggestOracle. The SuggestOracle is used to provide a set of selections given a specific query string. SuggestBox uses a MultiWordSuggestOracle as its oracle. Following are the steps to create and populate a SuggestBox and declaration of MultiSuggestOracle. 1) First step is to declare MultiSuggestOracle. private   final  MultiWordSuggestOracle oracle =  new  MultiWordSuggestOracle(); 2) Create SuggestBox @UiField  (provided =  true )  SuggestBox sbox =  new  SuggestBox(oracle); The important thing to remember is to add (provided = true). 3) Populating the SuggestBox private   void  populateSuggestBox()      { List

Dead Code

Hi Everyone, in this post I will be describing about dead code. I have heard about dead code only recently and thought of putting it in blog. 'Dead code' as it name suggests is a piece of code which is dead or has got no chance of it being executed. In other words dead code is a type of code which is unreachable. I will provide an example which will helps in understanding more about dead code. Consider a piece of code in Java. String fullName = employee.getFirstName() +  " "  + employee.getLastName(); if (fullName!= null ) { fullName =  "Sujith" ; } In the above example the code in the 'if' loop would never be executed. The reason is string 'fullName' cannot be empty because of the space (" ") we are adding between employee.getFirstName() and employee.getLastName().  It doesn't matter even if both employee.getFirstName() and employee.getLastName() be null, "fullName" would not be null. Some IDE's li

Maven project which adds another project as a dependency.

Image
Hi Everyone, in this post, I will be discussing about how to add another project as a dependency in maven. Maven is a build automation tool primarily used for java projects. Currently, maven is the most common build tool used in the software market. Please check the wiki page for maven to know more topics. I will be demonstrating this example by creating 2 projects named 'MavenProject' and 'MavenProject2'. The first project 'MavenProject' is added as a dependency to the second project 'MavenProject2'. To add a project as a dependency in Maven, the project is added as a dependency to pom.xml of the project which refers the latter project. pom.xml is the xml representation of maven project. It stands for project object model. Click here to read more about pom.xml. The structure of the first project is as follows: The class file MavenProject.java is as follows: package  com.techjourney.maven; public   class  MavenProjec

Java program to get the details about memory

Hi everyone, in this post, I am writing about how to get the details of maximum memory, free memory, total memory and used memory in java. maxMemory() - Specifies the maximum amount of memory java virtual machine will attempt to use. maxMemory is returned in bytes totalMemory() - Specifies the total amount of memory in java virtual machine. The totalMemory will vary according to the host environment. totalMemory is returned in bytes. freeMemory() - Specifies the amount of free memory in jvm. freeMemory is returned in bytes. The information about all these memory types are provided by Runtime class of java. The memory used by jvm can be obtained from different methods of the Runtime class. Following java program specifies a clear idea about getting details about  3 types of memories. package  com.techjourney; /**  * Created with IntelliJ IDEA.  * User: Sujith  * Date: 6/19/13  * Time: 11:17 PM  * To change this template use File | Settings | File Templates.  */ pub

instanceof operator in java

Hi everyone, in this blog post, I will be describing about instanceof operator in java. Instanceof operator is mainly used to specify whether an object is an instance of a class. It allows to determine the type of the object. It is a boolean operator which returns true if the object is an instance of the specified class.  Instanceof operator is a useful tool when there is a collection of objects and we are not sure why they are used. Here is a sample program which will give a better idea about the instanceof operator. package  com.example; import  java.util.ArrayList; import  java.util.HashMap; import  java.util.LinkedHashSet; import  java.util.List; import  java.util.Map; import  java.util.Set; public   class  InstanceOfExample {      public   static   void  main(String[] args) {         List myList =  new  ArrayList<>();         Map myMap =  new  HashMap<>();         Set mySet =  new  LinkedHashSet<>();                  System.out.println( &qu

Sample jdbc program using oracle database

Image
Hi everyone, in this post, I will be writing about a sample jdbc program using oracle database. I hope everyone have gone through my earlier post, sample program using HSQL DB . Connection of oracle database is almost similar to connection with hsql db, except the name of connection url and class for connection. Connection URL = "jdbc:oracle:thin:@localhost:1521" Class.forName("oracle.jdbc.driver.OracleDriver") Here, I am writing a jdbc program to fetch the values from a table. The sample jdbc program for oracle database is as follows : import  java.sql.Connection; import  java.sql.DriverManager; import  java.sql.ResultSet; import  java.sql.SQLException; import  java.sql.Statement; public   class  OracleDatabase {      public   static   void  main(String[] args) {                  System.out.println( " Oracle Database " );         System.out.println();          try         {             String connectionURL = "jdbc:oracle:th

Deploying application of Google Web Toolkit on Tomcat Server

Image
Hi Everyone, in this blog post, I will describe about how to display the application for Google Web Toolkit in Apache Tomcat. Apache Tomcat is an open source web server and servlet container developed by the apache software foundation. Please check the reference for knowing more about tomcat server. This blog is based on my previous blog post - Sample Application in GWT.  Please go through my previous blog post to get a better idea. In this post, I will be deploying the application, which I wrote in the previous blog in tomcat. Deployment is the term used for installing an application in your server. In tomcat, the web application itself is setup in 2 different ways, which are : Statically - The web application is setup in tomcat before it is started. Dynamically - By directly manipulating already deployed applications or remotely by using tomcat web application manager. Following are the steps to be followed to deploy the application in tomcat. For deploying the applica

Sample program for GWT

Image
Hi everyone, in this blog post, I will be writing a sample program on GWT. Please go through my previous post, Introduction to GWT , which will give you a basic idea about GWT. Here, I am creating a sample gwt application which displays an alert button. The name of the project is SampleGWT. To create a project, click on the google icon in eclipse and choose "New Web Application Project". The main folders in a gwt application are as follows :- 1) src - src include the source folder, where the source code is written. In GWT src includes, a) Client folder - Contains java classes for User Interface. The entry point of the application will be usually in client folder. b) Server folder - Contains java classes for server side processing. Usually server folder includes servlets. c) Shared folder - Contains java classes for transferring data from client to server. d) Module descriptor file - This file will be named as our project_name.gwt.xml. This file is required f

GWT for beginners - Introduction.

Image
Hi Everyone, from this post onwards I am planning to write about GWT (Google Web Toolkit) as it is currently used for many web development projects. GWT is an open source set of tools that allow web developers to create and maintain complex JavaScript front end applications in Java. Following are some of the specifications and features of GWT :- GWT is used to create RICH Internet Application (RIA). GWT provides option to write client side application in JAVA. GWT automatically generates javascript code suitable for each browser. GWT provides full debugging capability. Developers can debug the client side application just as an Java Application. GWT is licensed under apache license version 2.0. GWT provides easy integration with JUnit and Maven. GWT provides widget libraries. GWT has simple RPC mechanism. GWT has support for Internationalization and localization. HTML canvas support. GWT has support for using Google APIs. A number of libraries are available for GWT, by

Some more annotations in hibernate

Hi Everyone, in this blog post I am writing more about annotations used in hibernate. I hope everyone has seen my previous post about hibernate annotations - Basic Hibernate Annotations. 1) @Basic - It is the simplest type of mapping to a database column.If basic annotation is not specified for a field or property, the default values of basic annotation will apply. 2) @Transient - Specifies that the field or the property cannot be persisted. 3) @Temporal - Must be specified for persistent fields or properties of type and Date and Calendar. 4) @Lob - Used for persisting a large object to a database. 5) @Embeddable - The persistent properties or fields of the embedded object is mapped to the database table for the entity. 6) @Embedded - Specifies a field or persistent property of an entity whose value is an instance to an embeddable class. 7) @EmbeddedId - Applied to a field or persistent property of an entity class or mapped superclass to denote a composite primary

100 java interview questions

Hi everyone, in this blogpost, I am specifying 100 interview questions which are commonly asked during a java developer interview. It includes questions about core java, j2ee, struts, servlets, jsp, hibernate, spring and database. The questions are as follows: Difference between method overloading and method overriding. Different types of Annotations used. Explain about lists and sets. What are there differences ? Difference between super() keyword and this() keyword ? Difference between abstract class and interface. When are they preferred ? Static and dynamic binding in java? What is synchronization in java? What is synchronized method ? Difference between serializable and externalizable interface in java. What is abstraction ? What is polymorphism? What is runtime and compile time polymorphism ? What is generics in java ? What are abstract class and abstract method ? What is marker interface ? Examples of marker interface ? What is static method, static

Hibernate Annotations - @Table and @Column

Hi Everyone, in this blog post we will learn more about using the hibernate annotations @Entity and @Column. In this program, we are modifying the table and column name of the fields in the sample program of hibernate , which was discussed in my earlier post. The name of the table is changed using @Table annotation and name of the column is changed using @Column annotation. The updated code for AccountDetails.java is as follows: package  com.hibernate; import  java.util.Date; import  javax.persistence.Column; import  javax.persistence.Entity; import  javax.persistence.Id; import  javax.persistence.Table; @Entity @Table  (name =  "ACCOUNT_TABLE" ) public   class  AccountDetails {      @Id      @Column (name =  "ACCOUNT_ID" )      public   int  accountId;           @Column  (name =  "ACCOUNT_NAME" )      public  String accountName;           @Column  (name =  "ACCOUNT_DATE" )      public  Date accountDate;       

Important annotations used in Hibernate

Hi everyone, in this blogpost, I am discussing about some of the important annotations used in Hibernate. Following are some of the important annotations: - @Entity - @Entity specifies that the class is marked as an entity bean . @Table -  @Table is used to specify the table for persisting data. The optional attributes of the table annotation is ' name ' and ' schema ' which specifies both the name and schema of the table respectively. Other attributes are ' unique constraints ' which specifies the unique constraints that are to be placed on the table and ' catalog ' which specifies the catalog of the table. @Id - @Id specifies the identifier property of the entity bean. It is like a primary key of the table. The mapped column for primary key of the entity is assumed to be primary key of the primary table. If no column annotation is specified, the primary key column is assumed to be the name of primary key property or field. @Column - @C

Sample program using hibernate

Hi Everyone, in this blog post, I will write a sample program using hibernate. Please check my earlier post about hibernate, which is mainly the introduction of hibernate - Introduction to hibernate.  In this program, we are trying to add some data to database using hibernate. Here, we have mainly 2 classes. One for creating the bean class and the second one for creating the SessionFactory and saving the transaction details. The bean class ( AccountDetails.java ) which describes the table and rows is as follows: package  com.hibernate; import  java.util.Date; import  javax.persistence.Entity; import  javax.persistence.Id; @Entity (name =  "ACCOUNT" ) public   class  AccountDetails {      @Id      public   int  accountId;      public  String accountName;      public  Date accountDate;           public   int  getAccountId() {          return  accountId;     }      public   void  setAccountId( int  accountId) {          this .accountId = accoun