代码如下。每次都会更新值,ROLLBACK没用么?
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("update test set name=? where id=?");
pstmt.setString(1, "GG");
pstmt.setInt(2, 3);
pstmt.executeUpdate();
conn.rollback();
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("update test set name=? where id=?");
pstmt.setString(1, "GG");
pstmt.setInt(2, 3);
pstmt.executeUpdate();
conn.rollback();
conn.rollback();
你应该用
conn.commit();
if(z==1 )
{
conn.commit();//事務提交
}
else
{
conn.rollback();//操作不成功回*
}
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jpetstore";
Connection conn = DriverManager.getConnection(url, "root", "root");
conn.setAutoCommit(false);
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("update test set name=? where id=?");
pstmt.setString(1, "ZZZ");
pstmt.setInt(2, 3);
pstmt.executeUpdate();
conn.rollback();
conn.setAutoCommit(false);
Statement stmt=null;
stmt =conn.createStatement(1005,1008);
String sql="update test set name='"+gg+"' where id=3);
int zz=stmt.executeUpdate(sql);
if(z==1 )
{
conn.commit();//事務提交
}
else
{
conn.rollback();//操作不成功回*
}还不行的话,从其他方面找原因!
int zz=stmt.executeUpdate(sql);
应改为
int z=stmt.executeUpdate(sql);
改为
String sql="update test set name='"+gg+"' where id=3";
MYSQL吧,表用的是MyIsam吧?只有InnoDB才支持事务
请问你一下,你事务都没开启,你何来的rollback()呀。
真没搞懂
先关闭自动提交功能
set autocommit=0;然后再执行rollback就可以了