Tuesday, September 25, 2012

byte[] to Object in java

If data must be sent over network, it is needed to send  data as byte stream. In these cases, it can be used object serialize method as follows. Below method can be used to convert Object to byte stream. 

public static byte[] serialize(Object obj) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(out);
    os.writeObject(obj);
    return out.toByteArray();
}


Below deserialize method can be used to converted stream back to Object. If Object is specific use Object casting methods further.

public static Object deserialize(byte[] data) {
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    ObjectInputStream is = new ObjectInputStream(in);
    return is.readObject();
}


NOTE :- This can be avoided by using Java RMI technology.

Wednesday, September 12, 2012

RedHat Server IP Address configuration

In RedHat Linux, following steps can be used for configuring IP address of the machine. For modifying files, you must be logged as root user.

1) Log as root
2) Change directory to "/etc/sysconfig/network-scripts"
3) vi ifcfg-eth0 file & add following parameters

    IPADDR =192.168.102.10
    NETMASK=192.168.102.1

4) Restart network adapter using following command.

    /etc/init.d/network restart

5) Check IP address using ifconfig command.


Change file permission for execution

In Linux, it can be used chmod command to change permission of the file.

Simply, this can be done using as follows.

 chmod +x application.bin

Above +x means, file is grated for execution.

Tuesday, September 11, 2012

Linux crontab Command

crontab is used for executing scheduled processes. For example, you can run database backup everyday using schell script.

crontab -l 

Above command list the all available crontab command.

crontab -e

This command can be used to edit crontab command fro your preference.

Sample crontab command is shown below. This script.sh script is executed every day at midnight.
0 0 means zero minute at zero hours(midnight).

0 0 * * * /home/sujith/script.sh

Wednesday, September 5, 2012

MySQL days between SQL Syntax (Between Days)

In MySQL, there is a special ways to get data between two days.

select * from ret_bonus where DATE(start_time) between date_sub(curdate(), interval 5 day) and CURDATE()

Above SQL returns data between today and 5days before today. 

date_sub(curdate(), interval 5 day)

This MySQL command returns 5days before today.