代码之家  ›  专栏  ›  技术社区  ›  Thorin Oakenshield

Java:插入查询异常

  •  1
  • Thorin Oakenshield  · 技术社区  · 15 年前

    它是否跳过特定的val或抛出异常或回滚整个操作?

    编辑:样例代码

    try
    {
        String sql_ji_inser="insert into job_input values (?,?)";
        PreparedStatement pst_ji_inser=OPConnect.prepareStatement(sql_ji_inser);
    
        for(int i=0;i<v_new_data.size();i++)
                {
          Vector row=new Vector();
                  row=(Vector)v_new_data.get(i);
    
                  job_id=Integer.parseInt(row.get(0).toString());
                  item_no=Integer.parseInt(row.get(1).toString());
                  pst_ji_inser.setInt(1,job_id);
                  pst_ji_inser.setInt(2,item_no);
                  pst_ji_inser.addBatch();
                }
                System.out.println("No of rows inserted"+pst_ji_inser.executeBatch().length);
        }
        catch(Exception ex)
        {
               System.out.println("********Insert Exception*********************");
               ex.printStackTrace();
               return false;
        }
    

    try 
    {
    int count=0;// for checking no of inserting values
    OPConnect.setAutoCommit(false);
    String sql_ji_inser="insert into job_input values (?,?)";
    PreparedStatement pst_ji_inser=OPConnect.prepareStatement(sql_ji_inser);
    for(int i=0;i<v_new_data.size();i++)
        {
        job_id=Integer.parseInt(row.get(0).toString());
        item_no=Integer.parseInt(row.get(1).toString());
        pst_ji_inser.setInt(1,job_id);
        pst_ji_inser.setInt(2,item_no);
        pst_ji_inser.addBatch();
        count++;
        }
    int norowinserted=pst_ji_inser.executeBatch().length;
    if(count==norowinserted)
        {   
        OPConnect.commit();
        }
    }
    catch(Exception ex)
    {
    System.out.println("********Insert Exception*********************");
    OPConnect.rollback();
    ex.printStackTrace();
    return false;
    }
    
    1 回复  |  直到 15 年前
        1
  •  3
  •   Community Mohan Dere    5 年前

    这取决于插入行的方式。如果要将它们插入到已由关闭自动提交的连接上的单个事务中 connection.setAutoCommit(false) 在使用完成insert查询之后,您将提交连接 connection.commit() 你明确地打电话给 connection.rollback() 在catch块中,则整个事务将回滚。否则,你就依赖于你无法控制的环境因素。

    另请参见:


    更新: 这是你的代码重写。注意,连接和语句应该在 try 尝试 finally . 这是为了防止异常情况下的资源泄漏。

    String sql = "insert into job_input values (?, ?)";
    Connection connection = null;
    PreparedStatement statement = null;
    
    try {
        connection = database.getConnection();
        connection.setAutoCommit(false);
        statement = connection.prepareStatement(sql);
    
        for (List row : data) {
            statement.setInt(1, Integer.parseInt(row.get(0).toString()));
            statement.setInt(2, Integer.parseInt(row.get(1).toString()));
            statement.addBatch();
        }
    
        statement.executeBatch();
        connection.commit();
        return true;
    } catch (SQLException e) {
        if (connection != null) try { connection.rollback(); } catch (SQLException logOrIgnore) {}
        e.printStackTrace();
        return false;
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
    

    顺便说一句,我不喜欢退货 boolean 在这里。我只是想办法 void catch throw e 把呼叫码放在 try-catch .

    推荐文章