gpt4 book ai didi

java - resultSet.rowUpdated() 抛出 createSQLFeatureNotSupportedException

转载 作者:行者123 更新时间:2023-11-29 16:35:44 25 4
gpt4 key购买 nike

我正在尝试更新数据库中的一行。其他一切都工作正常,删除、创建、列出,但编辑一行给了我一个 createSQLFeatureNotSupportedException

@Override
public Boolean editarUsuario(Usuario usuario) throws SQLException {
try(Connection connection = MySqlConnection.abrirConexao()){
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

ResultSet resultSet = statement.executeQuery("select * from usuario where idUsuario = " + usuario.getIdUsuario());

if (resultSet.first()) {
resultSet.updateInt("IdUsuario", usuario.getIdUsuario());
resultSet.updateString("Login", usuario.getLogin());
resultSet.updateString("Senha", usuario.getSenha());
resultSet.updateBoolean("Admin",usuario.isAdmin());
resultSet.updateBoolean("Operador",usuario.isOperador());
resultSet.updateBoolean("Cliente",usuario.isCliente());
resultSet.updateRow();
return resultSet.rowUpdated();
}

}catch (SQLException e) {
e.printStackTrace();
}

return false;
}

这行 [return resultSet.rowUpdated();] 给了我这个异常。我只想更新一行。

我做错了什么?

这是我的 MySqlConnection

public class MySqlConnection {
public static String url = "jdbc:mysql://localhost:3306/webticket?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
public static String usuario = "root";
public static String senha = "123456";

public static Connection abrirConexao() throws SQLException {
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
return DriverManager.getConnection(url, usuario, senha);
}
}

这是一个异常(exception):

04-Dec-2018 09:00:36.294 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [C:\DevTools\apache-tomcat-9.0.12\webapps\manager]
04-Dec-2018 09:00:36.344 INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\DevTools\apache-tomcat-9.0.12\webapps\manager] has finished in [49] ms
java.sql.SQLFeatureNotSupportedException
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLFeatureNotSupportedException(SQLError.java:236)
at com.mysql.cj.jdbc.result.UpdatableResultSet.rowUpdated(UpdatableResultSet.java:1183)
at br.edu.fapi.webticket.usuario.dao.impl.UsuarioDAOImpl.editarUsuario(UsuarioDAOImpl.java:125)
at br.edu.fapi.webticket.usuario.dao.impl.OperadorDAOImpl.editarUsuario(OperadorDAOImpl.java:22)
at br.edu.fapi.webticket.usuario.web.OperadorEditar.doPost(OperadorEditar.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

知道如何解决吗?

最佳答案

这似乎是 MySQL Connector/J 实现中的一个错误。 ResultSet.rowUpdated() 方法仅对于 CONCUR_READ_ONLY 结果集是可选的,但显然 MySQL Connector/J 将此误解为“该方法是可选的”并简单地实现了它始终抛出 SQLFeatureNotSupportedException(请参阅 MySQL Connector/J 源代码中的 UpdatableResultSet.rowUpdated)。

我建议您提交 MySQL 的错误。

来自ResultSet.rowUpdated() apidoc (特别是注释):

Retrieves whether the current row has been updated. The value returned depends on whether or not the result set can detect updates.

Note: Support for the rowUpdated method is optional with a result set concurrency of CONCUR_READ_ONLY

Returns:
true if the current row is detected to have been visibly updated by the owner or another; false otherwise
Throws:
SQLException - if a database access error occurs or this method is called on a closed result set
SQLFeatureNotSupportedException - if the JDBC driver does not support this method

作为解决方案(或解决方法),我建议您将 return resultSet.rowUpdated(); 替换为 return true;。如果 resultSet.updateRow() 成功,您可以确定您已更新该行(尽管您可能已使用与已有的值相同的值对其进行了更新)。

尤其是 rowUpdated,根据定义,不一定会报告您认为它的作用。它的用途主要是检测滚动时并发的可见更新,而不是检测您自己的显式更新。另请参阅DatabaseMetaData.updatesAreDetected .

关于java - resultSet.rowUpdated() 抛出 createSQLFeatureNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53611616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com