Tuesday, May 10, 2011

Simple WebService Client

You will need apache-axis.jar in your classpath. The following client is apache axis based. A similiar client can be built also using the JAX-WS only[without apache-axis.jar].



import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

public class WEBSERVICE_CLIENT{
public static void main(String [] args) {
try {
String endpoint ="";

Service service = new Service();
Call call = (Call) service.createCall();

// YOU CAN SET VARIOUS PROPERTIES IN THE "CALL" OBJECT...LIKE
// USERNAME,PASSWORD,PROPERTIES OF HTTP HEADER. SEE "CALL" OBJECT'S API FOR MORE PROPERTIES.

call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName(new QName("", ""));

String msg = (String) call.invoke( new Object[] { "data" } );

System.out.println("RETURNED MESSAGE : "+msg);

} catch (Exception e) {
System.err.println(e.toString());
}
}
}

Friday, February 25, 2011

Load file in classpath in JAVA

package com.java.discussions101;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Main {


public static void main(String[] args) throws FileNotFoundException, IOException {


Properties prop = new Properties();


//prop.load(ClassLoader.getSystemResourceAsStream("property/file.txt"));

prop.load(Main.class.getResourceAsStream("/property/file.txt"));


String str=prop.getProperty("");

System.out.println(str);
}
}


You can choose 2 ways to load files and read it in your code. You can choose either the ClassLoader or the Class.clazz to load file. In my case I have a director in my root named "property" and inside it a file named "file.txt" is there.

Cheers....

Fetch all files contained in a directory and its sub-directories

package com.java.discussions101;

/**
* @author Supratim Samanta
* @since 13/01/2011 2025HRS
* @modified 14/01/2011 2348HRS
*
*/

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FileTree {

private static List fileArray = new ArrayList();
private static int count = 0;
private static final String location = "./";

public static void main(String[] args) throws IOException {

File file = new File(location);
getFileTreeIterated(file);
printAll(fileArray);
}

private static void getFileTreeIterated(File file) {
if (file.isDirectory()) {
File[] fileList = file.listFiles();
try {
for (File tempFile : fileList) {
if (tempFile.isDirectory()) {
getFileTreeIterated(tempFile);
} else {
fileArray.add(tempFile);
count++;
}
}
} catch (NullPointerException npe) {
return;
}
} else {
fileArray.add(file);
count++;
}
}

private static void printAll(List array) throws IOException {
if (!(array.size() == 0)) {
Iterator it = array.iterator();
System.out.println("The Number Of Files : " + array.size());

while (it.hasNext()) {
File tempFile = it.next();

System.out.println(tempFile.getCanonicalPath());
}
} else
System.out.println("NOTHING FOUND");
}

}




Just change the "location" String attribute to your directory path. All files in that directory wiil be displayed. In my case the location is the current directory.

Cheers....

Wednesday, February 23, 2011

Calculate TIME Difference in single sql query

SELECT
TRUNC(DATES.START_DATE - DATES.END_DATE) ||' DAYS '||
TRUNC((DATES.START_DATE - DATES.END_DATE) * 24 - TRUNC(TO_NUMBER(DATES.START_DATE - DATES.END_DATE)) * 24) ||' HOURS '||
TRUNC((TO_NUMBER(DATES.START_DATE - DATES.END_DATE) * 24 - TRUNC(TO_NUMBER(DATES.START_DATE - DATES.END_DATE) * 24))*60) ||' MINUTES '||
ROUND((((TO_NUMBER(DATES.START_DATE - DATES.END_DATE) * 24 - TRUNC(TO_NUMBER(DATES.START_DATE - DATES.END_DATE) * 24))*60) -
(TRUNC((TO_NUMBER(DATES.START_DATE - DATES.END_DATE) * 24 - TRUNC(TO_NUMBER(DATES.START_DATE - DATES.END_DATE) * 24))*60)))*60) ||' SECONDS ' AS TIME_DIFFERENCE

FROM (
SELECT TO_DATE('20110102123421', 'YYYYMMDDHH24MISS') AS START_DATE, TO_DATE('20110101101723', 'YYYYMMDDHH24MISS') AS END_DATE FROM DUAL
)DATES




OUTPUT : 1 DAYS 2 HOURS 16 MINUTES 58 SECONDS

Just change the dates in the outer sub query and with your own time format.
Enjoy..

Monday, October 18, 2010

PLSQL Developer Error - SQL*Net not properly installed [SOLVED]

Hello friends..

If you are using PLSQL developer to connect to the ORACLE database may have faced the problem "SQL*Net not properly installed".

This problem occur beacause ORACLE_HOME and the oci.dll path location may not have been configured in the preferences.

To do this, go to Tools>Preferences.

Now set the Oracle Home as the location of the ORACLE_HOME variable or where the Oracle is installed. In my case it is:

C:\Oracle\DataBase\product\10.2.0\db_1

After this set the oci.dll file location just under the Oracle Home text field.In my case it is:

C:\Oracle\DataBase\product\10.2.0\db_1\bin\oci.dll

click Apply>OK

Now restart the PLSQL developer. This should solve the problem.

Cheers..

Saturday, October 16, 2010

Eclipse 3.6 Permgen Error [SOLVED]

The latest SUN jdk release is the 21 update. As you all have noticed the eclipse 3.6 running under this jvm crashes due to permgen error when you try to run it. The reason behind it is that with Oracle/Sun JDK 1.6.0_21 (July 2010), the eclipse launcher cannot detect a Oracle/Sun VM, and therefore does not use the correct PermGen size.

If you are using either of this version, you have to add the Permgen size as below.



-startup
plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.0.v20100503
-product
org.eclipse.epp.package.jee.product
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
512m
--launcher.defaultAction
openFile
-vmargs
-XX:MaxPermSize=512m
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m


This will solve the annoying Permgen error in your eclipse.

Cheers...

Tuesday, October 12, 2010

Basic Encryption Using SHA Algorithm In JAVA



package com.java.discussions101;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


import sun.misc.BASE64Encoder;

public class Encryptor {
public static void main(String args[]){

String password="discussions101";

MessageDigest md = null;
try{
md = MessageDigest.getInstance("SHA");
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}
try{
md.update(password.getBytes("UTF-8"));
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}

byte raw[] = md.digest();
String hash = (new BASE64Encoder()).encode(raw);

System.out.println("Encrypted Password is : "+hash);
}

}



The Output is : "Encrypted Password is : yCYUi6yitVBINR/HKKdIj47alYM="


This program can be used for password authentication in user verification or in many purposes. Another good thing about this technique is that you can not get the original password back in form after encryption is done.

You may try the program backwards to check it out.

Cheers.....