Google+ Followers

Connect

Get Updates Via Email

Enter your email address:

Delivered by FeedBurner

Saturday, 16 November 2013

How to create and execute stored procedure in linux mysql command? [ Linux Mysql: Create and execute stored procedures ]

Sql Scripts for creating stored procedures:

mysql > delimiters //
mysql > use userDB;
          > select database();
          > create procedure userdbprdt()
          >  begin
          >  select u.id,u.name,u.username,ur.product from usertbl u join userproduct ur where u.id=ur.id;             > end//


Sql script for executing stored procedure

mysql> call userdbprdt()//

Java Swing MySql JDBC: insert data into database

Program

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class insertswing implements ActionListener
{
  JFrame fr;JPanel po;
  JLabel l1,l2,main;
  JTextField tf1,tf2;
  GridBagConstraints gbc;
  GridBagLayout go;
  JButton ok,exit;

public insertswing(){
fr=new JFrame("New User Data ");
Font f=new Font("Verdana",Font.BOLD,24);
po=new JPanel();
fr.getContentPane().add(po);
fr.setVisible(true);
fr.setSize(1024,768);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
po.setBackground(Color.WHITE);
go=new GridBagLayout();
gbc=new GridBagConstraints();
po.setLayout(go);
main=new JLabel("Enter User Details ");
main.setFont(f);
l1=new JLabel("Name  :");tf1=new JTextField(20);
l2=new JLabel("User Name  :");tf2=new JTextField(20);
ok=new JButton("Accept");
exit=new JButton("Exit");

gbc.anchor=GridBagConstraints.NORTH;gbc.gridx=5;gbc.gridy=0;
go.setConstraints(l1,gbc);po.add(main);

gbc.anchor=GridBagConstraints.WEST;gbc.gridx=5;
gbc.gridy=5;go.setConstraints(l1,gbc);po.add(l1);


gbc.anchor=GridBagConstraints.WEST;gbc.gridx=10;gbc.gridy=5;
go.setConstraints(tf1,gbc);po.add(tf1);

gbc.anchor=GridBagConstraints.WEST;gbc.gridx=5;gbc.gridy=10;
go.setConstraints(l2,gbc);po.add(l2);

gbc.anchor=GridBagConstraints.WEST;gbc.gridx=10;
gbc.gridy=10;go.setConstraints(tf2,gbc);po.add(tf2);

gbc.anchor=GridBagConstraints.WEST;gbc.gridx=10;gbc.gridy=90;
go.setConstraints(ok,gbc);po.add(ok);ok.addActionListener(this);

gbc.anchor=GridBagConstraints.WEST;gbc.gridx=11;gbc.gridy=90;
go.setConstraints(exit,gbc);po.add(exit);
exit.addActionListener(this);

}

@Override
public void actionPerformed(ActionEvent ev)
{
    if(ev.getSource()==ok)

    {
        try{
Connection con;JLabel last =new JLabel("Data inserted successfully");
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection
("jdbc:mysql://localhost/userDB","root","root");
 PreparedStatement ps=con.prepareStatement
("Insert into usertbl values(default,?,?)");
String id=tf1.getText();String sname=tf2.getText();
ps.setString(1,id);ps.setString(2,sname);
ps.executeUpdate();con.close();
tf1.setText("");tf2.setText("");
    JOptionPane.showMessageDialog(fr, "Data inserted successfully.");
        }
catch(Exception e){
    System.out.println("ERROR  "+e);
}
    }
if(ev.getSource() == exit){fr.dispose();

}
}


    public static void main(String a[]){
    new insertswing();
    }

}






output

 1. Adding values and data inserted successfully popup message




 2. Checking inserted value in database table


MySql linux terminal Commands: Basic commands to create and drop databases commands

1. Login on mysql  command



mysql -u root -h localhost -p








2. Check Status

mysql> status










3. show databases

mysql> show databases;














4. Show variables

mysql> show variables;



5. Show processlist

mysql> show processlist;



6. Using and selecting system already available mysql database

mysql> use mysql;
mysql> select database();


7. select user table in mysql database

mysql> select Host, User from user
or

mysql> select Host, User from mysql.user



8. Create database and display it

mysql> create database tempDB;
mysql> show databases;
mysql> use tempDB;
mysql> select database();



9. Drop database

 mysql> drop database tempDB;
 mysql> show databases;



10. exit  mysql 

 mysql> exit


 

Friday, 15 November 2013

Data Structure and Algorithms : Queue Program in Java

Program:

public class queuepgm {
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;

public queuepgm(int s)

{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}

public void enqueue(long j)

{
if(rear == maxSize-1)
rear = -1;
queArray[++rear] = j;
nItems++;
}

public long dequeue()
{
long temp = queArray[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public long peekFront()

{
    return queArray[front];
}
public boolean isEmpty()      
{
return (nItems==0);
}
public boolean isFull()
{
return (nItems==maxSize);
}
public int size()
{
return nItems;
}
public static void main(String[] args)
{
queuepgm theQueue = new queuepgm(5); // queue holds 5 items
theQueue.enqueue(10);
theQueue.enqueue(20);
theQueue.enqueue(30);
theQueue.enqueue(40);
System.out.println("Dequeue Values: FIFO");
while( !theQueue.isEmpty() )
{
long n = theQueue.dequeue();
System.out.print(n+"\n");

}
}

}

Output:


Data Structure and Algorithms : Stack Program in Java

Program

public class stackpgm {
private int maxSize;
private long[] stackArray;
private int top;
public stackpgm(int s)

{
maxSize = s;

stackArray = new long[maxSize];
top = -1;

}

public void push(long j)

{
    if(!isFull())
    {
      stackArray[++top] = j;
    }

}
public long pop()
{
   
      return stackArray[top--];  
   
     
}

public long peek()

{
return stackArray[top];
}

public boolean isEmpty()

{
return (top == -1);
}

public boolean isFull()
{
return (top == maxSize-1);
}

public static void main(String[] args)
{
stackpgm theStack = new stackpgm(10);
theStack.push(1);
theStack.push(2);
theStack.push(3);
theStack.push(4);
System.out.println("Pop up values");
while( !theStack.isEmpty() )
{
long value = theStack.pop();
System.out.print(value+"\n");
}
}


}

Output


Head and Tail Commands in Linux


Head Command

Description : prints output of first part of the file by default it will print first 10 lines

Syntax : head filename

examples:

1.  head upcasting.java


it will print first 10 lines of the file upcasting.java by default.

2. head -n 50 upcasting.java
 it will print first 50 lines of the file upcasting.java.

3. head upcasting.java downcasting.java
it will print first 10 line of the 2 files upcasting.java and downcasting.java.

4. head *.java
it will print all the java files first 10 lines.

5. head *.java *.class
it will print all the java and class files first 10 lines.


Tail Command:

Description : prints output of last part of the file by default it will print last 10 lines

Syntax : tail filename

Examples:

1. tail upcasting.java
it will print last 10 lines of the file upcasting.java by default.

2. tail -n 50 upcasting.java
 it will print last 50 lines of the file upcasting.java.

3. tail upcasting.java downcasting.java
it will print last 10 line of the 2 files upcasting.java and downcasting.java.

4. tail *.java
it will print all the java files last 10 lines.

5. tail *.java *.class
it will print all the java and class files last 10 lines.

Practice Examples:

1. How to Print the 11th line of the file ?

    head -n 11 upcasting.java | tail -n 1

2. How to print lines from 5 to 11th of the file ?

    head -n 11 upcasting.java | tail -n 5

3.  How to print number of lines head and tail command file (by default it will  print 10 lines)?
   
    head upcasting.java | wc -l
    tail upcasting.java | wc -l


    both will return 10

    head -n 20 upcasting.java | wc -l
    tail -n 20 upcasting.java | wc -l


   both will return 20

   for number of word wc -w and number of characters wc -c

   head upcasting.java | wc -c
   it will print number of first 10 lines characters in digits

   tail upcasting.java | wc -c
   it will print number of last 10 lines characters in digits
 

   head upcasting.java | wc -w
   it will print number of first 10 lines words in digits

   tail upcasting.java | wc -w
   it will print number of last 10 lines words in digits

4. How to get First and last 5 files of java in a java directory?

   ls *.java | tail -n 5  
   ls *.java | head -n 5
 
   ls -l *.java | tail -n 5  
   ls -l *.java | head -n 5

5. ls -la *.class | tail -n 12 | wc -l  it will return 12

6.  ls -la *.class | tail -n 12
it will shows last 12 class files list

7.  ls -la *.class | head -n 12 
it will shows first 12 class files list.  

8.      head -c 10 downcasting.java
9.      tail -c 10 downcasting.java
10.   head -c 400  downcasting.java | tail -n 3

(updates still come)

Monday, 11 November 2013

Linux Terminal Commands

Install Command

sudo apt-get install <package-name>
Example:
Installing vlc media player

sudo apt-get install vlc



ls Command

ls -l
list folders and files with read write permissions and user groups

ls -aThis command shows hidden files and folders

ls -al
ls -la


Clear Command

clear

this command clears the screen

Manual Command

man <command>

this command shows manual of the commands

example man ls shows manual command of ls


q to quit the manual screen back to terminal commands

ifconfig command

ifconfig command ip network address details

netstat command

netstat command used to show network status like ip addresses of local and foreign; port, pid, network protocols

cat Command 

cat command concatenate files and print on the standard output.























Java Program: Down casting Inheritence

Program

public class downcasting {
   public static void main(String[] args)
    {
     fordd f;
     benzz b;
     BMWw bm;
     carr c,cc,ccc;
     c=new fordd();
     cc=new benzz();
     ccc=new BMWw();
     f=(fordd)c; // downcasting or narrowing or specialization
     b=(benzz)cc;
     bm=(BMWw)ccc;
     f.car();
     b.car();
     bm.car();
    }
}
class carr
{
    void car()
    {
        System.out.println("This super class car method");
    }
}
class fordd extends carr
{
    void car()
    {
        System.out.println("This sub class Ford car method");
    }
}
class benzz extends carr
{
     void car()
    {
        System.out.println("This sub class Benz car method");
    }
}
class BMWw extends carr
{
     void car()
    {
        System.out.println("This sub class BMW car method");
    }

}

Output



Java Program: Up casting Program in inheritance

Program

public class upcasting {
   public static void main(String[] args)
    {
     car c,cc,ccc,o;
     o=new car();
     o.car(); //no casting
     c=(car)new ford(); //upcasting ford sub class to super class
     c.car();
     cc=(car)new benz();//upcasting ford sub class to super class
     cc.car();
     ccc=(car)new BMW();//upcasting ford sub class to super class      
     ccc.car();
   
    }
}
class car
{
    void car()
    {
        System.out.println("This super class car method");
    }
}
class ford extends car
{
    @Override
    void car()
    {
        System.out.println("This sub class Ford car method");
    }
}
class benz extends car
{
     @Override
    void car()
    {
        System.out.println("This sub class Benz car method");
    }
}
class BMW extends car
{
     @Override
    void car()
    {
        System.out.println("This sub class BMW car method");
    }

}

Output



Java Program: Inheritance Super Keyword

Program

class inheritance
{
 public static void main(String[] args)
 {
  apple a=new apple();
  a.disp();
 }
}
class fruit
{
 String f="fruit";
 public void disp()
 {
  System.out.println("Base Class "+f);
 }
}
class apple extends fruit
{
 String f="Apple";
 public void disp()
 {
  System.out.println("Derived Class  "+f);
  super.disp();
  System.out.println("Super Class "+super.f);
 }

}

Output


Java Program: File I/O Fileoutputstream to create file

Program

import java.io.*;
public class createfile {
    public static void main(String[] args)
    {
        try
        {
        createfile();
        }
        catch(Exception ex)
        {
            System.out.println("File Exceptio occured");
        }    
    }
    public static void createfile() throws java.io.IOException
    {
        DataInputStream d=new DataInputStream(System.in);
        FileOutputStream f=new FileOutputStream("jack.txt",true);
        BufferedOutputStream b=new BufferedOutputStream(f,1024);
        System.out.println("Enter $ at the end to finish text");
        char c;
        while((c=(char)d.read())!='$')
        {
            b.write(c);          
        }
        b.close();
    }

}

Output



Java Program: AWT Frame Closing Event

Program

import java.awt.*;
import java.awt.event.*;
public class awtexample extends Frame{
    public static void main(String[] args)
    {
        Frame f= new Frame("New Frame");
        f.setSize(400, 500);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e)
        {
        System.exit(0);
        }
     });
    }
}

Output




Sunday, 10 November 2013

Java Program: Multi Thread optimum way utilize CPU time using Thread(Runnable object, String thread_name)

Program

import java.util.logging.Level;
import java.util.logging.Logger;

class ThreadRunnnable {
    public static void main(String[] args)
    {
       
       new ThreadRun();
       new ThreadRunn();
       System.out.println("Main Thread Starts");
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }      
     
       
    }
}
class ThreadRun implements Runnable
{
    ThreadRun()
    {
       
        Thread t=new Thread(this,"Thread1");
        t.start();
        System.out.println("Thread 1");
       
    }
    @Override
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
   
    }  
               
}

class ThreadRunn implements Runnable
{
   
    ThreadRunn()
    {
       
        Thread t=new Thread(this,"Thread1");
        t.start();
        System.out.println("Thread 2");
       
    }
    @Override
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
               
    }  
               
}


Output

Thread 1
Thread 2
Main Thread Starts
Main Thread 1
Thread 2 1
Thread 1 1
Thread 2 2
Thread 2 3
Thread 2 4
Main Thread 2
Thread 2 5
Thread 1 2
Main Thread 3
Main Thread 4
Thread 1 3
Main Thread 5
Thread 1 4
Thread 1 5

Output of this program not unique it always random, because it depends on thread sleep method due to multiple threads, one thread waits and other threads are executing that's why java multi threads utilize the cpu time in optimum way

Java Program: Thread using Runnable interface program

Program

import java.util.logging.Level;
import java.util.logging.Logger;

class ThreadRunnnable {
    public static void main(String[] args)
    {      
       new ThreadRun();
       new ThreadRunn();
       System.out.println("Main Thread Starts");
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }      
     
    }
}
class ThreadRun implements Runnable
{
    ThreadRun()
    {      
        Thread t=new Thread();
        t.start();
        System.out.println("Thread 1");
        run();
    }
    @Override
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }  
    }                
}
class ThreadRunn implements Runnable
{  
    ThreadRunn()
    {      
        Thread t=new Thread();
        t.start();
        System.out.println("Thread 2");
        run();
    }
    @Override
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println(i);
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadRun.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
             
    }  
}

Output

Thread 1
1
2
3
4
5
Thread 2
1
2
3
4
5
Main Thread Starts
1
2
3
4

5

Java Program: Thread in swing implements Runnable interface and SwingUtilities

Program
import javax.swing.*;
public class SwingThread extends JFrame {
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        final JFrame f = new JFrame("Swing using Thread Frame 1");
        f.setSize(600, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
     
        final JFrame fr = new JFrame("Swing using Thread Frame 2");
        fr.setSize(600, 600);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setVisible(true);
      }
    });
    }
 

}

Output:


Java Program: Simple Thread Example

public class threadexample {
    public static void main(String args[])
    {
        Thread t=Thread.currentThread();
        System.out.println("Current Thread: "+t);
        t.setName("Thread Example");
        System.out.println("Current Thread Name changed as: "+t);
    }

}

output

Java Program: Swap two numbers without temp variable

import java.io.*;
public class Swapwithouttemp {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter first number");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int x=Integer.parseInt(br.readLine());    
        System.out.println("Enter second number");
        BufferedReader brr=new BufferedReader(new InputStreamReader(System.in));
        int y=Integer.parseInt(brr.readLine());        
        new swap(x,y);
    }
}
class swap
{  
    swap(int a, int b) throws IOException          
    {
        System.out.println("Before swapped values a= "+a+"\tb= "+b );
        a=a+b;
        b=a-b;
        a=a-b;
        System.out.println("After swapped values a= "+a+"\tb= "+b );
    }
}

Java Program: User defined input using BufferedReader, Checking Employee Age

import java.io.*;
public class EmployeeAgeCheck {
    public static void main(String[] args) throws IOException
    {
        EmployeeData e=new EmployeeData();
        e.getdetails();
        e.checkage();      
    }
}
class EmployeeData
{
    private String name;
    private int age;

    public void getdetails() throws java.io.IOException
    {
        // user defined input at runtime
        BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter name");
        name=buf.readLine();
        System.out.println("Enter Age");
        age=Integer.parseInt(buf.readLine());
    }
    public void checkage()
    {
        if(age<=30)
        {
            System.out.println(name+ " ,You're Junior Employee");
        }
        else
        {
            System.out.println(name+ " ,You're Senior Employee");
        }
    }
}

Java Program : Default and Parameterized Constructor

public class constructorexample {
    public static void main(String[] args)
    {
        personcon kumaran=new personcon();
        kumaran.display();
        personcon jannu=new personcon("Jannu",22);
        jannu.display();
    }
}

class personcon
{
    private String name;
    private int age;
    //default constructor
    personcon()
    {
        name="kumaran";
        age=23;
    }
   //parameterized constructor
    personcon(String n,int a)          
    {
        name=n;
        age=a;
    }
    public void display()
    {
        System.out.println("Name: "+name+"\t Age: "+age);
    }
}

Saturday, 9 November 2013

How to download google chrome browser in linux using terminal command?

Step 1: Download Google Chrome

For 32 bit version

$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb

For 64 bit version

$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

Step 2: Install .deb file

For 32 bit version

$ sudo dpkg -i google-chrome-stable_current_i386.deb

For 64 bit version

$ sudo dpkg -i google-chrome-stable_current_amd64.deb
Issues:

if you get any error while installing or downloading just use this below command to clear the issues

1. $ sudo apt-get update
2. $ sudo apt-get upgrade


3. $ sudo apt-get -f install
After installation check it google chrome in linux dash home search:




Thursday, 7 November 2013

How to check CPU usage in Linux OS?


Generally top is a command to check CPU usage on any OS.

top command


































































htop Command

In Linux Terminal command to CPU usage htop is another to do the task

but before install htop as: Sudo apt-get install htop in terminal then after use htop command other than iostat, /proc/meminfo,mpstat are the commands to be used to check CPU and memory usages