【发布时间】: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类的代码?