我应该使用休息吗?退出我的 while 循环?
Should I be using break; to exit my while loop?
我需要退出 while 循环,我尝试使用 break;但是,我的下一个输出没有正确对齐。这是我当前的输出(没有中断;):
List of Gamers:
-----------------------------------------------------------------------------------------------------
Adelie, Carden, Cora, Lada, Mario, Mich, Sally, Xavier-----------------------------------------------------------------------------------------------------
所需的输出应该是:
List of Gamers:
-----------------------------------------------------------------------------------------------
Adelie, Bob, Carden, Chang, Cora, Jonas, Lada, Lev, Mario, Mason, Mich, Raffi, Sally, Sergio, Xavier
-----------------------------------------------------------------------------------------------
这是我的代码:
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();
}
}
}
我添加了中断;低于 System.out.print(", ");
但输出结果相同。如果您能提供任何见解,我们将不胜感激。
要在新行上打印虚线,只需在此处插入 \n
(换行符):
System.out.println("\n-----------------------------------------------------------------------------------------------------");
我需要退出 while 循环,我尝试使用 break;但是,我的下一个输出没有正确对齐。这是我当前的输出(没有中断;):
List of Gamers:
-----------------------------------------------------------------------------------------------------
Adelie, Carden, Cora, Lada, Mario, Mich, Sally, Xavier-----------------------------------------------------------------------------------------------------
所需的输出应该是:
List of Gamers:
-----------------------------------------------------------------------------------------------
Adelie, Bob, Carden, Chang, Cora, Jonas, Lada, Lev, Mario, Mason, Mich, Raffi, Sally, Sergio, Xavier
-----------------------------------------------------------------------------------------------
这是我的代码:
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();
}
}
}
我添加了中断;低于 System.out.print(", ");
但输出结果相同。如果您能提供任何见解,我们将不胜感激。
要在新行上打印虚线,只需在此处插入 \n
(换行符):
System.out.println("\n-----------------------------------------------------------------------------------------------------");