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;  
      }  
 }  

Saturday, 25 July 2015

Mysql Backup and Restore command for linux


Backup MySQL DB
In order to take a backup of MySQL database,first you need to make sure that required database is present in database server and you have a required access to it.

The following command is used to take an backup:

mysqldump -u[username] -p[password] [database name] > [file name.sql]

for ex: mysqldump -uroot -prootDB mydb>/exports/db/mysql.sql


If you want to take an backup of all databases present in database server,then use following command:

mysqldump -uroot -prootDB --all-databases>/exports/db/allmysql.sql


To take a backup of mysql procedures ,functions along with tables,use this command:

mysqldump -uroot -prootDB --routines mydb>/exports/db/mysql.sql

Restore MySQL DB

To restore the database use this command:

mysql -u[username] -p[password] [database name] < [file name.sql]

for ex: mysql -uroot -prootDB mydb</exports/db/mysql.sql



In case of any doubt,please post a question.







Java Program to count number of 0's and 1's in n*n matrix




Given a matrix which contains only 0's and 1's .We need to count the number of occurrences of zeroes and ones in it.Its a very simple program but we get confused when it is asked by someone else and we start looking for complex solutions.


Hers is the implementation:-



 import java.util.Scanner;  
 /**  
  *   
  */  
 /**  
  * @author Dixit  
  *   
  */  
 public class SampleMatrix {  
      public static void main(String args[]) {  
           int m, n,i,j;  
           int sum=0;  
           Scanner in = new Scanner(System.in);  
           System.out.println("Enter the number of rows and columns of matrix");  
           m = in.nextInt();  
           n = in.nextInt();  
           int first[][] = new int[m][n];  
           System.out.println("Enter the elements of first matrix");  
           for (i = 0; i < m; i++)  
                for (j = 0; j < n; j++)  
                     first[i][j] = in.nextInt();  
           for (i = 0; i < m; i++)  
                for (j = 0; j < n; j++)  
                     sum = sum+first[i][j] ;   
           System.out.println(" Count of number of 1's in the matrix:-"+sum);  
           System.out.println(" Count of number of 0's in the matrix:-"+(m*n-sum));  
      }  
 }  


Difference between HashMap and ConcurrentHashMap



HashMap and ConcurrentHashMap are two different types of hashMap in java.Both implements Map interface but their internal working is different.


                                                     HashMap Vs ConcurrentHashMap



               HashMap


              ConcurrentHashMap
Null Value/Key


 It allow null value as well as key.
 It does not allow null values and keys.
 Thread Safe 


No.In order to make hashmap thread safe we need to use Collections.synchronized(Map) method
 Yes


Concurrency
 It does not allow concurrent access to multiple threads
 It allows a very high level of concurrency. Multiple threads can work on different portion of ConcurrentHashMap


 Iterator
 It implements fail fast.
 It implements fail safe.







Study internal working of both types of hashMap .It’s very important concept asked in interviews.