Tuesday, October 28, 2014

AJAX call using JQuery

AJAX call using  JQuery 

Example
$.ajax({url:validateUrl,
type: 'POST',
async:false,
data: 'method=validateCondition&condition='+data,
success: function(answerFromServer){
onSuccessValidateCondition(answerFromServer,fieldId);
}
});

Description:
url: URL where the AJAX call will be send.
type: POST , GET etc
async: true/false (Default is True, True will allow other content of the page to load independently of the ajax call. false is reverse of it) 
data: the particular method of the servlet you want to call, and other parameters you can append.
sucess: function(answerFromServer){} : answerFromServer will contains the data sent by server in response of the ajax call

Monday, October 27, 2014

Java Coding Care4 : Avoid Null Pointer Exception

Topic : Avoid Null Pointer Exception

Advantage
Code Execution will be liberal and better. Functionality break chances will be reduced.

Example

1)
if ("check me".equals(param)) // Do like this
{
// some code
}

2)
String str = (param == null) ? "NA" : param;

3)

Use String.valueOf() Rather than toString()

4)
Object object =null;
String.valueOf(object); /// is OK
String.valueOf(null); // throws Exception

Use collections default to get the empty objects of respected data struture
List<String> list = Collections.EMPTY_LIST;
Set<String> set = Collections.EMPTY_SET;
Map<String,String> map = Collections.EMPTY_MAP;

knownObject.equals(unknownObject)

Monday, October 20, 2014

Java Coding Care3: Use DBSPY for Database related issue tracking

Topic : DB SPY

log4jdbc SPY should be use for DB related issue Tracking/Logs

Advantage
It is useful to SPY / DEBUG database related issue tracking. It generates all kind of user
required logs


Libs Required
log4j-1.2.16.jar
log4jdbc4-1.2.jar
log4j-over-slf4j-1.7.5.jar
slf4j-api.jar"
slf4j-jdk14.jar"
slf4j-log4j12-1.7.5.jar"
slf4j-simple-1.7.5.jar"

ojdbc6-11.0.2.0.jar"

Example
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class SpyEngine {

public static void main(String[] rk) {

try {

  Class.forName("net.sf.log4jdbc.DriverSpy");
Connection conn = DriverManager.getConnection( "jdbc:log4jdbc:oracle:thin:@192.168.8.87:1521:rajdb", "rajusername", "rajpassword");

PreparedStatement prStmt = conn.prepareStatement("select * from tbdata");
prStmt.execute();
prStmt.close();

  conn.close();
 
} catch (Exception e) {
System.out.println("Error: " + e);

}
}

Sunday, October 19, 2014

Java Coding Care2 : Prepare TLD in jsp

Topic : TLD (Tag Library Descriptor)

Prepare TLD in jsp for some common features and tags

Example
Need /Use the custom prepared TLD tag. For some common code we can
prepare custom TLD tags (i.e Logo, specifc conditional statements)


Advantage
No dependency on others tags

Java Coding Care1: Use Declaration in JSP

Topic : Declaration in JSP
Always try to use Declaration in JSP page

Example
Need to Use declarations in jsp instead of SCRIPLET for common global
variables
.i.e <%! String path="......"; %>


Advantage

Each time a service method is being called then Variable declaration will be discarded if we will declaration. It declares variable in global scope of a generated servlet for that particular jsp.

Monday, October 6, 2014

Apache DBUTIL simple useful demo code

Apache DBUTIL simple useful demo code

Required Jar files
1) commons-dbutils-1.5.jar (Apache)
2) ojdbc6-11.0.2.0.jar (Oracle)

Functionality

Create table
Insert Data
Update Data
Update Partial Data
Delete Table

Code

DatabaseWorkController.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


class DatabaseWorkController{
private Connection connection = null;
private QueryRunner queryRunner = null;

long startTime = 0;

DatabaseWorkController(){ }

public void loadDriverAndMakeConnection(){
System.out.println("Method loadDriverAndMakeConnection()\n");
try
{

      Class.forName("oracle.jdbc.driver.OracleDriver");    
connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.8.87:1521:aaadb", "netvertextrunk", "netvertextrunk");     
queryRunner = new QueryRunner();

}catch(Exception e) {
       System.out.println("Error : "+e);
}  
}

public void insertData(){
System.out.println("\nMethod insertData()");
try {
startTime = System.currentTimeMillis();

int result = queryRunner.update(connection, "INSERT INTO PLAYERS (ID, NAME, TEAM, TOTALRUNS) VALUES(?, ?, ?, ?)","1","SACHIN","India",46000);
result = queryRunner.update(connection, "INSERT INTO PLAYERS (ID, NAME, TEAM, TOTALRUNS) VALUES(?, ?, ?, ?)","1","SAMSON","India",1000);
System.out.println("Inserted Rows: "+result);
System.out.println("Total Time taken: "+(System.currentTimeMillis()-startTime)+" ms");
} catch (SQLException e) {
e.printStackTrace();
}
}

public void selectAllData(){
System.out.println("\nMethod selectAllData()");

ResultSetHandler<List<PlayerData>> rsh = new BeanListHandler<PlayerData>(PlayerData.class);
try {
startTime = System.currentTimeMillis();
List<PlayerData> result = queryRunner.query( connection, "SELECT * FROM PLAYERS",rsh);
for(PlayerData player: result){
System.out.println(player);  
}
System.out.println("Total Time taken: "+(System.currentTimeMillis()-startTime)+" ms");
} catch (SQLException e) {
e.printStackTrace();

}

public void oldWaySelectAllData(){
System.out.println("\nMethod oldWaySelectAllData()");
ResultSet rs = null;
try {
startTime = System.currentTimeMillis();
Statement stmt = connection.createStatement();
rs = stmt.executeQuery("select * from players");
while(rs.next()){
int id = rs.getInt("ID");
String name = rs.getString("name");
String team = rs.getString("team");
String totalRuns = rs.getString("totalruns");
System.out.println("PlayerData [Id=" + id + ", Name=" + name + ", Team=" + team
+ ", TotalRun=" + totalRuns + "]");
}
rs.close();
System.out.println("Total Time taken: "+(System.currentTimeMillis()-startTime)+" ms");
} catch (SQLException e) {
e.printStackTrace();

}

public void selectParticularData(){
System.out.println("\n\nMethod selectParticularData()");

ResultSetHandler<PlayerData> rsh = new BeanHandler<PlayerData>(PlayerData.class);

try {
startTime = System.currentTimeMillis();
PlayerData player = queryRunner.query( connection, "SELECT * FROM PLAYERS WHERE ID = ? ",rsh, "1");
System.out.println("Total Time taken: "+(System.currentTimeMillis()-startTime)+" ms");
System.out.println(player);  
} catch (SQLException e) {
e.printStackTrace();

}

public void updateParticularData(){
System.out.println("\n\nMethod updateParticularData()");
try {
 
startTime = System.currentTimeMillis();
int result = queryRunner.update(connection,"UPDATE PLAYERS SET NAME = ? WHERE ID = ? ","Sachin Tendulkar","1");
System.out.println("Total Time taken: "+(System.currentTimeMillis()-startTime)+" ms");
System.out.println("Update Rows: "+result);
} catch (SQLException e) {
e.printStackTrace();
}
}

public void deleteTable(){
System.out.println("\n\nMethod deleteTable()");
try {
startTime = System.currentTimeMillis();
int result = queryRunner.update(connection,"DROP TABLE PLAYERS");
System.out.println("Total Time taken: "+(System.currentTimeMillis()-startTime)+" ms");
System.out.println("Table deleted successfully: "+result);
} catch (SQLException e) {
e.printStackTrace();
}

}

public void destroyConnection(){
System.out.println("\n\nMethod destroyConnection()");
try {
startTime = System.currentTimeMillis();
DbUtils.close(connection);
System.out.println("Total Time taken: "+(System.currentTimeMillis()-startTime)+" ms");
} catch (SQLException e) {
e.printStackTrace();
}
}

public void createTable() {
System.out.println("\n\nMethod createTable()");
try {

startTime = System.currentTimeMillis();
int result = queryRunner.update(connection,"CREATE TABLE PLAYERS ( ID int, Name varchar(255), Team varchar(255), TotalRuns varchar(255))");
System.out.println("Total Time taken: "+(System.currentTimeMillis()-startTime)+" ms");
System.out.println("Table created successfully: "+result);
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public class DBUtilFlight {

    public static void main(String[] dataBag) 
    {
    DatabaseWorkController controller = new DatabaseWorkController();
    controller.loadDriverAndMakeConnection();
   
    controller.createTable();
    controller.insertData();
    controller.selectAllData();
    controller.oldWaySelectAllData();
   
    controller.selectParticularData();
    controller.updateParticularData();
    controller.selectParticularData();
   
    controller.deleteTable();
    }
}


PlayerData.java

public class PlayerData {

private String id;
private String name;
private String team;
private Long totalRuns;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public Long getTotalRuns() {
return totalRuns;
}
public void setTotalRuns(Long totalRuns) {
this.totalRuns = totalRuns;
}

@Override
public String toString() {
return "PlayerData [Id=" + id + ", Name=" + name + ", Team=" + team
+ ", TotalRun=" + totalRuns + "]";
}


}





Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...