Thursday, December 26, 2013

Execute procedure in informix

Following command can be used to execute procedure in informix database server.
In informix "dbaccess" shell, execute the following command. This will run the "this_is_it" procedure with three parameters "1", "2" and "3".

execute procedure this_is_it ('1','2','3');

create procedure this_is_it(this CHAR(3), that CHAR(4), other CHAR(3))
select * from inff_charge into temp this_is_it;
end procedure;

Monday, December 23, 2013

Force use IPv4 instead of IPv6 in JAVA

Sometimes, Netbeans IDE can't communicate via HTTP over IPv6. In that cases, you can let IDE to use IPv4 protocol instead of IPv6.

Following JVM option can be used to communicate over IPv4.

-Djava.net.preferIPv4Stack=true

Tuesday, December 10, 2013

Send nohup.out to different file


Following command can use to redirect nohup.out to different files.

nohup java -jar -Xms32m -Xmx64m  Application.jar  > AppLog.out &

Above command will redirect "Application.jar" output to "AppLog.out" file.

Tuesday, November 26, 2013

SFTP JAVA Client

"JSch" is a library that can be used for SFTP connection. You have to follow following steps to create SFTP connection. For this secure connection, you need to download "jsch-0.1.50.jar" and add to the project.

1) Create SSH key

GitHub Shell can be used to create a SSH key pair for secure connection.

Reference :- https://github.com/

In Git Shell, execute following commands to create key pair for particular server.

ssh-keygen -t rsa -C your_machine_ip

Above command will create "id_rsa.pub" and "id_rsa" keys for connection between your PC and server.

id_rsa :- Private key of FTP client machine
id_rsa.pub :- Public key of SFTP server machine

Reference :- https://help.github.com/articles/generating-ssh-keys

2) Write code snip to create connection and store file

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SFTP {

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

Session session = null;
        Channel channel = null;

        try {
            JSch ssh = new JSch();
            session = ssh.getSession("sujith", "192.168.2.6", 22);
            session.setPassword("sujith");

            String identityfile = "d:/.ssh/id_rsa";
            ssh.setKnownHosts("d:/.ssh/id_rsa.pub");
            ssh.addIdentity(identityfile);

            session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password");
            session.connect();

            channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftp = (ChannelSftp) channel;

            sftp.put("d:/test.pdf", "/apps/home/sujith/");
            
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        }
    }
}

Sunday, October 27, 2013

Grouping data by SQL expression

Following SQL syntax can be used to group data using expression.

select date(added_time) as a,count(*) from test_table group by a                
This will group data by date and display its count. It doesn't work grouping with expression, So need to use alias naming for expression.

Wednesday, September 18, 2013

JAVA Resultset metadata information

In JAVA, you can use retrieve information about resultset using following code.

String str ="select * from table001";

ResultSet rs = st.executeQuery(str);
ResultSetMetaData rsmd = rs.getMetaData();

System.out.println("Number of columns - " + rsmd.getColumnCount());
System.out.println("JAVA data type for Table field 3 - "+rsmd.getColumnClassName(3));
System.out.println("Database Table field 3 type - "+rsmd.getColumnTypeName(3)); 

Tuesday, August 13, 2013

Avoid submitting form when enter key is pressed

Sometimes, you have to avoid submitting page when enter key is pressed. You need to add following style to  text box.

<input id="txtName" name="txtName" style="display: none;" type="text" />

Wednesday, August 7, 2013

Struts forward parameters to URL in action class

In some cases, you have to send additional parameters with the action forwarding.
Following code snip can be used to set additional parameters in the action class of the struts. 

ActionRedirect redirect = new ActionRedirect(mapping.findForward(forwardPage)); 
redirect.addParameter("username", "user"); 

redirect.addParameter("password", "PWD");
return redirect;

Tuesday, August 6, 2013

Find files which contains word using Linux command

Following Linux command can be used to find files which contains specific words. For example, if you need to search files which contain word "test", you can use the following command.

find . -exec grep -l "test" {} \;

Thursday, August 1, 2013

Avoid "Grid Layout Exception" in Jasper reports

When it is creating multiple reports by threads in Jasper, you will get "Grid Layout Exception" occurred by the jasper library. To avoid that, you can add the following property to JVM.

JRProperties.setProperty("net.sf.jasperreports.text.measurer.factory", "net.sf.jasperreports.engine.util.JdkGlyphFixTextMeasurerFactory");

How to get Linux bash command list

Type history in the Linux terminal and enter.

This will list all the commands you executed in the bash.

Wednesday, July 31, 2013

Count number of files in a directory using Linux command

Following command can be used to get number of files in the directory. This "maxdepth" option lets you to define maximum depth (sub directory levels) to search files.

find ./targetdir/ -maxdepth 20 -type f -follow | wc -l

Thursday, July 4, 2013

Create RSA public & private keys in Linux

Using following command you can create public & private keys in Linux.

ssh-keygen -t rsa

This command can be executed in any SSH enabled Linux machine. RSA keys will be stored in .ssh directory created in your home directory.

Thursday, April 25, 2013

Validate email address in JS

Following java script function can be used to check email validation.

function checkEmail() {        

var email = document.getElementById('txtEmail');      

  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;        
   if (!filter.test(email.value)) {      
   alert('Please provide a valid email address');      
   email.focus;      
   return false;   
}

Wednesday, April 17, 2013

Informix set connection timeout

jdbc:informix-sqli://192.168.2.15:1528/database:INFORMIXSERVER=online;user=sujith;password=sujith;INFORMIXCONRETRY=3;INFORMIXCONTIME=60

Above JDBC connection URL can be used for connection timeout issues in Informix database.

INFORMIXCONRETRY : connection retry times
INFORMIXCONTIME : connection time

Connection timeout can be occurred due to database slowness and after maximum connection reach.
You have to change JDBC connection URL.

Thursday, April 4, 2013

Verify HOST servers entrusted certificates in JAVA

In sometimes, it has to be trusted the certificates in JAVA client when applications are developed. This may occur when hosting URL doesn't match with certificate URL.

Following JAVA code can be used to verify host as a trusted host.

   static {
   javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){

   public boolean verify(String hostname,
               javax.net.ssl.SSLSession sslSession) {
       if (hostname.equals("192.168.11.123")) {
          return true;
       }
          return false;
       }
   });
     } 

Sunday, March 24, 2013

MySQL cluster JDBC URL


Following code can be used to create a JDBC Connection to MySQL cluster. Here, cluster contains two nodes as 192.168.2.23 & 192.168.2.24.

Class.forName("com.mysql.jdbc.Driver").newInstance();

url = "jdbc:mysql:loadbalance://
                + “192.168.2.23:3306, 192.168.2.24:3306/YourDataBaseName”
                + "?"
                + “user=YourUserName&password=YourPassword”
                + "&loadBalanceConnectionGroup=first&loadBalanceEnableJMX=false&autoReconnect=true";

DriverManager.getConnection(url);

Monday, March 18, 2013

Check application running in Linux


#!/bin/bash

PID1=`ps -eaf | grep app1.jar |grep -v  grep |wc -l`
if [ $PID1 -eq 0 ]
        then
        cd /apps/app1/
        java -jar -Xms32m -Xmx64m app1.jar &
        echo "app1.jar ....................................... Started"
        sleep 1
        else
        echo "app1.jar      ..................................... Already Running"
fi

Above script can be used to check application status. If "app1.jar" is running, it will be printed "Already Running" else, it will start the "app1.jar".

Friday, March 8, 2013

Find 12 months previous date

Following SQL commands can be used to find the date before 12 months

In Informix,
select * from table where connected_date  < today - (12 UNITS MONTH)

In MySQL,
select * from table where added_date>DATE_ADD(TODAY,INTERVAL -12 MONTH)

Thursday, February 21, 2013

Gson convertion between Object & String


Following JAVA function can be used to convert "Response" object to "String".
Gson library need to add to project.

Gson gson=new Gson();
Response response=new Response();
response.setStatus("OK");
response.setMessage("Test JSON");
String rtnStr=gson.toJson(response);
return rtnStr;
   
This code snip convert "String" to "Response" object.
  
Gson gson=new Gson();
Response r=gson.fromJson(str, Response.class);

Tuesday, February 19, 2013

Unzip batch of files in Linux

Following command can be used to unzip batch of files in Linux.

gzip -d *.gz

Rename batch of files in Linux

Following awk script can be used to rename list of files.
Here, it will rename ".txt.pdf" files into ".txt" files.

for k in *.pdf; 
do mv $k `echo $k | sed 's/txt.pdf/txt/g'`; 
done

Copy files from one location to another in Linux

Following "scp" command can be used to copy files from one location to another.

scp * sujith@192.168.1.153:/apps/sujith/data

Monday, February 4, 2013

SQL concat string in informix

Suppose database contains mobile_no field which has records like "0715355403".....

If you want to remove front "0" and add "94", you can use the following SQL command.

select  "94"||mobile_no[2,10] from sms_request

This is can used in SQL where clause, too.

Thursday, January 17, 2013

Store certificate in key store

When it is using trusted certificate in JAVA, it needs to be stored in JAVA key store.

JAVA default key store is %JAVA_HOME/jre/lib/security/cacerts

Using following command you can create a specific key store.

keytool -keystore keystore_name -storepass changeit -file \export\home\root.cer -import -alias alias_name -trustcacerts

Then use the following JAVA code to use the created key store.

System.setProperty("javax.net.ssl.trustStore", "./keystore_name ");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");


Wednesday, January 16, 2013

Restart JAVA Process in crontab


Following is a shell script which can be used for restart JAVA application. If Application active count is zero(0), then this script can be used to restart the process. This shell script can be called by a crontab.

cat /dev/null > nohup.out
PRGCOU=`ps -eaf|grep JAVAApp.jar |grep -v grep |wc -l`

if [ $PRGCOU -eq 0 ]
then
nohup java -Xms32m -Xmx64m -jar JAVAApp.jar &
sleep 3
exit
fi