Saturday 1 August 2015

Convert Collection into Comma Separated String


                            Convert Collection into Comma Separated String

while coding some module ,there comes a scenario where you need to convert your collection into comma separated string.
Suppose for example you need to pass list of strings to Stored procedure but your procedure argument accepts only varchar value.
in that case you need to convert your list of strings into comma separeted strings.


Below are some ways which you can implement.

1) Iterate over list elements,Concatenate each of the list elements and comma until last element  

 package com.ipaccess.nos.gui.module.config.wizard.changeProductClass;  
 import java.util.ArrayList;  
 import java.util.List;  
 public class ListToString {  
   public static void main(String[] args) {  
    //Supoose this is your list containing integer {1,2,3,4}  
    List<Integer> numbers = new ArrayList<Integer>();  
    numbers.add(1);  
    numbers.add(2);  
    numbers.add(3);  
    numbers.add(4);  
    //StringBuilder to store comma separated string    
    StringBuilder commaSepString = new StringBuilder();  
    // iterate over list,append numbers and comma until last element    
    for (int i = 0; i < numbers.size(); i++) {  
      commaSepString.append(numbers.get(i));  
      if (i != numbers.size() - 1) {  
       commaSepString.append(", ");  
      }  
    }  
    System.out.println(commaSepString.toString());  
   }  
 }  


2) you can make use of google-collections.jar which has a utility class called Joiner

    String commaSepString=Joiner.on(",").join(numbers);
  
  To download this jar ,you can use this link 
http://www.java2s.com/Code/Jar/g/Downloadgooglecollections10jar.htm

3) Another way is you can use StringUtils class which has function called join.To make use of StringUtils class,you need to use common-lang3.jar

    String commaSepString=StringUtils.join(slist, ',');
  To download this jar ,you can use this link 
http://www.java2s.com/Code/Jar/c/Downloadcommonlang3jar.htm



Enjoy Learning.:)

Thursday 30 July 2015

Execute mysql procedure from command line


                      Execute mysql procedure from command line


To call mysql stored proceudres from command line,first login into mysql using this command

     mysql –u[username] –p[password] database_name then,

Suppose your procedure name is countNoOfRows(),use this command

      call countNoOfRows();

To call parameterized stored procedure,

     call countNoOfRows(1,’myRow’);

To execute stored procedure and prints its output in txt file,open command line and use this command, suppose your procedure name is “calculate_raw_data()”

 mysql -uroot -prootDB nos -e "call calculate_raw_data('2,3','23')" >/exports/satish/output.txt

It will print the output in ouput.txt file which gets created in location >/exports/satish/.

This can be used when you are trying to debug your mysql stored procedure. You can print output at each line and monitor afterwards what’s exactly happening in your stored procedure.

As in many Linux distributions mysql gui application doesn’t work, above command can be useful for debugging purpose.
 




Frequently Used Linux Commands for Java Developer


                           Frequently Used Linux Commands for Java Developer


Many of the software developer’s work in Linux environment. Below are the list of some frequently used Linux commands.

1) ls -l :- It displays file or directory ,size,modified date and time ,file or folder name,its owner and its permissions.

2) find :-this command used to find files.
            For ex:-To find file in current directory:- find . –name hello.txt
                        To find file under home directory:- find /home –name hello.txt

3) kill:- To close running process,you can use kill -9 process_id or kill -15 process_id.
            Process_id you can find using ps aux | grep process_name

4) top:- this command will show information like tasks,memory,cpu etc.press q to exit or ctrl+c

5) free:-It display used memory and available memory of the system.

6) kill java process:
    ps ax | grep "java" | egrep -v "eclipse" | cut -b1-06 | xargs -t kill

7) free system memory:-
     echo 3 > /proc/sys/vm/drop_caches

8) pwd or cd - :- it shows current directory path.

9)To start,stop ,restart my sql service
    start mysql service:-/etc/init.d/mysqld start
    stop mysql service :- /etc/init.d/mysqld stop
    restart mysql service :- /etc/init.d/mysqld restart

10)To start,stop,restart network
     start network service: /etc/init.d/network start
     stop network service: /etc/init.d/network stop
     restart network service: /etc/init.d/network restart

11) set virtual ip address
     /sbin/ifconfig eth1:0  <vip> netmask 255.255.255.0

12) ifconfig:-displays your machines ip addess and all the interfaces details.

13)  shortcut key :ctrl+alt+f1 –displays the terminal which you can use if your GUI gets hanged up.

14) dhclient:-dhclient reads the file dhclient.conf for configuration instructions. It then gets a list of all the network interfaces that are configured in the current system. For each interface, it attempts to configure the interface using the DHCP protocol. 

For detailed information about this commands,browse google J

 



Java program to break the string after certain length

Java Program to break the String after certain length

Sometimes you want to break the long string value into smaller segments to display in small sized window.
For ex: In my case I had to display legend in Jasper Graph and my legend was quite long as compared to my Graph size So legend was not getting displayed properly.So I had to break the legend into equal sized smaller chunks .

Here is the sample program:


 /**  
  *   
  */  
 /**  
  * @author Dixit  
  *   
  */  
 public class BreakLongString {  
      public static void main(String a[]) {  
           String dn = "aaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbcccccccccccccdddddddddddeeeeeee";  
           StringBuffer legend = new StringBuffer();  
           if (dn.length() >= 20) {  
                String[] lines = Split(dn, 20, dn.length());  
                for (int j = 0; j < lines.length; j++) {  
                     legend.append(lines[j]);  
                     if (j != lines.length - 1) {  
                          legend.append("\n");  
                     }  
                }  
           }  
           System.out.println("legend:\n"+legend);  
      }  
      private static String[] Split(String text, int chunkSize, int maxLength) {  
           char[] data = text.toCharArray();  
           int len = Math.min(data.length, maxLength);  
           String[] result = new String[(len + chunkSize - 1) / chunkSize];  
           int linha = 0;  
           for (int i = 0; i < len; i += chunkSize) {  
                result[linha] = new String(data, i, Math.min(chunkSize, len - i));  
                linha++;  
           }  
           return result;  
      }  
 }