DBUtils 제어 트랜잭션 -------- 이체 작업
6923 단어 JavaWeb
package com.itheima.dao;
import com.itheima.domain.Account;
public interface AccountDao {
/**
*
* @param fromname
* @param toname
* @param money
*/
@Deprecated
public void updateAccount(String fromname,String toname,double money)throws Exception;
/**
*
* @param accout
*/
public void updateAccout(Account accout) throws Exception;
/**
*
* @param name
* @return
* @throws Exception
*/
public Account findAccountByName(String name)throws Exception;
}
dao.impl:
package com.itheima.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.util.C3P0Util;
import com.itheima.util.ManagerThreadLocal;
public class AccountDaoImpl implements AccountDao {
public void updateAccount(String fromname, String toname, double money) throws Exception {
// QueryRunner
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
qr.update("update account set money=money-? where name=?",money,fromname);
qr.update("update account set money=money+? where name=?",money,toname);
}
public void updateAccout(Account account) throws Exception {
QueryRunner qr = new QueryRunner();
qr.update(ManagerThreadLocal.getConnection(),"update account set money=? where name=?",account.getMoney(),account.getName());
}
public Account findAccountByName(String name) throws Exception {
QueryRunner qr = new QueryRunner();
return qr.query(ManagerThreadLocal.getConnection(),"select * from account where name=?", new BeanHandler(Account.class),name);
}
}
service:
package com.itheima.service;
public interface AccountService {
/**
*
* @param fromname
* @param toname
* @param money
*/
public void transfer(String fromname,String toname,double money);
}
service.impl:
package com.itheima.service.impl;
import java.sql.Connection;
import java.sql.SQLException;
import com.itheima.dao.AccountDao;
import com.itheima.dao.impl.AccountDaoImpl;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import com.itheima.util.C3P0Util;
import com.itheima.util.ManagerThreadLocal;
public class AccountServiceImpl implements AccountService {
public void transfer(String fromname, String toname, double money) {
// ad.updateAccount(fromname, toname, money);
AccountDao ad = new AccountDaoImpl();
try {
ManagerThreadLocal.startTransacation();//begin
//
Account fromAccount = ad.findAccountByName(fromname);
Account toAccount = ad.findAccountByName(toname);
//
fromAccount.setMoney(fromAccount.getMoney()-money);
toAccount.setMoney(toAccount.getMoney()+money);
//
ad.updateAccout(fromAccount);
// int i = 10/0;
ad.updateAccout(toAccount);
ManagerThreadLocal.commit();//
} catch (Exception e) {
try {
ManagerThreadLocal.rollback();//
} catch (Exception e1) {
e1.printStackTrace();
}
}finally{
try {
ManagerThreadLocal.close();
} catch (Exception e) {
e.printStackTrace();
}//
}
}
}
domain:
package com.itheima.domain;
public class Account {
private int id;
private String name;
private double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + ", money=" + money
+ "]";
}
}
util:
C3P0Util:
package com.itheima.util;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Util {
//
private static DataSource dataSource = new ComboPooledDataSource();
public static DataSource getDataSource() {
return dataSource;
}
//
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(" ");
}
}
public static void release(Connection conn,Statement stmt,ResultSet rs){
//
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();//
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}
}
ManagerThreadLocal:
package com.itheima.util;
import java.sql.Connection;
import java.sql.SQLException;
public class ManagerThreadLocal {
private static ThreadLocal tl = new ThreadLocal();
//
public static Connection getConnection(){
Connection conn = tl.get();//
if(conn==null){
conn = C3P0Util.getConnection();//
tl.set(conn);// conn
}
return conn;
}
//
public static void startTransacation(){
try {
Connection conn = getConnection();
conn.setAutoCommit(false);// ,
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void commit(){
try {
getConnection().commit();//
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void rollback(){
try {
getConnection().rollback();//
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(){
try {
getConnection().close();//
tl.remove();// conn
} catch (SQLException e) {
e.printStackTrace();
}
}
}
xml:
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/day13
root
abc
10
30
100
10
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JavaWeb 파일 다운로드 기능 인스턴스 코드업무 중에 만난 파일을 다운로드하는 기능은 스스로 추출합니다. 코드가 간단합니다. 여러분에게 도움이 되었으면 합니다. 자, 말이 많지 않습니다. 코드를 올리십시오! 이상은 본고의 JavaWeb 파일을 다운로드한 코드...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.