Ad

Should I Be Using Break; To Exit My While Loop?

- 1 answer

I am in need of exiting my while loop, I have attempted using break; however, my next output does not align correctly. Here is my current output (without break;):

List of Gamers:
-----------------------------------------------------------------------------------------------------
Adelie, Carden, Cora, Lada, Mario, Mich, Sally, Xavier-----------------------------------------------------------------------------------------------------

The desired output should be:

List of Gamers: 
-----------------------------------------------------------------------------------------------
Adelie, Bob, Carden, Chang, Cora, Jonas, Lada, Lev, Mario, Mason, Mich, Raffi, Sally, Sergio, Xavier
-----------------------------------------------------------------------------------------------

Here is my code:

public class luis_ramirez_game_scores {

    public static void main(String[] args) {
        String connectionString = "********/game_scores?serverTimezone=UTC";
            String dbLogin = "*****";
            String dbPassword = "******";
            Connection conn = null;
            System.out.println("-----------------------------------------------------------------------------------------------------");
            System.out.println("List of Gamers:");
            System.out.println("-----------------------------------------------------------------------------------------------------");
         // The sql variable has been added to store the SQL statement
            // that we will send to MySQL. This SQL statement asks MySQL
            // for all first names from the gamers table. By default, it
            // will return them in the order in which they are stored in
            // the table (not ordered, in other words).
            String sql = "SELECT first_name FROM gamers ORDER BY first_name";
            // Here are a couple of variations you can try that sorts
            // our list of names ascending and descending
            //String sql = "SELECT first_name FROM gamers ORDER BY first_name";
            //String sql = "SELECT first_name FROM gamers ORDER BY first_name DESC";
            try
            {
              conn = DriverManager.getConnection(connectionString, dbLogin, dbPassword);
              if (conn != null)
              {
                // This nested try-catch structure was added which uses two
                // interfaces from the java.sql package: Statement, which
                // creates a statement object and a ResultSet object which
                // contains the results of the sql statement declared above,
                // and is executed in MySQL using the executeQuery method.
                try (Statement stmt = conn.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
                ResultSet rs = stmt.executeQuery(sql))
                {
                  // If the try is successful, then we should have a
                  // result set (rs) and we can use a while look to
                  // loop through the result set and print the
                  // first names of the gamers. The next() method of
                  // the rs object will return each row in the result
                  // set until there are no more rows left.
                  while (rs.next())
                  {
                    System.out.print(rs.getString("first_name"));
                    if (rs.next()) {
                    System.out.print(", ");
                  }
                } 
                }
                // If the try fails then the catch will run, which in
                // this example it captures any exceptions thrown by
                // SQL and prints those exceptions.
                catch (SQLException ex)
                {
                  System.out.println(ex.getMessage());
                }
                System.out.println("-----------------------------------------------------------------------------------------------------");
              }
            }
            catch (Exception e)
            {
            System.out.println("Database connection failed.");
            e.printStackTrace();
            }
            
          }
}

I have added break; below System.out.print(", "); but the output results the same. Any insight you can provide would be greatly appreciated.

Ad

Answer

To print dashed line on a new line, just insert \n (new line character) here:

System.out.println("\n-----------------------------------------------------------------------------------------------------");
Ad
source: stackoverflow.com
Ad