import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

class dvdjdbc
{
   public static void main(String args[])
   {
      try
      {
         Class.forName("com.mysql.jdbc.Driver").newInstance();
         Connection conn = DriverManager.getConnection(
               "jdbc:mysql://localhost/dvdcat?user=j2ee&password=stivell");
         java.sql.Statement stmt = conn.createStatement();

         try
         {
            String query = "INSERT INTO ";
            query += "dvdtitle (title, year, length, rating, url, stars)";
            query += " VALUES ('";

            for (int i = 0; i < args.length; i++)
            {
               System.out.println(i + ": " + args[i]);

               switch (i)
               {
                  case 3 :
                     if (args[i].compareTo("G") == 0)
                        query += "1";
                     else if (args[i].compareTo("PG") == 0)
                        query += "2";
                     else if (args[i].compareTo("PG-13") == 0)
                        query += "3";
                     else if (args[i].compareTo("R") == 0)
                        query += "4";
                     else if (args[i].compareTo("M") == 0)
                        query += "5";
                     else if (args[i].compareTo("X") == 0)
                        query += "6";
                     else if (args[i].compareTo("0") == 0)
                        query += "0";
                     else if (args[i].compareTo("1") == 0)
                        query += "1";
                     else if (args[i].compareTo("2") == 0)
                        query += "2";
                     else if (args[i].compareTo("3") == 0)
                        query += "3";
                     else if (args[i].compareTo("4") == 0)
                        query += "4";
                     else if (args[i].compareTo("5") == 0)
                        query += "5";
                     else if (args[i].compareTo("6") == 0)
                        query += "6";
                     else
                        query += "0";     // "(NR)" or not rated
                     break;

                  case 0 :
                  case 1 :
                  case 2 :
                  case 4 :
                  case 5 :
                     query += args[i];
                     break;
               }


               if (i+1 < args.length)
                  query += "', '";
            }

            query += "')";
            System.out.println(query);
            stmt.executeUpdate(query);
         }

         catch (SQLException ex)
         {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
         }
      }

      catch (Exception ex)
      {
         System.out.println("Oh, crap!");
         //System.out.println(ex.getMessage());
         ex.printStackTrace();
         System.out.println("Remember: mandatory fields include:");
         System.out.println("title, year, length, rating, url, stars");
      }
   }
}
