Thursday, February 12, 2015

importing .sql file into a database using wamp



















when you have a sql file you can easily import it in to a database Using wamp.

1. left click wamp -> phpmyadmin
2. select database then click import (top right)
3. locate the database and click go.
You can use the command prompt as well. For that,

1. Run the cmd (DOS) and get into the mysql folder, which in my case works like this
C:\>cd C:\wamp\bin\mysql\mysql5.0.51b\bin
2. Then use this command to fire up MySQL

C:\wamp\bin\mysql\mysql5.0.51b\bin>mysql.exe -use databasename -u username -p

you should provide a password if you have one.
After that copy .sql file to directory where mysql.exe is located and run the command ,
mysql> source myfilename.sql;

Sunday, February 8, 2015

Use java Comparable interface to sort objects

In previous tutorial we looked at how to sort int, double and String arrays using java Arrays.sort() method. That is not all that we can do with that method. We can sort Objects as well.
But when we come to Objects there is a small problem. Lets see that problem and solution for that using java Comparable Interface.

This is the code for Student class
public class Student {

 private String name;
 private int marks; 

 public Student(String name,int marks){
  this.name=name;
  this.marks=marks;
 } 

 public void setName(String name) {
  this.name = name;
 }

 public String getName() {
  return name;
 }

 public void setMarks(int marks) {
  this.marks = marks;
 }

 public int getMarks() {
  return marks;
 }
}


If we create few objects of Student class and try to sort them using Arrays.sort() method,
  
public class ObjSort {

 public static void main(String args[]){ 

  Student[] students = new Student[4];

  Student pineappale = new Student("nimal", 70);
  Student apple = new Student("Anuradha", 95);
  Student orange = new Student("Tharaka", 82); 
  Student banana = new Student("Isuru", 90); 

  students[0]=pineappale;
  students[1]=apple;
  students[2]=orange;
  students[3]=banana; 

  //sort with student's marks 
  Arrays.sort(students); 

  int i=0;
  for(Student temp: students){
   System.out.println("Students " + ++i + " : " + temp.getName() +
   ", Marks : " + temp.getMarks());
  }
    }
}

It will give an error because to sort there should be something to compare. But in Student objects there are multiple attributes to compare. To sort objects on particular attribute we have to implement Comparable class and override the  compareTo() method in Student class.
After making those differences it will look like this.
 
public class Student implements Comparable<Student> { 

 private String name;
 private int marks; 

 public Student(String name,int marks){
  this.name=name;
  this.marks=marks;
 }  

 public void setName(String name) {
  this.name = name;
 }
 public String getName() {
  return name;
 }

 public void setMarks(int marks) {
  this.marks = marks;
 }

 public int getMarks() {
  return marks;
 } 

 public int compareTo(Student compStudent) { 
  int comparemark = compStudent.getMarks(); 

  //if we want to sort in ascending order
  return this.marks - comparemark; 
 } 
}

Now we can use Arrays.sort() method on array of Student objects because now it compares objects on marks attribute.

Sort an array using java Arrays class

There are lot of sorting algorithms to use. but if we are in a hurry or we just want to sort a small set of data using sort() method of java Arrays class is the best thing you can do.
So lets see how to sort an array using java Arrays class.
here is the coding example.

import java.util.Arrays;

public class ArraySort {
public static void main (String[] args){  

  String[] names = { "Nimal", "Sunil", "Tharaka", "Isuru","Anuradha"};
  int[] intNumbers = { 3, 5, 12, 7, 1, 9};
  double[] doubleNumbers = { 2.34, 45.213, 4.76, 4.23, 8.32};  

  //sorting String array 'names'
  Arrays.sort(names);  

  int i=1;
  System.out.println("After sorting String array");
  for(String name:names)
   System.out.println("item"+i+++" - "+name);  

  //sorting int array 'intNumbers'
  Arrays.sort(intNumbers);    

  i=1;
  System.out.println("After sorting int array");
  for(int num:intNumbers)
   System.out.println("item"+i+++" - "+num);   

  //sorting double array 'doubleNumbers'
  Arrays.sort(doubleNumbers);    

  i=1;
  System.out.println("After sorting double array");
  for(double num:doubleNumbers)
   System.out.println("item"+i+++" - "+num);  

 }
}

Output for this code will be like this.

After sorting String array
item1 - Anuradha
item2 - Isuru
item3 - Nimal
item4 - Sunil
item5 - Tharaka
After sorting int array
item1 - 1
item2 - 3
item3 - 5
item4 - 7
item5 - 9
item6 - 12
After sorting double array
item1 - 2.34
item2 - 4.23
item3 - 4.76
item4 - 8.32
item5 - 45.213

This Arrays.sort() method support not only for String, int and double arrays but also for byte, char, long, float and object arrays

Sorting strings using java Collections

In this tutorial Lets see how to sort collection of Strings in alphabetical order in a very simple way. We use java Collections for that.

Here is the coding example.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionSort {
 public static void main (String[] args){ 

  List<String> names = new ArrayList<String>();  

  names.add("Nimal");
  names.add("Sunil");
  names.add("Tharaka");
  names.add("Isuru");
  names.add("Suresh");
  names.add("Anuradha");
  names.add("Thilak");  

  Collections.sort(names);  

  int i=1;
  for(String name:names)
   System.out.println("item "+i+++" "+name);
 }
}

Output for this code will be like this.


item 1 Anuradha
item 2 Isuru
item 3 Nimal
item 4 Sunil
item 5 Suresh
item 6 Tharaka
item 7 Thilak


Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...