Sunday, 29 January 2012

Insert details using java into mongodb

package com.lnt.mongodb;
import java.io.File;
import java.net.UnknownHostException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class ParseExcel
{
 public static void main(String[] args) throws UnknownHostException, MongoException
 {
  File inputWorkbook = new File("Your Excel File Name");//Extension should be .xls
 
  Workbook wrkBk;
 
  //For local mongodb server  you can mention both "localhost" and "127.0.0.1"
  //For remote mongodb server you can mention directly remote system ipaddress
 
  Mongo mongo = new Mongo( "127.0.0.1" );
 
  DB db = mongo.getDB( "Your DB Name");
 
  DBCollection collection = db.getCollection("Your Collection Name");
 
  try
  {
   wrkBk = Workbook.getWorkbook(inputWorkbook);
  
   for(int sheetno=0;sheetno<Number of sheets in excel ;sheetno++)
   {
    Sheet workSheet = wrkBk.getSheet(sheetno);
  
     for(int i=1; i<workSheet.getRows(); i++)
     {
     
      //here you can get values from excel
      Cell cell = null;
      cell = workSheet.getCell(0, i);
    
      String psno = cell.getContents();
         
      BasicDBObject doc = new BasicDBObject();
 
      //if you mention _id ,then it will generate user primary key
      //otherwise it will automatically generate one primary key
      doc.put("_id", id);
     
      //insert value in collection
      doc.put("collection field name",which value you what to insert);
     
      For example :doc.put("username",mongo);
      doc.put("userid",1);
 
      collection.insert(doc);
     }
   }
      
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 
 }

}

Saturday, 28 January 2012

My first Post

Hi ,
This is my first day in blog spot.Main purpose of this blog is "mention about 'NoSql' new software MongoDB Importance' and how we can use this , what are the steps needed . I hope this this blog spot helpful for whoever like to use MongoDB