Friday, February 25, 2011

Using IMDb api to get details of movie from Java application

The Internet Movie Database (IMDb) is an online database of information related to movies, television shows, actors, production crew personnel, video games and fictional characters featured in visual entertainment media by Amazon.com. IMDbdoes not provide any api but some websites on the internet provides api which can be used to access IMDb database. One of the such website is http://www.deanclatworthy.com/imdb/.

Here a simple Java application shows how to access these apis. You can visit http://www.deanclatworthy.com/imdb/ for more information about api.


/*********************************************************************************
  Program to demonstrate how to use IMDB api to get details about any movie
  Developed by : Malhar Vora
  Developed on : 25-02-2011
  Development Status : Completed and tested
  Email              : vbmade2000@gmail.com
  WebSite            : http://malhar2010.blogspot.com
**********************************************************************************/

import java.io.*;
import java.net.*;
import java.util.*;

public class IMDBDemo{
 
 public static void main(String []str){
   
  URL url = null;
  Scanner sc = null;
  String apiurl="http://www.deanclatworthy.com/imdb/";
  String moviename=null;  
  String dataurl=null;
  String retdata=null;
  InputStream is = null;
  DataInputStream dis = null;
  
  

  try{
    
   //Getting movie name from user
   sc = new Scanner(System.in);
   moviename=sc.nextLine();
   
   //Check if user has inputted nothing or blank
   if(moviename==null || moviename.equals("")){
    System.out.println("No movie found");
    System.exit(1);
   }
   
   //Remove unwanted space from moviename yb trimming it
   moviename=moviename.trim();
   
   //Replacing white spaces with + sign as white spaces are not allowed in IMDB api
   moviename=moviename.replace(" ","+");
   
   //Forming a complete url ready to send (type parameter can be JSON also)
   dataurl=apiurl+"?q="+moviename + "&type=text";
      
   System.out.println("Getting data from service");
   System.out.println("########################################");
   
   url = new URL(dataurl);   
   
   is = url.openStream();
   dis  = new DataInputStream(is);
   
   String details[];
   //Reading data from url
   while((retdata = dis.readLine())!=null){
    //Indicates that movie does not exist in IMDB databse
    if(retdata.equals("error|Film not found")){
     System.out.println("No such movie found");
     break;
    }
    
    //Replacing | character with # character for spliting
    retdata=retdata.replace("|","#");
    
    //Splitting up string by # character and storing output in details array
    details=retdata.split("#");
    
    //details[0] contains name of detail. e.g title,genre etc
    System.out.print(details[0].toUpperCase() + " -> ");
    
    //details[1] contains value of detail. e.g The Cave
    System.out.print(details[1]);
    System.out.println();
       
   }  
      
  }  
  catch(Exception e){
   System.out.println(e);
  }
  finally{
   try{
   
    if(dis!=null){
     dis.close();
    }
    
    if(is!=null){
     is.close();
    }
    
    if(sc!=null){
     sc.close();
    }
   }
   catch(Exception e2){
    ;
   }
  }
   
     
 }

}

Wednesday, February 23, 2011

Simple interest calculator using Servlets ( Practical : 23, MCA, Sem : 4, GTU Programs )

Download simple interest calculator developed using Servlet and html.
Click here to download

Sunday, February 6, 2011

Using Rapidshare.com api to check file status on rapidshare.com

/*********************************************************************************
Program to demonstrate how to that file exist on rapidshare.com server using api provided by rapidshare.com
  Developed by : Malhar Vora
  Developed on : 6-1-2011
  Development Status : Completed
  Email              : vbmade2000@gmail.com
  WebSite            : http://malhar2010.blogspot.com
**********************************************************************************/
 import java.net.*;
import java.io.*;
import java.util.*;

public class RapidChecker
{
 public static void main(String []str)
 {
  URL u=null;
  String temp[];
  Scanner sc = null;
  String strurl=null;
  try{
  
   sc = new Scanner(System.in);
   System.out.println("Enter rapidshare url to check :");
   strurl = sc.nextLine();
   u = new URL(strurl);
   
   //Getting fileid and filename by splaitting string into parts
   temp = u.getFile().split("/");
   
   //Calling function to check file status
   checkRapidShareFile(Integer.parseInt(temp[2].trim()),temp[3]);
   
  }
  catch(Exception e)
  {
   System.out.println("Error in checking file");
  }
  
 
 }
 
 static void checkRapidShareFile(int file,String filename)
 {
  URL u = null;
  InputStream is = null;   
  DataInputStream dis = null;
  String s=null,url=null;
  String data[];
   
  
  try
  {
   url = "http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles&files=" +file + "&filenames=" + filename;
   u = new URL(url);
   is = u.openStream();
   dis = new DataInputStream(is);
   
   s=dis.readLine();
   data = s.split(",");
   
   
   System.out.println(data[4]);
   
    if(data[4].equals("0"))
    {
    System.out.println("File not found");
    }
    else if(data[4].equals("1"))
    {
    System.out.println("File found");
    }
    else if(data[4].equals("3"))
    {
    System.out.println("Server down");
    }
    else if(data[4].equals("4"))
    {
    System.out.println("File is illegal");
    }
    else if(data[4].equals("5"))
    {
    System.out.println("File is locked because 10 download is already done");
    }
    else
    {
    System.out.println("Undefined response");
    }
    
    
   
  }
  catch(Exception e)
  {
   System.out.print(e);
  }
  finally
  {
   try
   {
    dis.close();
    is.close();
   }
   catch(Exception e2)
   {
     System.out.print("Error in closing streams");
   }
  }
  
 }
 
}

Saturday, February 5, 2011

Connect MS Access database using Type - I JDBC driver

import java.sql.*;

/*********************************************************************************
Program to demonstrate connection to MS Access database using JDBC driver Type - I
  Developed by : Malhar Vora
  Developed on : 6-1-2011
  Development Status : Completed
  Email              : vbmade2000@gmail.com
  WebSite            : http://malhar2010.blogspot.com
**********************************************************************************/

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


 Connection conn=null;
 Statement st=null;
 ResultSet rs=null;
 try { 
  Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
 } 
 catch (Exception E) {
  System.err.println ("Unable to load driver.");
  E.printStackTrace ();
 } 
 
 
 try { 
  String db = "jdbc:odbc:db1"; 
  conn = DriverManager.getConnection (db, "","");
  st = conn.createStatement();
  rs = st.executeQuery("Select * from emp");
  while(rs.next())
  {
    
   System.out.println(rs.getString(1));
  }
  System.out.println ("*** Connected to the database ***"); 

    
 } 
 catch (SQLException E) { 
  System.out.println ("SQLException: " + E.getMessage()); 
  System.out.println ("SQLState: " + E.getSQLState()); 
  System.out.println ("VendorError: " + E.getErrorCode()); 
 } 
 try { 
  st.close();
                if(conn!=null)
                {
                     conn.close (); 
                } 
 } 
 catch (SQLException E) {
  System.err.println ("Unable to load driver.");
  E.printStackTrace ();
 } 
}

}