import java.sql.*;
/*****************************************************************************************
/ Read data from Mysql database 
/****************************************************************************************/

public class MysqlQuery
{
    public static void main(String[] args) 
    {
        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;
        ResultSet  rs      =  null;

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

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

            try
            {
                sql  = "SELECT * FROM java "  
                     + "ORDER BY id        ";

                rs = stmt.executeQuery(sql);
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
            
            System.out.println("ID \tFIRST \tLAST \tUPDATED");    //headers
            System.out.println("-- \t----- \t---- \t-------");   

            while(rs.next())
            {
                int    id      = rs.getInt("id");
                String lname   = rs.getString("lname");
                String fname   = rs.getString("fname");
                Date   upd     = rs.getDate("upd_date");

                System.out.println(id+"\t"+fname+"\t"+lname+"\t"+upd);
            }  
        } 
        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
                }
            }
        }
    }
}