gpt4 book ai didi

java.sql.SQLException : SQL sentence is closed at org. hsqldb.jdbc.JDBCUtil.sqlException

转载 作者:行者123 更新时间:2023-12-02 04:09:00 27 4
gpt4 key购买 nike

我正在尝试在 HSQLDB 数据库中插入新行。我有这个代码:

private final String SQL_CREAR_ATLETA=" INSERT INTO ATLETA (ID_ATLETA, DNI, NOMBRE, APELLIDOS, GENERO, NACIMIENTO, CORREO, TELEFONO, CODIGOFEDERACION) VALUES (?,?,?,?,?,?,?,?,?) ";

@Override
public void crearAtleta(String dni, String nombre, String apellidos, char genero, Date nacimiento, String correo,
String telefono, String codigoFederacion) throws SQLException {

try {
con = Jdbc.getConnection();
pst = con.prepareStatement(SQL_CREAR_ATLETA);

Integer idAtleta = calcularIdAtleta(); /*this is not null , it gives the next id , for example if there are 3 athlete in the DB , will return a 4*/

pst.setString(1, String.valueOf(idAtleta)); // <- the code crash here
pst.setString(2, nombre);
pst.setString(3, apellidos);
pst.setString(4, String.valueOf(genero));
pst.setDate(5, new java.sql.Date(nacimiento.getTime()));
pst.setString(6, dni);
pst.setString(7, correo);
pst.setString(8, telefono);
pst.setString(9, codigoFederacion);
pst.execute();

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

}

程序在将 id 设置为 sql 语句的 pst.setString(1) 行处崩溃。 stackTrace 是这个:

java.sql.SQLException: SQL sentence is closed
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatementBase.checkClosed(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.checkSetParameterIndex(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setParameter(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setString(Unknown Source)
at reestructurado.persistencia.impl.AtletasGatewayImpl.crearAtleta(AtletasGatewayImpl.java:50)
at ui.UInscripcion$1.actionPerformed(UInscripcion.java:316)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.hsqldb.HsqlException: sentencia SQL está cerrada
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
... 43 more

我不明白这个问题。我尝试更改数据类型,但似乎没有任何效果。我的 table 是这样的:

enter image description here

最佳答案

即使您没有披露所有相关代码,问题也可能是您在不同的方法中重复使用相同的 PreparedStatement 对象。您可能正在使用 pstcalularIdAtleta() 中查询下一个玩家 ID,这将使该语句在 calularIdAtleta() 返回时关闭。你不应该这样做。相反,使用方法局部变量;它们使您的代码更易于阅读并且不易出错。

尝试将您的方法更改为此,本质上是在本地声明 PreparedStatement:

public void crearAtleta(String dni, String nombre, 
String apellidos, char genero,
Date nacimiento, String correo,
String telefono, String codigoFederacion) throws SQLException {

PreparedStatement ps = null;

try {
ps = Jdbc.getConnection().prepareStatement(SQL_CREAR_ATLETA);

Integer idAtleta = calcularIdAtleta();

ps.setString(1, String.valueOf(idAtleta));
ps.setString(2, nombre);
ps.setString(3, apellidos);
ps.setString(4, String.valueOf(genero));
ps.setDate(5, new java.sql.Date(nacimiento.getTime()));
ps.setString(6, dni);
ps.setString(7, correo);
ps.setString(8, telefono);
ps.setString(9, codigoFederacion);
ps.execute();

} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
}
}
}

关于java.sql.SQLException : SQL sentence is closed at org. hsqldb.jdbc.JDBCUtil.sqlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33977810/

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