java - 试图告诉用户他们在 java 中输入了错误的值

标签 java mysql crud

我不知道如何告诉用户当他们去搜索标题时“没有找到这样的标题”。当我测试它并从数据库中输入标题时,它会显示正确的信息:

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

最佳答案

使用 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;

关于java - 试图告诉用户他们在 java 中输入了错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19602137/

相关文章:

postgresql - 如何在 PostgreSQL 数据库中创建内容

java - Android:如何确定 HTTP PUT 请求是否已完成处理

java - Hibernate 中的一对一共享主键映射可以是可选/空吗?

sql - 在 MySQL 的 `IN ()` 中使用逗号分隔的字符串

php - ajax 保存在数据库中,无需外键

mysql - 通过 Rails 调用存储过程?

java - 速度串函数

java - map 的 map ,如何更新内部 map Java 8的 key

python - Django URL、模板、模型和 View 的命名约定

grails - grails通过单选按钮设置bean值