import java.sql.*;
/*****************************************************************************************
/ Insert/Update data in a Mysql database 
/****************************************************************************************/

public class MysqlUpdate
{
    public static void main(String[] args) 
    {
        if (args.length < 2)                            //if no cmd line input
        {                                       
            System.out.println("Enter firstname lastname on command line");
            System.exit(-1);
        }

        String     host    = "jdbc:mysql://localhost/";                 //from localhost 
//      String     host    = "jdbc:mysql://storm.cis.fordham.edu/";     //from remote PC 
        String     db      = "demo2"; 
        String     user    = "demo2"; 
        String     pswd    = "demo2"; 
        Connection connect =  null;
        Statement  stmt    =  null;
        String     sql     =  null;
        String     fname   =  args[0];
        String     lname   =  args[1];

        try 
        {
            Class.forName("com.mysql.cj.jdbc.Driver");   //dynamically load  
                                                         //the JDBC Driver class
            String host_db = host + db;
            connect = DriverManager.getConnection(host_db, user, pswd);

            stmt = connect.createStatement();           //create a statement object

            sql  = "INSERT INTO java " 
                 + "VALUES(0,'"+lname+"','"+fname+"',CURRENT_TIMESTAMP)";

            try
            {
                stmt.executeUpdate(sql);
            
                System.out.println("Update Successful");
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
        } 
        catch (Exception e) 
        {
            System.out.println(e);
            e.printStackTrace();
        } 
        finally 
        {
            if (stmt != null) 
            {
                try 
                {
                    stmt.close();
                } 
                catch (SQLException e) 
                {                            // let JVM handle it
                } 
            }
            if (connect != null) 
            {
                try 
                {
                    connect.close();
                } 
                catch (SQLException e) 
                {                            // let JVM handle it
                }
            }
        }
    }
}