Wednesday, December 29, 2010

Array To List & Vice Versa

Converting an array to a List type
Object[] array = new Object[]{"12","23","34"};
java.util.List list = Arrays.asList(array);

Object can be a String array or Integer array. However, they cannot be a primitive array.
To convert a primitive array such as int to a List, the int array must be converted to an Object array such as Integer array.

Converting a List to an array.
List list = new ArrayList();
list.add("AB");
list.add("CD");
list.add("EF");
String[] sl = (String[]) list.toArray(new String[list.size()]);

To find the minimum in a integer array,
int min = (int) Collections.min(Arrays.asList(lenArr));
where lenArr is an Object[] array such as an Integer array.

To find the minimum in a integer array,
int max = (int) Collections.max(Arrays.asList(lenArr));

Wednesday, November 10, 2010

Setting JVM Size in Eclipse

One quick way to resolve the Java OutOfMemory error in Eclipse...
1) Open 'Debug Configurations' in Eclipse. (Run --> Debug Configurations)
2) In the left pane, click on 'Java Application'.
3) The list of java application ( with the main() method) are listed here.
4) Select the required Java Application.
5) In the right pane, click on the '(x)=Arguments'. Under VM Arguments, type
-Xmx1g - This will increase the heap size to 1 GB
-Xmx256m - This will increase the heap size to 256 MB.
6) Click Apply and Ok.

Wednesday, September 15, 2010

DB2 Basic Commands

1) To import data from a csv file to a table in database:
import from C:\testfile.csv of del insert into workemployee
2) To export data to a csv file from a table in database:
export to C:\names.csv of del select * from names
3) To connect to db2 database
connect to database_name user user_id using pwd
4) To set the schema for accessing the tables under a schema
set current schema schema_name
5) To list all tables under the schema or in general to list all tables. Two ways of doing the same.
- list tables for schema schema_name
- list tables

Sunday, May 30, 2010

Javascript Tips - 2

To trim a word using Javascript, use the following:
var field1 = document.formname.textbox.value.replace(/^\s+\s+$/g, "");

Deployment in JBoss

Following are the steps to deploy a war file in JBoss.
1) First, create a HTML page - form.html or a JSP file.
2) Have a folder say, CoffeeV1. Have a folder inside CoffeeV1 with the name WEB-INF. Place the form.html file or JSP inside CoffeeV1.
3) Create a folder under WEB-INF called classes.
4) Create deployment descriptor(web.xml) file with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"&gt;
<web-app>

<display-name>Hello Coffee</display-name>
</web-app>
5) Place the web.xml file inside WEB-INF.
6) Run the following commands to create a war file.
D:\HFSApps>cd CoffeeV1
D:\HFSApps\CoffeeV1>set CLASSPATH=C:\j2sdk1.4.2_02\bin
D:\HFSApps\CoffeeV1>set JAVA_HOME=C:\j2sdk1.4.2_02
D:\HFSApps\CoffeeV1>"%JAVA_HOME%\bin\jar.exe" -cvf CoffeeV1.war *.html WEB-INF
D:\HFSApps\CoffeeV1>copy CoffeeV1.war C:\jboss\server\default\deploy\
7) Start the JBoss server using run.bat command.
8) In web browser, enter http://localhost:8080/CoffeeV1/form.html.


Thursday, April 10, 2008

Javascript Tips

1) Hide a form element using javascript:
The following is a simple textbox which will be hidden by default:
<input type="text" id="text_id" name="text_name" style="visibility: hidden">
The following javascript code will make the hidden textbox visbile.
document.getElementById('text_id').style.visibility = 'visible';

2) Working with <SELECT> tags.
A simple <SELECT> tag would be created as follows:
<SELECT name="select_name" id="select_id"></SELECT> - This creates an empty drop down list.
<SELECT name="select_name" id="select_id"><OPTION value="1">1</OPTION></SELECT> - This creates a drop down with a value 1 and text as 1.
Adding a new option dynamically using javascript
var txt = '2'; // txt is the value that is set as the text of the option
var val = '2'; // val is the value that is set in the 'value' attribute of the option
var newOpt = new Option( txt, val, false); // Creating a new option object. false implies this option will not be pre-selected in the drop down on adding to the <SELECT> option.
document.<form_name>.<select_name>.options[document.<form_name>.<select_name>.options.length] = newOpt; // Assigns the newly created option to the <SELECT> list.
Getting the length of a drop down list.
document.<form_name>.<select_name>.options.length
Getting a pointer to the value that is pre-selected in the drop down currently.
document.<form_name>.<select_name>.selectedIndex.text - This returns the text that is set for the selected option.
document.<form_name>.<select_name>.selectedIndex.value - This returns the value that is set for the selected option.
Removing an element from the <SELECT> list
var x=document.<form_name>.<select_name>;
x.remove(x.selectedIndex);

3) Confirmation alert
var input_box=confirm("Do you want to delete the selected network set?");
if (input_box==true)
{
alert("You clicked OK");
}else{
alert("You clicked Cancel");
}