package Transaction;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class transactiontest { /**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
business();
}
@SuppressWarnings("null")
public static void business() throws SQLException{
String url="jdbc:mysql://localhost:3306/er";
String user="root";
String password="123456";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn=DriverManager.getConnection( url,user, password);
String sql="update user set salary=salary-100 where id=1";
st.executeUpdate(sql);
sql="select salary from user where id=2";
rs=st.executeQuery(sql);
float salary=0.0f;
if(rs.next())
{
salary=rs.getFloat("salary");
}
if(salary>7000)
throw new RuntimeException("已经超过最大值!!");
sql="update user set salary=salary+100 where id=2";
st.executeUpdate(sql);
conn.commit();
}
catch (SQLException e)
{
if(conn!=null)
conn.rollback();
throw e;
}
finally{
if(rs!=null) rs.close();
if(st!=null) st.close();
if(conn!=null) conn.close();
}
}
}
哪位大侠帮忙看一下啊。。
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class transactiontest { /**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
business();
}
@SuppressWarnings("null")
public static void business() throws SQLException{
String url="jdbc:mysql://localhost:3306/er";
String user="root";
String password="123456";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn=DriverManager.getConnection( url,user, password);
String sql="update user set salary=salary-100 where id=1";
st.executeUpdate(sql);
sql="select salary from user where id=2";
rs=st.executeQuery(sql);
float salary=0.0f;
if(rs.next())
{
salary=rs.getFloat("salary");
}
if(salary>7000)
throw new RuntimeException("已经超过最大值!!");
sql="update user set salary=salary+100 where id=2";
st.executeUpdate(sql);
conn.commit();
}
catch (SQLException e)
{
if(conn!=null)
conn.rollback();
throw e;
}
finally{
if(rs!=null) rs.close();
if(st!=null) st.close();
if(conn!=null) conn.close();
}
}
}
哪位大侠帮忙看一下啊。。
Statement=null;
Exception in thread "main" com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Can't call rollback when autocommit=true
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Connection.rollback(Connection.java:5094)
at Transaction.transactiontest.business(transactiontest.java:54)
at Transaction.transactiontest.main(transactiontest.java:17)
这是怎么回事啊?
conn.rollback();
throw e;你如果想出错要rollback的话就先把 数据库的自动提交 给关闭 在你的代码conn=DriverManager.getConnection( url,user, password);下面 加上一句conn.setAutoCommit(false); 就可以了
http://database.51cto.com/art/201006/204217.htm