0

ユーザーがタイトルを検索するときに「そのようなタイトルは見つかりませんでした」とユーザーに伝える方法がわかりません。テストしてデータベースからタイトルを入力すると、正しい情報が表示されます。

Game Id:   2
Title:     Goldeneye 007
Rating:    T
Platform:  Nintendo 64
Developer: RockStar

しかし、ランダムな情報を入力すると、出力は次のようになります。

Game Id:   0
Title:     asdsdfdfg
Rating:    null
Platform:  null
Developer: null 

私は mysql を使用して Java で基本的なコンソール アプリケーションを使用しています。2 つのレイヤーがあります。私のプレゼンテーション層:

private static Games SearchForGame() {
        Logic aref = new Logic();
        Games g = new Games();
        @SuppressWarnings("resource")
        Scanner scanline = new Scanner(System.in);
        System.out.println("Please enter the name of the game you wish to find:");
        g.setTitle(scanline.nextLine());
        aref.SearchGame(g);

            System.out.println();
            System.out.println("Game Id:   " + g.getGameId());
            System.out.println("Title:     " + g.getTitle());
            System.out.println("Rating:    " + g.getRating());
            System.out.println("Platform:  " + g.getPlatform());
            System.out.println("Developer: " + g.getDeveloper());

        return g;


    }

およびロジック層

public Games SearchGame(Games g) {

         try {
            Class.forName(driver).newInstance();
            Connection conn = DriverManager.getConnection(url+dbName,userName,password);
            String sql = "SELECT GameId,Title,Rating,Platform,Developer FROM games WHERE Title=?";
            java.sql.PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, g.getTitle());
            ResultSet rs = statement.executeQuery();

            while(rs.next()){

            g.setGameId(rs.getInt("GameId"));        
            g.setTitle(rs.getString("Title"));
            g.setRating(rs.getString("Rating"));
            g.setPlatform(rs.getString("Platform"));
            g.setDeveloper(rs.getString("Developer"));
             }
             } catch (Exception e) {
             e.printStackTrace();
             }
             return g;
    }
4

3 に答える 3

0

ifステートメントを使用しますか?

if(g.getRating() != null /*or g.getGameId() == 0 or many other things*/) {
    System.out.println();
    System.out.println("Game Id:   " + g.getGameId());
    System.out.println("Title:     " + g.getTitle());
    System.out.println("Rating:    " + g.getRating());
    System.out.println("Platform:  " + g.getPlatform());
    System.out.println("Developer: " + g.getDeveloper());
} else {
    System.out.println();
    System.out.println("No such title found");
    //throw some sort of exception (and plan to catch it) so that you
    //can get out of this method without returning g full of null values
}

return g;
于 2013-10-26T02:29:34.980 に答える
0

null のチェックは、フロー制御の悪い形です。代わりにブール値の結果を考慮する必要があります。

if(aref.SearchGame(g)) {
   System.out.println();
   System.out.println("Game Id:   " + g.getGameId());
   . . . 
else {
   System.out.println("No such title found"); 
}

次に、ロジックで次のようにします。

public boolean SearchGame(Games g) {

   boolean found = false;
   try {

     [your sql select here]

     if (rs.next()) {

        [access result set]

        found = true;
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   return found;
}

しかし、さらに良い方法は、次のように Game インスタンスのリストを返し、そのリストが空かどうかを確認することです。

List<Game> SearchGames(String title)

これは優れた堅牢な API であり、次のように使用できます。

List<Game> games = aref.SearchGames(title);
if(games.size() > 0) {
   Game g = games.get(0);     
   System.out.println();
   System.out.println("Game Id:   " + g.getGameId());
   . . . 
else {
   System.out.println("No such title found"); 
}

これにより、必要に応じて、類似したタイトルの複数のゲームを見つけることもできます.

于 2013-10-26T03:14:00.233 に答える