日期:2014-05-16  浏览次数:20468 次

jdbc thin连接oracle10,setSavepoint方法报错解决办法
package test.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;

public class TxTest {
static void update() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Savepoint sp = null;
try {
String sql1 = "update user1 set money = money - 10 WHERE id=1";
conn = JdbcUtil.getConnection();
conn.setAutoCommit(false);
st = conn.createStatement();
st.executeUpdate(sql1);
sp = conn.setSavepoint();

String sql2 = "select money from user1 where id = 2";
rs = st.executeQuery(sql2);
int money = 0;
while (rs.next()) {
money = rs.getInt("money");
}
if (money > 330) {
throw new RuntimeException("!!!!!!!!!!!!");
}

conn.commit();
} catch (RuntimeException e) {
if (conn != null && sp != null) {
conn.rollback(sp);
conn.commit();
throw e;
}
} catch (SQLException e) {
if (conn != null) {
conn.rollback();
throw e;
}
} finally {
JdbcUtil.free(rs, st, conn);
}
}

public static void main(String[] args) throws SQLException {
update();
}
}