java - 有时数据不会插入到表中

标签 java sql jdbc struts oracle11g

我有一个表 A。我通过用户界面将数据插入表 A。表 A 有一个使用序列生成的 ID(主键)和其他 16 列。其中一列称为 cntrct_no。

当我尝试通过 UI 将数据插入表中时,第一次工作正常。我检查了表A,所有数据都在那里。

但是当我尝试再次插入相同的数据而不更改任何内容时,看起来数据已添加到表中,并且我没有收到任何错误。但是当我查看A表时,第二次插入的数据已经不存在了。

如果我尝试通过 SQL 开发人员直接插入相同的数据,数据将被插入到表中。

奇怪的是,如果我只是更改 UI 中 cntrct_no 的值并保持其余数据不变,数据就会被插入。

谁能向我解释一下这可能是什么原因造成的?

不确定这是否有帮助:stmt.executeUpdate();未插入数据时返回 0,插入数据时返回 1。

public void writeToAudit(String contractNo, String tripNo, 
        String tripEffDate, 
        String tripDiscDate, String transpModeId, String userId, 
        String transType, AplLeg[] legs) {

    final Session session = HibernateUtil.getSession();
    Connection con = null;
    con = session.connection(); 
    PreparedStatement stmt = null;
    PreparedStatement stmtSelId = null;
    ResultSet rs = null;
    long nextId = -1;
    int i=0;

    try {

        for(i=0;i<legs.length;i++) {

            String sqlNextId = "SELECT rpt_audit_transportation_seq.NEXTVAL as seqval FROM DUAL";
            stmtSelId = con.prepareStatement(sqlNextId, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = stmtSelId.executeQuery();
            rs.last();
            final int rows = rs.getRow();
            if (rows == 0){
                nextId = -1;
            }
            rs.beforeFirst();
            rs.next();
            nextId = rs.getInt(1);

            if(nextId==-1)
                throw new SQLException("Cannot get next val from rpt_audit_transportation sequence.");

            stmt = con.prepareStatement(WRITE_TO_AUDIT_DML);
            stmt.setLong(1, nextId);
            stmt.setString(2, userId.toUpperCase());
            stmt.setString(3, transType);
            stmt.setString(4, contractNo);
            stmt.setString(5, tripNo);
            stmt.setInt(6, Integer.parseInt(transpModeId));
            stmt.setString(7, tripEffDate);
            stmt.setString(8, tripDiscDate);
            stmt.setLong(9, legs[i].getLegId().longValue());
            int temp =  stmt.executeUpdate();
            con.commit();
        }

        stmt.close();
    }
    catch (Exception e) {
    }
    finally {
        closeConnection(session, con, stmtSelId, rs);
    }
}

SQL 语句:

private static final String WRITE_TO_AUDIT_DML =
    "INSERT INTO rpt_audit_transportation " + 
    "(audit_id, audit_date, audit_process, audit_userid, " +
    "audit_trans_type, audit_route_no, audit_trip_no, " +
    "audit_eff_dt, audit_disc_dt, audit_orig_facility_id, " +
    "audit_dest_facility_id, audit_arvl_tm, audit_dprt_tm, " +
    "audit_leg_seq_no, audit_freq_id, audit_trnsp_mode_id) " +
    "(SELECT ?, " +     // audit id
         "SYSDATE, " +
         "'TOPS_UI', " +
         "?, " +        // userId
         "?, " +
         "rte.cntrct_no, " +
         "trp.trip_no, " +
         "rte.cntrct_eff_dt, " +
         "rte.cntrct_disc_dt, " +
         "NVL(leg.orig_facility_id, trp.orig_fac_id), " +
         "NVL(leg.dest_facility_id, trp.dest_fac_id), " +
         "NVL(leg.arvl_tm, trp.arvl_tm), " +
         "NVL(leg.dprt_tm, trp.dprt_tm), " +
         "leg.leg_seq, " +
         "trp.freq_id, " +
         "rte.trnsp_mode_id " +
        "FROM apl_contract rte, " +
         "apl_trip trp, " +
         "apl_leg leg " +
        "WHERE rte.cntrct_no = ? " +        // contract id
          "AND trp.trip_no = ? " +          // trip no
          "AND rte.trnsp_mode_id = ? " +        // transp mode id
          "AND rte.cntrct_locked_ind = 'N' " +
          "AND trp.trip_eff_dt = to_date(?,'MM/DD/YYYY') " +            // trip eff date
          "AND trp.trip_disc_dt = to_date(?,'MM/DD/YYYY') " +           // trip disc date
          "AND trp.cntrct_id = rte.cntrct_id " +
          "AND leg.trip_id = trp.trip_id " +
          "AND leg.leg_id = ?) ";

最佳答案

看起来您没有插入普通值,而是根据参数进行选择的结果。 您使用的是 INSERT ... SELECT () 子句,因此如果 SELECT 部分不返回任何行,则 INSERT 获胜不插入任何内容,stmt.executeUpdate() 将返回 0。了解为什么 SELECT 不返回任何行。

这可能是由于当您插入 rpt_audit_transportation 时,某些触发器将内容保存在其他表中,但这只是猜测。

关于java - 有时数据不会插入到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10888025/

相关文章:

mysql - UPDATE 可以用不同的方式写吗?

java - 子类抛出 UnsupportedOperationException 与忽略输入参数

mysql - 从 cascalog 写入 MySQL 不起作用。如何调试这个?

java - MySQL 语句关闭后不允许执行任何操作

java - 删除父项后如何存储受影响的值

java - 我有一个函数,在其中我正在使用匿名类运行一个线程,那么如何将值返回给该函数

java - Spring自动注册意义

mysql查询 "SHOW COLUMNS FROM table like ' colmunname'":questions

java - 在 Java 中反转单词而不使用任何语言特定函数

MySQL 将行转为动态数量的列