我试图删除从数据库中单击的某些行。每行都会显示,并有一个删除按钮来删除该行。 Onclick我从行中获取ID,这一切都有效。就在我查看网络时,它返回500错误。这是我的代码
$("body").on('click', '#delete', function(){
var trid = $(this).closest('tr').attr('id');
console.log(trid);
$.ajax({
url: '/firstapp/restservices/countries/delete/' + trid,
type: 'DELETE',
success: function(response) {
console.log(response);
}
});
});
和Java
@Path("countries")
public class WorldResource {
private CountryPostgresDaoImpl CountryPostgresDao = new
CountryPostgresDaoImpl();
@Path("delete/{code}")
@DELETE
@Produces("application/json")
public Response deleteCountry(@PathParam("code") String code) throws SQLException {
Country country = ServiceProvider.getWorldService().getCountryByCode(code);
if(!CountryPostgresDao.Delete(country)) {
return Response.status(404).build();
}
return Response.ok().build();
}
返回
Request URL: http://localhost:8888/firstapp/restservices/countries/delete/AF
Request Method: DELETE
Status Code: 500
Remote Address: [::1]:8888
Referrer Policy: no-referrer-when-downgrade
在服务器上返回一个巨大的错误,我不确定在哪里看
六月12,20188 1:50:48 P.M. org.apache.catalina.core.StandardWrapperValve调用 严重:servlet [Jersey REST Service]的Servlet.service()在路径[/ firstapp]的上下文中引发异常[java.lang.IndexOutOfBoundsException:索引0越界长度为0]具有根本原因 java.lang.IndexOutOfBoundsException:索引0超出范围,长度为0 at java.base / jdk.internal.util.Preconditions.outOfBounds(Unknown Source) at java.base / jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source) 在java.base / jdk.internal.util.Preconditions.checkIndex(未知来源) at java.base / java.util.Objects.checkIndex(Unknown Source) 在java.base / java.util.ArrayList.get(未知来源) at nl.hu.v1wac.firstapp.persistence.CountryPostgresDaoImpl.findByCode(CountryPostgresDaoImpl.java:74) at nl.hu.v1wac.firstapp.webservices.WorldService.getCountryByCode(WorldService.java:52) 在nl.hu.v1wac.firstapp.webservices.WorldResource.deleteCountry(WorldResource.java:103) at java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(未知来源) at java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在java.base / java.lang.reflect.Method.invoke(未知来源) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda $ static $ 0(ResourceMethodInvocationHandlerFactory.java:76) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher $ 1.run(AbstractJavaResourceMethodDispatcher.java:148) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191) at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider $ ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:200) 在org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103) 在org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493) 在org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415) 在org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104) 在org.glassfish.jersey.server.ServerRuntime $ 1.run(ServerRuntime.java:277) 在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:272) 在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:268) 在org.glassfish.jersey.internal.Errors.process(Errors.java:316) 在org.glassfish.jersey.internal.Errors.process(Errors.java:298) 在org.glassfish.jersey.internal.Errors.process(Errors.java:268) 在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289) 在org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256) 在org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703) 在org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416) 在org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370) 在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389) 在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342) 在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) 在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407) 在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:754) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1376) 在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.base / java.lang.Thread.run(未知来源)
世界服务代码如下
public Country getCountryByCode(String code) throws SQLException {
return CountryPostgresDao.findByCode(code);
}
这是指Dao findbycode:
public Country findByCode(String cd) throws SQLException {
return findCountries("SELECT code, iso3, name, continent, region, surfacearea, population, latitude, longitude, capital, governmentform FROM country WHERE code = " + cd).get(0);
}
使用findcountries方法
public List<Country> findCountries(String query) throws SQLException{
List<Country> results = new ArrayList<Country>();
try (Connection con = super.getConnection()) {
Statement stmt = con.createStatement();
ResultSet dbResultSet = stmt.executeQuery(query);
while (dbResultSet.next()) {
String code = dbResultSet.getString("code");
String iso3 = dbResultSet.getString("iso3");
String name = dbResultSet.getString("name");
String capital = dbResultSet.getString("capital");
String continent = dbResultSet.getString("continent");
String region = dbResultSet.getString("region");
Double surfacearea = dbResultSet.getDouble("surfacearea");
int population = dbResultSet.getInt("population");
Double latitude = dbResultSet.getDouble("latitude");
Double longitude = dbResultSet.getDouble("longitude");
String governmentform = dbResultSet.getString("governmentform");
results.add(new Country(code,iso3,name,capital,continent,region,surfacearea,population,governmentform,latitude,longitude));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return results;
}
删除DAO
public boolean Delete(Country country) throws SQLException {
boolean result = false;
boolean countryExists = findByCode(country.getCode()) != null;
if (countryExists) {
String query = "DELETE FROM country WHERE code = " + country.getCode();
try (Connection con = super.getConnection()) {
Statement stmt = con.createStatement();
if (stmt.executeUpdate(query) == 1) { // 1 row updated!
result = true;
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
return result;
}
答案 0 :(得分:0)
将此findByCode代码更改为
public Country findByCode(String cd) throws SQLException {
List<Country> countries= findCountries("SELECT code, iso3, name,
continent, region, surfacearea, population,
latitude, longitude, capital, governmentform FROM country WHERE code =
'" + code + "'");
}
if(!countries.isEmpty()) {
return countries.get(0);
}
return null;
}
并且查询错误,应该是
"SELECT code, iso3, name, continent, region, surfacearea, population,
latitude, longitude, capital, governmentform FROM country WHERE code =
'" + code + "'"