我正在为这家公司制作一个控制台应用程序,以展示我在5周的课程中学到了什么。它是一个控制台应用程序,在MySQL数据库中使用crud操作。它是一个基本的电子游戏存储。
我有两层。陈述和逻辑。我的问题是,我似乎无法使我的观点方法发挥作用。首先,它只从我的表中查看一行,然后我添加了一个for循环,现在它没有显示任何内容。
这里是我的演示层:
public static void ViewAll() {
List<Games> gamelist = new ArrayList<Games>();
Logic aref = new Logic();
aref.ViewAllGames();
for(Games g : gamelist){
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());
}
}这是我的逻辑层:
public static List<Games> ViewAllGames() {
List<Games> game = new ArrayList<Games>();
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM games");
while(rs.next()){
Games g = new Games();
for(Games gamelist : game){
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"));
game.add(g);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return game;
}如能提供任何帮助,我们将不胜感激。
编辑:所以我让它打印多行刚才它打印了最后一行。以下是研究结果:
游戏名称: 10标题:黄金眼007评级:M平台:任天堂64开发者: RockStar
游戏名称: 10标题:黄金眼007评级:M平台:任天堂64开发者: RockStar
游戏名称: 10标题:黄金眼007评级:M平台:任天堂64开发者: RockStar
游戏名称: 10标题:黄金眼007评级:M平台:任天堂64开发者: RockStar
游戏名称: 10标题:黄金眼007评级:M平台:任天堂64开发者: RockStar
发布于 2013-10-24 16:56:20
aref.ViewAllGames()返回游戏列表,但是您不会对它做任何事情。因此,for循环在空列表上循环。试试这个:
public static void ViewAll() {
Logic aref = new Logic();
List<Games> gamelist = aref.ViewAllGames();
for(Games g : gamelist){
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());
}
}回应您的编辑:
for循环在ViewAllGames()中不属于那里。您只需遍历正在执行的结果集,但只需在每次迭代的列表中添加一个新游戏:
while(rs.next()){
Games g = new Games();
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"));
game.add(g);
}发布于 2013-10-25 19:39:30
解决了!结果,我让GameId、标题、评级、平台和开发人员都是静态的,而不是公开的。对不起,如果我浪费了任何人的时间!)
https://stackoverflow.com/questions/19571080
复制相似问题