【问题标题】:"java.sql.SQLException: General error" even if the insert worked“java.sql.SQLException:一般错误”即使插入有效
【发布时间】:2014-04-06 16:39:29
【问题描述】:

我正在做一个程序,即使插入指令正常工作并且数据库(Microsoft Access)中的一切正常,以下代码也会引发此异常,这根本没有帮助: INSERT INTO Presupuesto (Id_cliente,ID_Presuspuesto,Reserva) VALUES (1,234, false) INSERT INTO Presupuesto (Id_cliente,ID_Presuspuesto,Reserva) VALUES (1,234, false) java.sql.SQLException: General error at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source) at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source) at datos.AccesoBD.insertaPresupuesto(AccesoBD.java:137) at aplicaciones.CapaAplicacion.insertaPresupuesto(CapaAplicacion.java:81) at Interfaces.NuevoPresupuesto$2.actionPerformed(NuevoPresupuesto.java:103) 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$200(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$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.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$1.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) hasta aqui bien INSERT INTO ProductoPresupuesto (Id_Producto,PrecioUd,ID_Presuspuesto,Unidades) VALUES (1,123.0,234,1); hasta aqui bien INSERT INTO ProductoPresupuesto (Id_Producto,PrecioUd,ID_Presuspuesto,Unidades) VALUES (2,270.0,234,1);

成功执行的打印语句,我打印的值在数据库中。我检查了数据库,所有的 Id 也都在那里。

ProductoPresupuesto 有两个外键作为主键,它们从不重复(我的意思是同时)。

在这里你可以看到我的代码:

public void insertaPresupuesto(Presupuesto presu){

    String insert;

    for(int i=0; i<presu.getL().size(); i++){

        insert="INSERT INTO Presupuesto (Id_cliente,ID_Presuspuesto,Reserva) VALUES ("+presu.getC().getID_Cliente()+","+presu.getId()+", "+presu.isReserva()+")";
        Statement stm1;
        try {
            stm1 = conn.createStatement();
            System.out.printf("%s\n",insert);
            stm1.executeUpdate(insert);//THIS IS THE LINE 137



        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }


    }

    for(int i=0; i<presu.getL().size(); i++){
        float precio=presu.getL().get(i).getPrecioProd()-presu.getL().get(i).getRebaja();
        System.out.printf("hasta aqui bien\n");
        insert="INSERT INTO ProductoPresupuesto (Id_Producto,PrecioUd,ID_Presuspuesto,Unidades)  VALUES ("+presu.getL().get(i).getId()+","+precio+","+presu.getId()+","+presu.getUnidades().get(i)+");";

        Statement stm2;
        try {
            stm2 = conn.createStatement();
            System.out.printf("%s\n",insert);
            stm2.executeUpdate(insert);




        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }


    }



}

“Producto”中的所有项目都已经存在,客户端也存在。此函数仅尝试插入 Presupuesto(成功无一例外),然后插入 ProductoPresupuesto。

现有的表是 Cliente(有一个示例)、Producto(带有已加载的产品,已经存在)、Presupuesto(不会引发任何异常)以及最后两个之间的表:ProductoPresupuesto(插入成功)但仅当 Object Presupuesto (presu) 内部包含多个 Producto 时才会引发此异常。

希望你能给我一个想法,因为我已经这样快一周了,也在尝试 PreparedStatements(同样的结果)。

谢谢。

【问题讨论】:

    标签: java sql ms-access odbc sqlexception


    【解决方案1】:

    您能否突出显示 AccesoBD.java 中的第 137 行。

    datos.AccesoBD.insertaPresupuesto(AccesoBD.java:137)

    这将有助于缩小问题的根源。

    顺便说一句:我建议您使用 PreparedStatements。这样您就可以动态设置参数并避免字符串转义错误。类似的东西:

    PreparedStatement ps = con.prepareStatement("INSERT INTO ProductoPresupuesto (Id_Producto,PrecioUd,ID_Presuspuesto,Unidades)  VALUES (?,?,?));
    ps.setString(1, param1);
    ps.setString(2, param2);
    ps.setString(3, param3);
    

    【讨论】:

    • stm1.executeUpdate(插入);是第 137 行。抱歉,我没有找到任何突出显示内部代码的选项。 @SUNNYben
    猜你喜欢
    • 1970-01-01
    • 2011-06-02
    • 2011-12-02
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2015-11-28
    相关资源
    最近更新 更多