错误的值我无法弄清楚如何告诉用户有当他们去寻找一个标题为“没有发现这样的标题”。当我测试它,并键入从数据库中标题它显示了正确的信息:试图告诉用户他们进入在Java
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;
}