2013-04-24 14 views
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(文本)。 如果需要更多信息,請詢問。 謝謝大家!

回答

0

你需要在你的語句WHERE條款,如:

"SELECT * FROM gebruiker WHERE username =" + username; 
相關問題