“java.sql.SQLException:一般错误”即使插入工作

时间:2014-04-06 16:39:30

标签: java sql ms-access odbc sqlexception

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

成功执行的打印语句,我打印的值在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();

        }


    }



}

&#34; Producto&#34;中的所有项目已经存在,客户也存在。此函数仅尝试在Presupuesto中插入(成功无例外),然后在ProductoPresupuesto中插入。

现有的表是Cliente(有一个示例),Producto(已加载的产品,已存在),Presupuesto(不抛出任何异常)和最后两个之间的表:ProductoPresupuesto(成功插入)但是,如果Object Presupuesto(presu)里面有多个Producto,则抛出此异常。

希望你能给我一个想法,因为我已经这样做了差不多一个星期,也尝试过PreparedStatements(结果相同)。

谢谢。

1 个答案:

答案 0 :(得分:0)

可以在AccesoBD.java中突出显示第137行。

  

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

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

BTW:我建议您使用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);