【问题标题】:my Search method is coming up with all nulls我的搜索方法提出了所有空值
【发布时间】:2013-10-25 21:09:17
【问题描述】:

让我快速解释一下。我在 7 月份通过一家公司参加了为期 5 周的 Java 课程。它们涵盖了基本的东西,比如控制台应用程序、crud 操作、mysql 和 n 层架构。课程结束后,我并没有太多使用它,因为我回去工作了,其他医疗原因浮出水面....等等等等。

公司告诉我制作一个简单的程序来反映我所学到的东西。原来我保留的很少。

我决定制作一个视频游戏starage 程序。它将用于盯着你的视频游戏,这样你就不必搜索你的书柜(或者你如何存储你的游戏。)

这是一个使用 MYSQL 的 crud 操作的基本控制台应用程序。我无法让我的搜索功能真正发挥作用。我有 2 层表示层和逻辑层。搜索方法允许他们按标题搜索游戏。当我运行程序并使用搜索时,它只显示标题,其余为空。 这是我的表示层:

private static Games SearchForGame() 
    {
    Logic aref = new Logic();
    Games g = new Games();
    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();
    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 = new Games();
     try {
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url+dbName,userName,password);
        java.sql.PreparedStatement statement = conn.prepareStatement("SELECT GameId,Title,Rating,Platform,Developer FROM games WHERE Title=?");
        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"));
        statement.executeUpdate();

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

这也是我最后的结果

Please enter the name of the game you wish to find:
 Skyrim

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

任何帮助将不胜感激,并提前致谢


编辑:这是我的游戏类的代码

 public class Games 
{
    public int GameId;
    public String Title;
    public String Rating;
    public String Platform;
    public String Developer;

    public int getGameId() 
    {
        return GameId;
    }

    public int setGameId(int gameId) 
    {
        return GameId = gameId;
    }

    public String getTitle() 
    {
        return Title;
    }

    public String setTitle(String title) 
    {
        return Title = title;
    }

    public String getRating() 
    {
        return Rating;
    }

    public void setRating(String rating) 
    {
        Rating = rating;
    }

    public String getPlatform() 
    {
        return Platform;
    }

    public void setPlatform(String platform) 
    {
        Platform = platform;
    }

    public String getDeveloper() 
    {
        return Developer;
    }

    public void setDeveloper(String developer) 
    {
        Developer = developer;
    }

}

【问题讨论】:

  • 我会尝试调试应用程序。 rs.getInt("GameId") 会返回任何东西吗?数据库中有数据吗?
  • 是的,我的表中至少有 4 行
  • 它们包含其他字段的数据?您能否也发布Games 类的代码?

标签: java mysql search crud


【解决方案1】:

SearchGame() 中创建一个新的Games 对象,然后使用getTitle() 甚至不设置标题。在SearchForGame() 中,您确实设置了标题,但您应该将Games 对象传递给SearchGame() 并存储返回的对象。 (SearchGame() 中的gSearchForGame() 中的g 是两个不同的对象)

【讨论】:

    猜你喜欢
    • 2014-08-10
    • 2021-01-01
    • 2021-02-28
    • 2013-12-02
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多