0
我有一个MySQL数据库(称为数据库)与表:event,gebruiker和situatie。现在我需要用Java编写代码(使用NetBeans)来测试我的宁静Web服务。我可以检索所有用户(= gebruikers,荷兰语),但是当我想通过搜索主键“用户名”来检索一个特定用户时,在测试宁静的web服务时会出现这个500错误。请帮忙! 这是我在Java代码:从java中获取来自mysql的1个特定用户的请求
@Stateless
@Path("gebruikers")
public class GebruikerService {
@Resource(name = "jdbc/socialebuurt")
private DataSource source;
/*
* Request all users.
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Gebruiker> getGebruikers() {
try (Connection conn = source.getConnection()) {
try (PreparedStatement stat = conn.prepareStatement("SELECT * FROM gebruiker")) {
try (ResultSet rs = stat.executeQuery()) {
List<Gebruiker> results = new ArrayList<>();
while (rs.next()) {
Gebruiker g = new Gebruiker();
g.setUsername(rs.getString("Username"));
g.setNaam(rs.getString("Naam"));
g.setVoornaam(rs.getString("Voornaam"));
g.setStraat(rs.getString("Straat"));
g.setHuisNr(rs.getInt("huisnr"));
g.setPostcode(rs.getInt("postcode"));
g.setGemeente(rs.getString("gemeente"));
g.setWachtwoord(rs.getString("wachtwoord"));
results.add(g);
}
return results;
}
}
} catch (SQLException ex) {
throw new WebApplicationException(ex);
}
}
/*
* Request one specific user.
*/
@Path("{username}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Gebruiker getGebruiker(@PathParam("username") String username) {
try (Connection conn = source.getConnection()) {
try (PreparedStatement stat = conn.prepareStatement("SELECT * FROM gebruiker WHERE username = ?")) {
stat.setString(1,"");
try (ResultSet rs = stat.executeQuery()) {
if (rs.next()) {
Gebruiker ge = new Gebruiker();
ge.setUsername(rs.getString("Username"));
ge.setNaam(rs.getString("Naam"));
ge.setVoornaam(rs.getString("Voornaam"));
ge.setStraat(rs.getString("Straat"));
ge.setHuisNr(rs.getInt("huisnr"));
ge.setPostcode(rs.getInt("postcode"));
ge.setGemeente(rs.getString("gemeente"));
ge.setWachtwoord(rs.getString("wachtwoord"));
return ge;
} else {
throw new WebApplicationException(Status.NOT_FOUND);
}
}
}
} catch (SQLException ex) {
throw new WebApplicationException(ex);
}
}
}
表 'gebruiker' 具有以下属性:用户名(主键,VARCHAR(26)),NAAM(文本),voornaam(文本),STRAAT(文本),huisNr (int(11)),postcode(int(4)),gemeente(文本),wachtwoord(文本)。 如果需要更多信息,请询问。 谢谢大家!