java.sql.SQLException:在org.hsqldb.jdbc.JDBCUtil.sqlException中关闭SQL语句

时间:2021-10-31 01:08:01

I am trying to insert a new row in my HSQLDB database. I have this code:

我试图在我的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();
    }

}

The program crashes at the line pst.setString(1) to set the id into the sql sentence. The stackTrace is this one :

程序在pst.setString(1)行崩溃,将id设置为sql语句。 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

I don't unserstand the problem. I tried to change the data type, but nothing seems to work. My table is this one:

我没有解决这个问题。我试图改变数据类型,但似乎没有任何工作。我的桌子是这样的:

java.sql.SQLException:在org.hsqldb.jdbc.JDBCUtil.sqlException中关闭SQL语句

1 个解决方案

#1


2  

Even though you haven't disclosed all relevant code, the problem is likely that you are re-using the same PreparedStatement object across different methods. You are probably querying the next player id in calcularIdAtleta() using pst, which will leave the statement closed when calcularIdAtleta() returns. You shouldn't do that. Instead, use method-local variables; they make your code easier to read and less error-prone.

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

Try changing your method into this instead, essentially declaring the PreparedStatement locally:

尝试将您的方法更改为此方法,基本上在本地声明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();
        }
    }    
}

#1


2  

Even though you haven't disclosed all relevant code, the problem is likely that you are re-using the same PreparedStatement object across different methods. You are probably querying the next player id in calcularIdAtleta() using pst, which will leave the statement closed when calcularIdAtleta() returns. You shouldn't do that. Instead, use method-local variables; they make your code easier to read and less error-prone.

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

Try changing your method into this instead, essentially declaring the PreparedStatement locally:

尝试将您的方法更改为此方法,基本上在本地声明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();
        }
    }    
}