|
请模拟银行转账原理,把A账户上的钱转账给B账户,; |
|
| 5分 |
不论是Statement还是PreparedStatement还是CallableStatment,都只能一次执行一条sql。
你需要做的是事务控制。 比如取到conn后, 最后 |
| 10分 |
String sql=””;
st=conn.createStatment(); sql=”…”; st.addBatch(); sql=”…”; st.addBatch(); st.excuteBatch(); conn.commit(); |
| 4分 |
+1 |
|
最好使用存储过程
|
|
|
这个要用到存储过程。用callableStream 这个接口调用sql中是存储过程。用call
去调用。 |
|
| 3分 |
这两个操作放在一个事物里面就ok了阿.!
你把Connection的autoCommit设置为false, 两个都成功了再conn.commit吧.! |
|
应该用存储过程吧
|
|
|
顶 |
|
|
事务和存储过程都行,存储过程是在数据库中编译的,速度快,只不过你不能修改。
|
|
|
i学习了。,。
|
|
|
我正想在一个方法中执行性质不同两条语句呢!,呵呵,这是提这个问题的重要原因 |
|
|
能不能给出具体点的代码呀,就像这位兄弟一样,目前就发现这段代码我懂了,谢谢 |
|
| 7分 |
这是启事务调两个存储过程的(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;
}
|
|
顺便问一句;
dml(增加,删除,修改)语句都用executeUpdate()方法吗 executeUpdate()与executeUpdate(String sql)方法有什么区别呢;好像没有吧 |
|
|
。。。
|
|
| 1分 |
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; } |
|
上面两个答案可以,你自己看看
|
|
|
既然你是用的JDBC为什么不用存储过程呢
|
|
|
学习了!
|
|