Friday, September 3, 2010
Can't start Server within 50 seconds from Eclipse. How to increase the Time out value?
Eclipse gave the following error when I tried to run a jboss server for my webservice application.
Server JBoss v5.0 at localhost was unable to start within 50 seconds. If the server requires more time, try increasing the timeout in the server editor.
Fine, increase the time in server editor.. should be straight forward, I thought.. but then, I can't find a time out field to change the value...
After a minute or so, it turned out that you need to double click on the server that appears in the "Servers" tab("Servers" tab usually appears next to the "Console" tab). When you double click, a new window is opened in the main editor and then, Expand the "Timeouts" section..
Now I can change the 'Start time out value' to 300, then its worked fine....
Friday, August 27, 2010
String split in java
/*
Java String split example.
This Java String compare example describes how Java String is splited into multiple Java String objects.
*/
public class JavaStringSplitExample{
public static void main(String args[]){
/*
Java String class defines following methods to split Java String object.
String[] split( String regularExpression )
Splits the string according to given regular expression.
String[] split( String reularExpression, int limit )
Splits the string according to given regular expression. The number of resultant substrings by splitting the string is controlled by limit argument.
*/
/* String to be splitted. */
String str = "one-two-three";
String[] temp;
/* delimeter */
String delimeter = "-";
/* given string will be splitted by the argument delimeter provided. */
temp = str.split(delimeter);
/* print splitted substrings */
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
/*
IMPORTANT : Some special characters needs to be escaped while providing them as delimeters like "." and "|".
*/
System.out.println("");
str = "one.two.three";
delimeter = "\\.";
temp = str.split(delimeter);
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
/*
Using second argument in the String.split() method, we can control the maximum number of substrings generated by splitting a string.
*/
System.out.println("");
temp = str.split(delimeter,2);
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
}
}
/*
OUTPUT of the above given Java String split Example would be :
one
two
three
one
two
three
one
two.three
*/
Friday, August 13, 2010
How to get the size of ResultSet (total number of rows/columns)?
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
To Get RowCount:
1. Using Query
// Get a record count with the SQL Statement
Statement stmt = connection.createStatement();
String strQry = "SELECT COUNT(*) AS rowcount FROM Person";
ResultSet rs = stmt.executeQuery(strQry);
rs.next();
// Get the rowcount column value.
int ResultCount = rs.getInt(rowcount) ;
rs.close() ;
2. Using Scrollable ResultSet:
String strQuery = "SELECT * FROM Person";
// Create a scrollable ResultSet.
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(strQuery);
// Point to the last row in resultset.
rs.last();
// Get the row position which is also the number of rows in the ResultSet.
int rowcount = rs.getRow();
System.out.println("Total rows for the query: "+rowcount);
// Reposition at the beginning of the ResultSet
rs.beforeFirst();
Thursday, July 15, 2010
Measuring time(in ms) for particular operation in java
public static void main(String arg[])
{
long startOperation = System.currentTimeMillis();
// Some operation
long endOperation = System.currentTimeMillis();
long timeTaken = endOperation - startOperation;
//long timeTaken = System.currentTimeMillis()-startOperation;
System.out.println("Time taken = "+timeTaken+" ms");
}
}
Iterator Demo
// Demonstrate iterators.
import java.util.*;
class IteratorDemo {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
// use iterator to display contents of al
System.out.print("Original contents of al: ");
Iterator itr = al.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// modify objects being iterated
ListIterator litr = al.listIterator();
while(litr.hasNext()) {
Object element = litr.next();
litr.set(element + "+");
}
System.out.print("Modified contents of al: ");
itr = al.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// now, display the list backwards
System.out.print("Modified list backwards: ");
while(litr.hasPrevious()) {
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();
}
}
Tuesday, June 29, 2010
Comparing two dates in JAVA
import java.util.Calendar;
public class MyDate {
public static void main(String[] args) {
Calendar c1=Calendar.getInstance();
c1.set(2009,22,05);
Calendar c2=Calendar.getInstance();
c2.set(2009,23,05);
if(c1.compareTo(c2)<0)
{
// return -1 if date1 is less than date2
System.out.println("1. Date1 is less than date2");
}
else if(c1.compareTo(c2)>0)
{
// return 1 if date1 is greater than date2
System.out.println("1. Date1 is greater than date2");
}
else
{
// return 0 if date1 is equal to date2
System.out.println("1. Date1 is equal to date2");
}
/**
* Compare date with after(), before() and equals() method
*/
if(c1.after(c2))
{
// if date1 is greater than date2
System.out.println("2. Date1 is greater than date2");
}
else if(c1.before(c2))
{
// if date1 is less than date2
System.out.println("2. Date1 is less than date2");
}
else if(c1.equals(c2))
{
// if date1 is equal to date2
System.out.println("2. Date1 is equal to date2");
}
}
}
Example programs of Collection
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList
//
arlist.add("First Element"); // adding element in ArrayList
arlist.add("Second Element");
arlist.add("Third Element");
arlist.add("forth Element");
arlist.add("fifth Element");
// add element with index for fix order
arlist.add(2, "Fixed Order of Element");
// arlist.size() inform number of elements in ArrayList
System.out.println("ArrayList Size :"+arlist.size());
// get elements of ArrayList
for(int i=0;i
// duplicate element is not permitted
hs.add("b");
hs.add("a");
hs.add("c");
hs.add("d");
hs.add("d");
Iterator it=hs.iterator();
while(it.hasNext())
{
String value =(String)it.next();
System.out.println("Value :"+value);
}
//find size of hashSet
System.out.println("Size :"+hs.size());
// Remove element from hashSet :
hs.remove("d");
// To remove all object from hashSet
hs.clear();
}
}
HashTableExample :
import java.util.Hashtable;
import java.util.Enumeration;
public class HashTableExample {
public static void main(String[] args) {
Hashtable
//adding or set items in Hashtable by put method key and value pair
hTable.put(new Integer(2), "Two");
hTable.put(new Integer(1), "One");
hTable.put(new Integer(4), "Four");
hTable.put(new Integer(3), "Three");
hTable.put(new Integer(5), "Five");
// Get Hashtable Enumeration to get key and value
Enumeration em=hTable.keys();
while(em.hasMoreElements())
{
//nextElement is used to get key of Hashtable
int key = (Integer)em.nextElement();
//get is used to get value of key in Hashtable
String value=(String)hTable.get(key);
System.out.println("Key :"+key+" value :"+value);
}
}
}
HashMapExample :
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashMapExample {
public static void main(String[] args) {
HashMap