JDBC事务
什么事务?
一件事情有N个单元组成,要么这N个单元同行执行成功,要么同时失败,就是值将这N个单元都放在一个事务里。
Mysql事务:
例如:zhangsan——lisi转帐,对应于两条sql语句
update account set money=money-100 where aname=‘zhangsan’;
update account set money=money+100 where aname=‘lisi’;
MySql默认自动提交。及执行一条sql语句提交一次事务。
数据库默认事务是自动提交的,一条Sql语句就是一个事务,如果想多条sql放在一个事务中执行,则需要使用如下语句。
开启事务:Start transaction
提交事务:commit 这句代码所指示的是从数据库开启事务到提交事务 中间的所有sql都认为是真真正正的更新到数据库!!
事务回滚:rollback;回滚功能,从开启事务到事务回滚,中间所有的sql语句都认为是无效数据,不更新数据库,回滚到开启事务! 如果进行了回滚功能,则必须要重新开启,重新提交(commit)方可有效
JDBC事务:
Connection.setAutoCommit(false); // 相当于start transaction 默认是true,false是不用自动提交(手动提交的意思)
Connection.rollback(); rollback
Connection.commit(); commit
注意:控制事务的connection必须是同一个
执行sql的connection与开启事务的connnection必须是同一个才能对事务进行控制
DBUtils事务
QueryRunner
有参构造:QueryRunner runner = new QueryRunner(DataSource dataSource);
有参构造将数据源(连接池)作为参数传入QueryRunner,QueryRunner会从连 接池中获得一个数据库连接资源操作数据库,所以直接使用无Connection参数 的update方法即可操作数据库
无参构造:QueryRunner runner = new QueryRunner();
无参的构造没有将数据源(连接池)作为参数传入QueryRunner,那么我们在使 用QueryRunner对象操作数据库时要使用有Connection参数的方法
package com.oracle.domain; public class Users { private String uid; private String username; private String password; private String name; private String email; private String telephone; private String birthday; private String sex; private int state; private String code; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getState() { return state; } public void setState(int state) { this.state = state; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String toString() { return "Users [uid=" + uid + ", username=" + username + ", password=" + password + ", name=" + name + ", email=" + email + ", telephone=" + telephone + ", birthday=" + birthday + ", sex=" + sex + ", state=" + state + ", code=" + code + "]"; } }
package com.oracle.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.oracle.domain.Users; import com.oracle.tools.JDBCUtils; import com.oracle.tools.MyDBUtils; public class UserDao { //注册 public void register(Users users) throws SQLException{ //创建QueryRunner对象 QueryRunner qr=new QueryRunner(MyDBUtils.getDataSource()); String sql="insert into users(uid,username,password,email,name,sex,birthday) values(?,?,?,?,?,?,?)"; qr.update( sql, new Object[] {users. getUid(), users. getUsername(), users. getPassword(),users. getEmail(),users. getName(),users. getSex(),users. getBirthday()}); } //登录 public int login(String username,String password) throws SQLException{ QueryRunner qr = new QueryRunner(MyDBUtils.getDataSource()); String sql = "select count(*) from users where username=? and password=?"; Long count = qr.query(sql, new ScalarHandler<Long>(),username,password); return count.intValue(); } }
package com.oracle.service; import java.sql.SQLException; import com.oracle.dao.UserDao; import com.oracle.domain.Users; public class UsersService { private UserDao usersDao=new UserDao(); //注册 public void register(Users users){ try { usersDao.register(users); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //登录 public int login(String username,String password){ int count=0; try { count=usersDao.login(username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return count; } }