import java.sql.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.util.*;

public class releasedates {
	
	
	public static void main (String[] args) throws Exception
       {
       	
       	
       	
       	String primo,secondo;
		secondo = null;
		String year=null;
		int movieid=0;
		
       	String[]res;
		String[]arr=null;
        
          	
         FileWriter fi1 = new FileWriter("G:\\Documents and Settings\\cristiano\\Desktop\\tesina seminario\\tesina\\finalScripts\\countryfromrelease.sql");
		PrintWriter out1=new PrintWriter(fi1);
		FileWriter fi2 = new FileWriter("G:\\Documents and Settings\\cristiano\\Desktop\\tesina seminario\\tesina\\finalScripts\\releasein.sql");
		PrintWriter out2=new PrintWriter(fi2);
		
         
       	FileReader f = new FileReader("G:\\Documents and Settings\\cristiano\\Desktop\\tesina seminario\\tesina\\file IMDB\\release-dates.txt");
        BufferedReader filebuf = new BufferedReader(f); 
      
      	out1.println("INSERT IGNORE INTO country VALUES\n");
        out2.println("INSERT IGNORE INTO releasein VALUES\n");
        
      		Connection conn = null;
		ResultSet rs=null;
        
           try
           {
               String userName = "root";
               String password = "041524";
               String url = "jdbc:mysql://localhost/imdb";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               //System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
               System.err.println ("Cannot connect to database server");
           }
          
           
    	

        String nextStr;
        nextStr = filebuf.readLine();
        
			
		while (!nextStr.equals("RELEASE DATES LIST")){
		nextStr = filebuf.readLine();
        }	
		
	
		
		nextStr = filebuf.readLine();
        nextStr = filebuf.readLine();
        	
			
     	while (nextStr!=null){
     			if(nextStr.equals("--------------------------------------------")){
			break;
		}
        	
         nextStr = nextStr.replace("'","");
        	
         res=nextStr.split("\t");
        
          for (int x=1; x<res.length; x++){
         
          if (!res[x].equals("\t")){
          	if(!res[x].startsWith("(")){
          		secondo=res[x];
          	}
          
          	else {
          	
          	
          	secondo=secondo.concat(res[x]);
          	}
          	
          	
          	arr=secondo.split(":");
          }
        
      	 }
       	
      
       	
        primo=res[0];
        	if (primo.contains("(")){
        	int chiusa=primo.indexOf(")");
 	  		int indice = primo.indexOf("(");
 	  		
 	  		year=primo.substring(indice+1,chiusa);
 	  		
 	  		primo = primo.substring(0,indice);
 	  		}
 	  	
 	  	else {
 	  		year=null;
 	  	}
 	  		
 	  	/*
        System.out.println("***************");
        System.out.println(primo);
      	System.out.println(secondo);
      	System.out.println(arr[0]);
      	System.out.println(arr[1]);
      	*/	
      	
      	try{
     	  		
				Statement s = conn.createStatement();
  				
  				rs=s.executeQuery("SELECT id FROM movies WHERE title='"+primo+"' and year='"+year+"'");
         				
  			 	rs.last();
  			 	
  			 	
  			    movieid =(Integer)rs.getObject(1);
  			 	}
  			 	
  			 	catch(Exception e){
  			 		
  			 	}
      	
    			out1.append("('"+arr[0]+"'),\n");
  			 	out2.append("('"+movieid+"','"+arr[0]+"','"+arr[1]+"'),\n");
              
   				
    
       
       
       nextStr = filebuf.readLine();// legge una riga del file 
    
           }
           
       
       filebuf.close();  // chiude il file    
        out1.close();   
        out2.close();   
       
       }
		
		
		
	}
