テーブルを持つMySQLデータベース(データベースと呼ばれる)があります:イベント、ゲブルイカー、シチュアティ。ここで、Java で (netbeans を使用して) コードを記述し、安静な Web サービスをテストする必要があります。すべてのユーザー (=gebruikers、オランダ語) を取得することはできますが、特定の 1 人のユーザーをプライマリ キー 'Username' で検索して取得したい場合、安静な Web サービスをテストすると、この 500 エラーが発生します。助けてください!これはJavaでの私のコードです:
@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))、郵便番号 (int(4))、 gemeente (テキスト)、wachword (テキスト)。さらに情報が必要な場合は、お問い合わせください。ありがとうございます!