import java.sql.*;

public class CommentTest
{
    public static void main(String[] args)
        throws Exception
    {
        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        String query = "SELECT\n"
            + "---\n"
            + "--- This is a comment\n"
            + "---\n"
            + "1 AS num";

        System.out.println("Running query:");
        System.out.println(query);

        try
        {
            conn = DriverManager
                .getConnection("jdbc:mysql://localhost/test");
            conn.setReadOnly(true);

            stmt = conn.createStatement();

            rs = stmt.executeQuery(query);

            if(rs.next())
            {
                System.out.println("Results:");

                ResultSetMetaData rsmd = rs.getMetaData();
                long rowCount = 0l;
                int colCount = rsmd.getColumnCount();
                for(int i=0; i<colCount; ++i)
                {
                    if(i > 0) System.out.print("\t");
                    System.out.print(rsmd.getColumnName(i + 1));
                }
                System.out.println();

                do
                {
                    for(int i=0; i<colCount; ++i)
                    {
                        if(i > 0) System.out.print("\t");
                        System.out.print(rs.getString(i + 1));
                    }
                    System.out.println();
                    ++ rowCount;
                } while(rs.next());
            }
            else
            {
                System.out.println("Query returned no results.");
            }
        }
        finally
        {
            if(null != rs) try { rs.close(); } catch(SQLException sqle)
            {
                System.err.println("Error closing ResultSet: ");
                sqle.printStackTrace();
            }
            if(null != stmt) try { stmt.close(); } catch(SQLException sqle)
            {
                System.err.println("Error closing Statement: ");
                sqle.printStackTrace();
            }
            if(null != conn) try { conn.close(); } catch(SQLException sqle)
            {
                System.err.println("Error closing Connection: ");
                sqle.printStackTrace();
            }
        }
    }
}
