簡體   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