带有查询返回的JDBC事务

时间:2014-07-16 14:20:20

标签: java sql postgresql jdbc

我在JDBC中的事务有问题,我想执行插入,我需要自动生成的键值,但如果我在代码的末尾提交,则会抛出NullPointerException

我将autocommit设置为false,如果我提交单个更新,则返回将不是null。我试图创建一个保存点但是如果我提交保存点就会被破坏 谢谢!

    try {
            if (Main.conn != null) {
                if (!Main.conn.isClosed()) {
                    salvataggio=Main.conn.setSavepoint("inserisco");
                    PreparedStatement pst = null;
                    String prodotti = "";
                    if (jCBDataAutomatica.isSelected()) {
                        prodotti = "INSERT INTO prodotti (idCliente, idProdotto, numComp, rev, ins) VALUES ( (SELECT idCliente FROM Clienti WHERE Nome=?), ?, ?, ?, current_date)";
                    } else {
                        prodotti = "INSERT INTO prodotti (idCliente, idProdotto, numComp, rev,  ins) VALUES ( (SELECT idCliente FROM Clienti WHERE Nome=?), ?, ?, ?,?)";
                    }
                    pst = Main.conn.prepareStatement(prodotti);
                    pst.setString(1, cliente);
                    pst.setString(2, codprod);
                    pst.setInt(3, Integer.parseInt(jTFNumeroComponenti.getText()));

                    if (!jCBDataAutomatica.isSelected()) {
                        pst.setDate(5, new java.sql.Date(Integer.parseInt((String) jCBAnno.getSelectedItem()), Integer.parseInt((String) jCBMese.getSelectedItem()), Integer.parseInt((String) jCBGiorno.getSelectedItem())));
                        pst.setBigDecimal(4, rev);
                    } else {
                        pst.setBigDecimal(4, rev);
                    }
                    res = pst.executeUpdate();
                    Main.conn.commit();
                    if(res==0){
                        System.err.println("errore prodotti");
                    }
                    String prezzi="INSERT INTO Prezzi (data,idCliente, idProdotto, rev, Prezzo, Commento) VALUES(current_timestamp,(SELECT idCliente FROM Clienti WHERE Nome=?),?,?,?,?)";
                    pst= Main.conn.prepareStatement(prezzi,Statement.RETURN_GENERATED_KEYS);
                    pst.setString(1, cliente);
                    pst.setString(2, codprod);
                    BigDecimal prezzo=new BigDecimal(jTFPrezzoEffettivo.getText().replaceAll(",","."));
                    pst.setBigDecimal(3, rev);
                    pst.setBigDecimal(4, prezzo);
                    pst.setString(5, jTACommenti.getText());
                    res = pst.executeUpdate();
                    Main.conn.commit();
                    if(res==0){
                        System.err.println("errore prezzi");
                    }
                    Vector keyprezzi=new Vector(4);
                        ResultSet rs = pst.getGeneratedKeys();
                        if (rs != null && rs.next()) {
                            keyprezzi.add(rs.getTimestamp(1));
                            keyprezzi.add(rs.getInt(2));
                            keyprezzi.add(rs.getString(3));
                            keyprezzi.add(rs.getBigDecimal(4));
                        }
                    String lavorazioni="INSERT INTO Lavorazioni(Tempo, Descrizione, Commento) VALUES(?,?,?)";
                    pst= Main.conn.prepareStatement(lavorazioni, Statement.RETURN_GENERATED_KEYS);
                    String molti="INSERT INTO PrezziLavorazioni (idFase, data, idCliente, idProdotto, rev) VALUES(?,?,?,?,?)";
                    PreparedStatement pstmolti=Main.conn.prepareStatement(molti);
                    int righe=jTFasi.getModel().getRowCount();
                    for(int i=0; i<righe; i++){
                        if(!((String)jTFasi.getModel().getValueAt(i,1)).equals("")){
                            pst.setInt(1,Integer.parseInt((String)jTFasi.getModel().getValueAt(i,1)));
                            pst.setString(2,(String)jTFasi.getModel().getValueAt(i,0));
                            pst.setString(3,(String)jTFasi.getModel().getValueAt(i, 2));
                            res = pst.executeUpdate();
                            System.err.println(pst.getGeneratedKeys().getMetaData().getColumnCount());
                            Main.conn.commit();
                            if(res==0){
                                System.err.println("errore lavorazioni");
                            }
                            long key=-1;
                            ResultSet reslav = pst.getGeneratedKeys();
                            if (reslav != null && reslav.next()) {
                                key = reslav.getLong(1);
                            }
                            pstmolti.setLong(1,key);
                            pstmolti.setTimestamp(2,(Timestamp)keyprezzi.get(0));
                            pstmolti.setInt(3, (int)keyprezzi.get(1));
                            pstmolti.setString(4,(String)keyprezzi.get(2));
                            pstmolti.setBigDecimal(5, (BigDecimal)keyprezzi.get(3));
                            pstmolti.executeUpdate();
                            Main.conn.commit();
                            if(res==0){
                                System.err.println("errore moltimolti");
                            }
                        }
                    }
                    reset();
                } else {
                    JOptionPane.showMessageDialog(jBConnetti, "Connessione persa", "ERRORE", JOptionPane.ERROR_MESSAGE);
                    Connessione.cambiaStato(false);
                }
            } else {
                JOptionPane.showMessageDialog(jBConnetti, "Connessione persa", "ERRORE", JOptionPane.ERROR_MESSAGE);
                Connessione.cambiaStato(false);
            }
        } catch (SQLException ex) {
            System.err.println(ex);
            if (Main.conn != null) {
                try {
                    if (!Main.conn.isClosed()) {
                        Main.conn.rollback(salvataggio);
                    }else {
                        JOptionPane.showMessageDialog(jBConnetti, "Connessione persa", "ERRORE", JOptionPane.ERROR_MESSAGE);
                        Connessione.cambiaStato(false);
                    }
                } catch (SQLException ex1) {
                    Logger.getLogger(Inserisci.class.getName()).log(Level.SEVERE, null, ex1);
                }
            }else {
                JOptionPane.showMessageDialog(jBConnetti, "Connessione persa", "ERRORE", JOptionPane.ERROR_MESSAGE);
                Connessione.cambiaStato(false);
            }
            Logger.getLogger(Clienti.class.getName()).log(Level.SEVERE, null, ex);
        }

0 个答案:

没有答案
相关问题