【问题标题】:Java : Insert query-ExceptionJava:插入查询异常
【发布时间】:2010-09-18 04:16:33
【问题描述】:

我对数据库操作有疑问。我有一个插入查询应该运行 10 次。循环开始并在插入 6 时插入 4 或 5 val,db 连接失败了一段时间,然后再次连接。那么会发生什么, 它是跳过那个特定的 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;
}

【问题讨论】:

    标签: java jdbc


    【解决方案1】:

    这取决于您插入行的方式。如果您将它们插入到由connection.setAutoCommit(false) 关闭自动提交的连接上的单个事务中,并且您在使用connection.commit() 完成插入查询后提交连接,并且您在内部显式调用connection.rollback() catch 块,那么整个事务将被回滚。否则,您将依赖于您无法控制的环境因素。

    另见:


    更新:这是对您的代码的重写。请注意,连接和语句应在try 之前声明,在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 中。

    【讨论】:

    • @BalusC:谢谢,我没有使用任何 autocommit() 和 rollback()。在 try 块中我使用了插入查询并使用 addbatch() 处理所有值,最后使用 executeBatch()
    • 然后相应地修复它。顺便说一句,Vector 确实是一个遗留类。这是您必须维护的 15 年历史的代码库吗?从 Java 1.2 开始,ArrayList 被引入作为 Vector 的高级替代品。
    • @BalusC:请注意编辑后的帖子
    猜你喜欢
    • 2012-11-23
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多