简体   繁体   中英

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

I'm doing a program and even if the insert instructions work correctly and everything in the DB (Microsoft Access) is OK, the following code throws this exception, which doesn't help at all: 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);

The printed statements where successfully executed, the values I printed are in the DB. I checked the DB and all the Id are there too.

ProductoPresupuesto has two foreign keys as primary keys and they are never repeated (I mean both at the same time).

Here you can see my code:

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();

        }


    }



}

All the items in "Producto" already exist, the client exists too. This function only tries to insert in Presupuesto (which success without exception) and then in ProductoPresupuesto.

The existing tables are Cliente (with one example), Producto (with the loaded products, already existing), Presupuesto (which does not throw any exception) and the table in between of this last two: ProductoPresupuesto (which succeeds with insertions but throws this exception ONLY if the Object Presupuesto (presu) has more than one Producto inside.

Hope you can give me an idea, because I've been like this for almost a week, trying PreparedStatements too (same results).

Thank you.

Can you highlight line 137 in AccesoBD.java.

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

This will help narrow down where the problem is coming from.

BTW: I'll advise you use PreparedStatements. That way you'll be able to set the parameters dynamically and avoid string escaping errors. Something like:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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