尝试通过 JAVA 500 错误删除数据库行
Trying to Delete database row through JAVA 500 error
我正在尝试删除从数据库中单击的某些行。每行都显示并有一个删除按钮来删除该特定行。单击我从行中获取 ID,所有这些都有效。就在我查看网络时,它 returns 出现 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
在服务器上 returns 一个巨大的错误,我不知道去哪里找
六月。 2018 年 12 月 1:50:48 P.M。 org.apache.catalina.core.StandardWrapperValve 调用
严重:Servlet.service() for servlet [Jersey REST Service] 在路径 [/firstapp] 的上下文中抛出异常 [java.lang.IndexOutOfBoundsException:索引 0 超出长度 0] 的根本原因
java.lang.IndexOutOfBoundsException:索引 0 超出长度 0 的范围
在 java.base/jdk.internal.util.Preconditions.outOfBounds(来源不明)
在 java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(未知来源)
在 java.base/jdk.internal.util.Preconditions.checkIndex(来源不明)
在 java.base/java.util.Objects.checkIndex(来源不明)
在 java.base/java.util.ArrayList.get(来源不明)
在 nl.hu.v1wac.firstapp.persistence.CountryPostgresDaoImpl.findByCode(CountryPostgresDaoImpl.java:74)
在 nl.hu.v1wac.firstapp.webservices.WorldService.getCountryByCode(WorldService.java:52)
在 nl.hu.v1wac.firstapp.webservices.WorldResource.deleteCountry(WorldResource.java:103)
在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(未知来源)
在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(来源不明)
在 java.base/java.lang.reflect.Method.invoke(来源不明)
在 org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
在 org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148)
在 org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191)
在 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)
在 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)
在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(来源不明)
在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(来源不明)
在 org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
在 java.base/java.lang.Thread.run(未知来源)
worldservice代码如下
public Country getCountryByCode(String code) throws SQLException {
return CountryPostgresDao.findByCode(code);
}
指的是道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;
}
将此 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 + "'"
我正在尝试删除从数据库中单击的某些行。每行都显示并有一个删除按钮来删除该特定行。单击我从行中获取 ID,所有这些都有效。就在我查看网络时,它 returns 出现 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
在服务器上 returns 一个巨大的错误,我不知道去哪里找
六月。 2018 年 12 月 1:50:48 P.M。 org.apache.catalina.core.StandardWrapperValve 调用 严重:Servlet.service() for servlet [Jersey REST Service] 在路径 [/firstapp] 的上下文中抛出异常 [java.lang.IndexOutOfBoundsException:索引 0 超出长度 0] 的根本原因 java.lang.IndexOutOfBoundsException:索引 0 超出长度 0 的范围 在 java.base/jdk.internal.util.Preconditions.outOfBounds(来源不明) 在 java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(未知来源) 在 java.base/jdk.internal.util.Preconditions.checkIndex(来源不明) 在 java.base/java.util.Objects.checkIndex(来源不明) 在 java.base/java.util.ArrayList.get(来源不明) 在 nl.hu.v1wac.firstapp.persistence.CountryPostgresDaoImpl.findByCode(CountryPostgresDaoImpl.java:74) 在 nl.hu.v1wac.firstapp.webservices.WorldService.getCountryByCode(WorldService.java:52) 在 nl.hu.v1wac.firstapp.webservices.WorldResource.deleteCountry(WorldResource.java:103) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(来源不明) 在 java.base/java.lang.reflect.Method.invoke(来源不明) 在 org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76) 在 org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148) 在 org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191) 在 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) 在 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) 在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(来源不明) 在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(来源不明) 在 org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 在 java.base/java.lang.Thread.run(未知来源)
worldservice代码如下
public Country getCountryByCode(String code) throws SQLException {
return CountryPostgresDao.findByCode(code);
}
指的是道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;
}
将此 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 + "'"