这是我第一次编写servlet。我通过ajax成功发布了(它给Output Service打电话了)。但是,我意识到它不会在数据库中发布任何内容。它也没有给出任何错误。
有人知道我做错了吗?
这是我的剧本:
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response createBestellung(
@FormParam("Uhrid") int uhrid,
int id
) {
Bestellung entity = new Bestellung();
ResponseBuilder responseBuilder = null;
try {
String sql = "SELECT max(id) FROM Kunde";
PreparedStatement preparedStatement = this.conn.getConnection().prepareStatement(sql);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next() == false)
throw new SQLException("No result found for id=" + id);
entity.setId(rs.getInt(2));
UhrDao uhrDao = new UhrDao(this.conn);
Uhr uhr = uhrDao.loadById(rs.getInt(1));
entity.setUhr(uhr);
KundeDao kundeDao = new KundeDao(this.conn);
Kunde kunde = kundeDao.loadById(rs.getInt(2));
entity.setKunde(kunde);
conn = DbConnection.getInstance();
BestellungDao dao = new BestellungDao(conn);
entity = dao.insert(entity);
System.out.println("Inserted entity: " + entity.toString());
responseBuilder = Response.status(Status.OK).entity(entity);
} catch (DbException | SQLException e) {
e.printStackTrace();
ServiceException se = new ServiceException("Fehler aufgetreten", e);
responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(se.toJSON());
} finally {
conn.close();
}
// return the response
return responseBuilder.build();
}
$.ajax({
url: 'rest/Bestellung',
method: 'POST',
data: $("#uhrid").serialize(),
dataType: 'JSON'
}).done(function(response) {
console.log(response);
// erfolgsfall
alert("klappt")
//
}).fail(function(error) {
console.log(error);
alert("fehler")
// fehlerfall
});
});
输出: 服务称为 但没有其他反应
我希望有人可以帮助我解决问题