jdbc工具类 package com.offcn.util; import java.sql.Connection; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtil { private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); //线程中存储连接的形式相当于map(key,value) private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); //获取一个连接 private static Connection getconn() { Connection conn = tl.get();//从线程中获取连接 if (conn==null) { try { //当第一次问线程中获取连接时,线程中时没有连接的,需要去连接池获取一条 dataSource.getConnection(); tl.set(conn);//将连接绑定到线程中去 } catch (SQLException e) { e.printStackTrace(); } } return null; } //开启事务 public static void startTransaction() { Connection conn = getconn(); try { conn.setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } } //提交事务 public static void commitTransaction() { Connection conn = getconn(); try { conn.commit(); } catch (SQLException e) { e.printStackTrace(); } } //回滚事务 public static void rollBackTransaction() { Connection conn = getconn(); try { conn.rollback(); } catch (SQLException e) { e.printStackTrace(); } } } ------------------------------------------------------------------------------------------------------------------------------ 控制层 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解决请求传递中文参数乱码的的问题 浏览器--》服务器 request.setCharacterEncoding("utf-8"); //服务器--》浏览器乱码问题的解决 response.setContentType("text/html;charset=utf-8"); String from = request.getParameter("from"); String to = request.getParameter("to"); double money =Double.parseDouble( request.getParameter("money")); MoneyService ms = new MoneyService(); Boolean flag = ms.trans(from,to,money); if(flag) { response.getWriter().println("转账成功。。。。。。"); }else { response.getWriter().println("转账失败。。。。。。"); } } ------------------------------------------------------------------------------------------------------------------------------ 业务层 public class MoneyService { public Boolean trans(String from, String to, double money) { MoneyDao md = new MoneyDao(); try { JdbcUtil.startTransaction(); int a = md.from(from,money); int b = md.to(to,money); if(a*b>0) { JdbcUtil.commitTransation(); return true; } } catch (Exception e) { e.printStackTrace(); JdbcUtil.rollBackTransaction(); }finally { try { JdbcUtil.getConn().close(); } catch (SQLException e) { e.printStackTrace(); } } return false; } } ------------------------------------------------------------------------------------------------------------------------------ 持久层 public class MoneyDao { public int from(String from, double money) throws Exception { Connection conn = JdbcUtil.getConn(); PreparedStatement pstmt = conn.prepareStatement("update account set money=money-? where name=?"); pstmt.setDouble(1, money); pstmt.setString(2, from); int i = pstmt.executeUpdate(); pstmt.close(); return i; } public int to(String to, double money) throws Exception { Connection conn = JdbcUtil.getConn(); PreparedStatement pstmt = conn.prepareStatement("update account set money=money+? where name=?"); pstmt.setDouble(1, money); pstmt.setString(2, to); int i = pstmt.executeUpdate(); pstmt.close(); return i; } }