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 

我在java中使用带有mysql的基本控制台应用程序我有两层。我的表示层:

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(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 回答
0

您可以通过多种方式做到这一点,将在此处解释一种。

在 SearchGame 方法中,使用isBeforeFirst()方法检查您是否有任何数据。

if(!resultSet.isBeforeFirst()){
     return null;
 }

如果SearchForGame()对象为空,则显示一条消息。

if(g != null) {
    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("No data found");
}
于 2013-10-26T02:32:50.613 回答
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 回答