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

Posted

技术标签:

【中文标题】java.sql.SQLException: SQL 语句在 org.hsqldb.jdbc.JDBCUtil.sqlException 处关闭【英文标题】:java.sql.SQLException: SQL sentence is closed at org.hsqldb.jdbc.JDBCUtil.sqlException 【发布时间】:2015-11-28 23:52:48 【问题描述】:

我正在尝试在我的 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();
    


程序在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

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

【问题讨论】:

你能告诉我们堆栈跟踪吗? 首先,我认为您应该阅读how to ask a good question 并发布MCVE 以获得帮助。其次,您应该将表架构/数据直接包含在您的帖子中,作为文本,而不是作为外部站点中的图片。第三,您需要解释您的实际错误是什么以及您的预期。 @Reimeus 我认为你是绝对正确的!罗伯托,请告诉我们这个方法calcularIdAtleta() 做了什么! 但是 String.valueOf(null) 应该返回 "null" 并且根据表定义 (varchar(255)) 它是一个有效值... 我不知道你为什么要关注某事是否是null。异常消息>>说calcularIdAtleta 方法中的连接正在关闭。 【参考方案1】:

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

尝试将您的方法改为此方法,本质上在本地声明 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 语句在 org.hsqldb.jdbc.JDBCUtil.sqlException 处关闭的主要内容,如果未能解决你的问题,请参考以下文章

java.sql.SQLException:getShort() 的值无效 - ''

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

Oracle DB:java.sql.SQLException:关闭连接

Cause: java.sql.SQLException: 请求的转换无效 ; uncategorized SQLException for SQL []; SQL state [99999]; er

java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 发生错误

Java:Sqlexception:位置不支持的 SQL92 令牌:178