2017-08-03 80 views
0

我想从mysql数据库获取一行。但我的程序打印来自数据库的所有行。我不确定什么是错的。你可以帮帮我吗?ArrayList显示来自数据库的所有结果

public class ScreenBalance { 

// show all records 
public static ArrayList<String> showOnScreenBalance() throws Exception { 
    LoginVerification.login(); 

    try { 
     Connection con = ConnectionDB.getConnection(); 
     PreparedStatement statement = con.prepareStatement("SELECT ClientID, Money FROM BankDB.Info"); 

     ResultSet result = statement.executeQuery(); 

     ArrayList<String> array = new ArrayList<String>(); 
     System.out.print("\nID\t\tAmount\t\n"); 
     while (result.next()) { 
      System.out.print(result.getString("ClientID")); 
      System.out.print("\t"); 
      System.out.print(result.getString("Money")); 
      System.out.print("\t"); 
      array.add(result.getString("Money")); 
     } 
     System.out.println(); 
     return array; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

以下是我希望选择一行的第二堂课。但它不能正常工作。我输入clientID +客户端密码,为什么我的方法'showOnScreenBalance'没有看到它?

public class LoginVerification { 

private static boolean loggedIn = false; 

    public static void login() throws Exception { 

     System.out.println("ATM Machine v 1.0, Welcome user."); 
     if(loggedIn){ 
      //do nothing, user already logged in 
      return; 
     } 

    Scanner input = new Scanner(System.in); 
    System.out.println("Please enter USER ID:"); 
    String userId = input.nextLine(); 
    System.out.println("Please enter PIN CODE:"); 
    String pass = input.nextLine(); 

    try { 
     Connection con = ConnectionDB.getConnection(); 
     String sql = "SELECT COUNT(*) FROM Info WHERE ClientID = ? AND ClientPass = ?"; 

     PreparedStatement posted = con.prepareStatement(sql); 
     posted.setString(1, userId); 
     posted.setString(2, pass); 
     ResultSet resultSet = posted.executeQuery(); 
     resultSet.next(); 
     int rowCount = resultSet.getInt(1); 

     if (rowCount == 1) { 
      loggedIn = true; 
      System.out.println("LOGIN SUCCESSFUL"); 
      Helper.showMainMenu(); 

     } else { 
      loggedIn = false; 
      System.out.println("#LOGIN UNSUCCESSFUL, WRONG LOGIN OR PASSWORD"); 

      System.out.println("\nTry again? \n1) yes \n2) no [quit]"); 
      int choice = input.nextInt(); 
      if (choice == 1) { 
       login(); 
      } else { 
       System.exit(0); 
      } 
     } 
     con.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

}

+3

您的查询选择表中的所有行。你期望发生什么? – Kayaman

+0

您的第二堂课不会在您的第一堂课中看到(本地)变数,因为您没有将它们传递给第二堂课。你可以使你的方法'showOnScreenBalance(String userid)'在where子句中用来选择正确的用户余额。 – Kayaman

+0

哇谢谢,这就是我在找:)可以请你纠正我的showOnScreenBalance方法吗?我有麻烦去自己做 – damianm

回答

2

你必须定义要打印哪个数据库表中的行。要做到这一点,你可以在你的sql语句中定义一个WHERE子句。

PreparedStatement statement = con.prepareStatement("SELECT ClientID, Money FROM BankDB.Info WHERE some_condition"); 

例如:

PreparedStatement statement = con.prepareStatement("SELECT ClientID, Money FROM BankDB.Info WHERE ClientID='some_value'"); 

对于结果的数量是1,WHERE条件也必须设计这样的方式(仅一个行与列这样的值可以在那里)。否则在那里。该程序无法知道您的预期结果。

希望它有帮助:)

相关问题