ArrayList显示数据库的所有结果

时间:2017-08-03 09:34:59

标签: java mysql jdbc arraylist

我想从mysql db中获取一行。但是我的程序会打印db中的所有行。我不确定是什么问题。你能帮帮我吗?

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();
    }

}

}

1 个答案:

答案 0 :(得分:3)

您必须定义要打印的数据库表的哪一行。为此,您可以在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条件也必须设计成这样的方式。(只有一行具有这样的列值)。否则程序无法知道您的预期结果是什么

希望有所帮助:)