查询情况1:查询所有的游戏信息(此处省略代码...)
查询情况2:模糊查询(以下代码)
代码说明:以下代码为,实现查询情况2
public List<Game> Query(Game g) {
List<Game> list = new ArrayList<Game>();
String sql = " SELECT * FROM game where 1=1 ";
if(g.getGame_name() != null){//按照游戏名称查询
sql+=" AND `game_name` LIKE '%"+g.getGame_name()+"%' ";
}
if(g.getGame_type()!=null ){//按照游戏类别查询
sql+=" AND `game_type` LIKE '%"+g.getGame_type()+"%' ";
}
if(g.getGame_company() != null){//按照发行公司查询
sql+=" AND `game_company` LIKE '%"+g.getGame_company()+"%' ";
}
if(g.getGame_year()!=0 ){//按照发行年份查询
sql+=" AND `game_year` LIKE '%"+g.getGame_year()+"%' ";
}
ResultSet rs = this.executeQuery(sql);
try {
while (rs.next()) {
Game game = new Game();
game.setGame_id(rs.getInt("game_id"));
game.setGame_name(rs.getString("game_name"));
game.setGame_type(rs.getString("game_type"));
game.setGame_company(rs.getString("game_company"));
game.setGame_year(rs.getInt("game_year"));
list.add(game);
}
} catch (SQLException e) {
return null;
}
return list;
}