繁体   English   中英

即使插入成功,“ java.sql.SQLException:常规错误”

[英]“java.sql.SQLException: General error” even if the insert worked

我正在做一个程序,即使插入指令正常工作并且DB(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); 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);

成功执行的打印语句,我打印的值在DB中。 我检查了数据库,所有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(通过插入成功但会引发此异常)仅当对象前提(presu)内部具有多个Producto时例外。

希望您能给我一个主意,因为我已经有将近一个星期的时间了,我也尝试了PreparedStatements(结果相同)。

谢谢。

您可以在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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM