请模拟银行转账原理,把A账户上的钱转账给B账户,;

解决方案 »

  1.   

    不论是Statement还是PreparedStatement还是CallableStatment,都只能一次执行一条sql。你需要做的是事务控制。比如取到conn后,
    conn.setAutoCommit(false);
    ....
    ...
    if(成功)
       conn.commit();
    else
      conn.rollback();最后
    conn.setAutoCommit(true);
      

  2.   

    String sql="";
    st=conn.createStatment();
    sql="...";
    st.addBatch();
    sql="...";
    st.addBatch();
    st.excuteBatch();
    conn.commit();
      

  3.   


    +1
    可以尝试用下 addBatch() 
      

  4.   

    这个要用到存储过程。用callableStream 这个接口调用sql中是存储过程。用call
    去调用。
      

  5.   

    这两个操作放在一个事物里面就ok了阿.!
    你把Connection的autoCommit设置为false,
    两个都成功了再conn.commit吧.!
      

  6.   

    这是启事务调两个存储过程的(mysql不支持数组没办法):public static int insPublisher(PublisherObj po) throws IPlugException {
    IConnection icon = null;
    ICallableStatement cs = null;
    int ret = -10000;
    try {
    icon = DbFactory.getInstance().getConnection(DBI.GLOBAL.DB);
    icon.setAutoCommit(false);
    cs = icon.prepareCall(proc_ins_publishers);
    cs.setString(1, po.getPublisherID());
    cs.setString(2, po.getUserID());
    cs.setString(3, po.getLoginName());
    cs.setString(4, po.getName());
    cs.setString(5, po.getContactName());
    cs.setString(6, po.getAddress());
    cs.setString(7, po.getPhone());
    cs.setString(8, po.getMobile());
    cs.setString(9, po.getUserType());
    cs.setString(10, po.getFax());
    cs.setString(11, po.getZipCode());
    cs.setString(12, po.getEmail());
    cs.registerOutParameter(13, Types.INTEGER);
    cs.execute();
    ret = cs.getInt(13);
    if (ret == 1) {
    cs.close();
    cs = icon.prepareCall(proc_ins_lnk_pub_dest);
    String[] lst = po.getDests().split(",");
    for (int i = 0; i < lst.length && !lst[i].equals(""); i++) {
    cs.setString(1, po.getPublisherID());
    cs.setInt(2, Integer.parseInt(lst[i]));
    cs.registerOutParameter(3, Types.INTEGER);
    cs.execute();
    ret = cs.getInt(3);
    if (ret == -1) {
    throw new Exception("DB Error!");
    }
    }
    } else if (ret == -2) {
    throw new Exception("Dup_Name Error!");
    } else {
    throw new Exception("DB Error!");
    }
    icon.commit();
    } catch (Exception e) {
    if (icon != null) {
    try {
    icon.rollback();
    } catch (SQLException e1) {
    e1.printStackTrace();
    }
    }
    e.printStackTrace();
    } finally { if (cs != null) {
    try {
    cs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (icon != null) {
    try {
    icon.setAutoCommit(true);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    try {
    icon.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    return ret;
    }
      

  7.   

    顺便问一句;
    dml(增加,删除,修改)语句都用executeUpdate()方法吗
    executeUpdate()与executeUpdate(String sql)方法有什么区别呢;好像没有吧
      

  8.   

    public boolean saveOrUpdateCommit(String sql, String sql2) {
    boolean temp=true;
    try {
    conn.setAutoCommit(false);
    stmt = conn.createStatement();
    stmt.addBatch(sql);
    stmt.addBatch(sql2);
    stmt.executeBatch();
    conn.commit();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    temp=false;
    // 回滚
    try {
    conn.rollback();
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    return temp;
    }
      

  9.   

    既然你是用的JDBC为什么不用存储过程呢