« May 2009 | Main | August 2009 »

June 20, 2009

Excel to MYSQL: Here's another technique

- Anjana. M

I had to upload pin codes in excel to database. After procrastinating on net for considerable time, I decided to find a quick solution... :) Though I am aware of available excel api’s, I dint want to spend time in reading api docs for breaking a thin twig. This method can be followed if you want to export a single column from excel to a table.  

Steps:
  1. So i copied the pin code column  from the excel and pasted on to a notepad
  2. Save the file as ok.txt
  3. then I wrote program that will read a the file line by line and a simple string operation
  4. The program will generate a Sql script and will also execute the script and will insert the data into database.

    "*********Sample output*********"

insert into tblpincode_zone(pincodes,zone)values(834001,9);

insert into tblpincode_zone(pincodes,zone)values(834002,9);

***********************************

package beans;  

import java.io.*;

import java.sql.*;

class FileRead

   public static void main(String args[])

  { 

       try{

           Connection con=null; 

        Statement pst=null;

       String str=""; 

       Class.forName("com.mysql.jdbc.Driver");

    con=DriverManager.getConnection("jdbc:mysql://192.169.70.45/application?user=root&password=root");         

    // Open the file that is the first

    // command line parameter 

    FileInputStream fstream = new FileInputStream("D:/ok.txt");

    // Get the object of DataInputStream 

    DataInputStream in = new DataInputStream(fstream);

        BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

    String strLine;

    //Read File Line By Line 

    while ((strLine = br.readLine()) != null)   {

      pst=con.createStatement(); 

      // Print the content on the console

      System.out.println ("insert into tblpincode_zone(pincodes,zone)values("+strLine+",9);");      str="insert into tblpincode_zone(pincodes,zone)values("+strLine+",9);"; 

     pst.executeUpdate(str);

    } 

    //Close the input stream

    in.close(); 

    }catch (Exception e){//Catch exception if any

      System.err.println("Error: " + e.getMessage()); 

    }

  }

}

 

(The author, Anjana.M, is a Software Engineer at Binary Spectrum).

 

June 19, 2009

Ruby on Rails: Convensions over Configurations

- Jagadish. M

The interest of developer will always be towards learning new technologies. It’s secondary that whether in real-time you are going to use it in or not. One of the most popular and fastest growing technologies that I came across is Ruby on Rails (RoR).

Ruby on Rails was developed by David Heinemeier Hansson from his work on BaseCamp; a project management tool, which offers to-do lists, wiki-style web-based text documents, milestone management, file sharing, time tracking, and a messaging system.

Like many web frameworks, Rails uses the Model View Controller (MVC) architecture to organize applications.

Features of Ruby:
  • Each and Everything is Object: Even numbers in ruby are treated as object. Looking at the example, what output you can expect from this?

8.times {print “Guess what”} its prints Guess what 8 times.

  • Powerful blocks: Code blocks can be passed as parameter to method. Following example prints out all the elements in an array

    jArray.each{|element| print element}

  • Return is optional in Methods: In ruby return statement is optional in methods. The value of the last expression becomes the return value of that method. For example: 

    def testReturn n = 2 * 3 en

  • Parallel Assignment in Ruby: We can change the multiple variables in one assignment statement. Swapping of two variables is the good example for it.   n1 = 1

    n2 = 3

     n1, n2 = n2,n1

  • Everything in Ruby is open: Including the built-in classes, in ruby, additional methods can be added to the classes even at run-time also. For example: FixNum is the build in Data Type for all to which I added the method called old.                         
  • class Fixnum

    def old return self-1

    end

    end

    8.old  # prints 7

    (The author, Jagadish.M, is a Software Engineer at Binary Spectrum).