流程如下:
创建项目工程如下:
transfer包下的代码如下:
package beyond. transfer. dao; import java. sql. Connection;
import java. sql. SQLException; import org. apache. commons. dbutils. QueryRunner; import beyond. utils. DataSourceUtils; public class TransferDao { public void out ( Connection conn, String out, double money) throws SQLException { QueryRunner runner = new QueryRunner ( ) ; String sql = "update account set money= money-? where name=?" ; runner. update ( conn, sql, money, out) ; } public void in ( Connection conn, String in, double money) throws SQLException { QueryRunner runner = new QueryRunner ( ) ; String sql = "update account set money= money+? where name=?" ; runner. update ( conn, sql, money, in) ; } }
package beyond. transfer. service; import java. sql. Connection;
import java. sql. SQLException; import beyond. transfer. dao. TransferDao;
import beyond. utils. DataSourceUtils; public class TransferService { public boolean transfer ( String out, String in, double money) { TransferDao dao = new TransferDao ( ) ; boolean isTransferSuccess = true ; Connection conn = null; try { conn = DataSourceUtils. getConnection ( ) ; conn. setAutoCommit ( false ) ; dao. out ( conn, out, money) ; dao. in ( conn, in, money) ; } catch ( SQLException e) { isTransferSuccess = false ; try { conn. rollback ( ) ; } catch ( SQLException e1) { e1. printStackTrace ( ) ; } e. printStackTrace ( ) ; } finally { try { conn. commit ( ) ; } catch ( SQLException e) { e. printStackTrace ( ) ; } } return isTransferSuccess; } }
package beyond. transfer. web; import java. io. IOException;
import javax. servlet. ServletException;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse; import beyond. transfer. service. TransferService; public class TransferServlet extends HttpServlet { public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String out = request. getParameter ( "out" ) ; String in = request. getParameter ( "in" ) ; String moneyStr = request. getParameter ( "money" ) ; double money= Double. parseDouble ( moneyStr) ; TransferService service = new TransferService ( ) ; boolean isTransferSuccess = service. transfer ( out, in, money) ; response. setContentType ( "text/html;charset=UTF-8" ) ; if ( isTransferSuccess) { response. getWriter ( ) . write ( "转账成功!!!" ) ; } else { response. getWriter ( ) . write ( "转账失败!!!" ) ; } } public void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet ( request, response) ; }
}
transfer.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv = " Content-Type" content = " text/html; charset=UTF-8" >
< title> Insert title here</ title>
</ head>
< body> < form action = " ${pageContext.request.contextPath}/transfer" method = " post" > 传出账户:< input type = " text" name = " out" > < br/> 传入账户:< input type = " text" name = " in" > < br/> 转转金额:< input type = " text" name = " money" > < br/> < input type = " submit" value = " 确认转账" > < br/> </ form>
</ body>
</ html>
数据库创建如下:
CREATE TABLE ` account` ( ` id` int ( 11 ) NOT NULL AUTO_INCREMENT , ` name` varchar ( 30 ) DEFAULT NULL , ` money` double DEFAULT NULL , UNIQUE KEY ` id` ( ` id` )
) ENGINE = InnoDB AUTO_INCREMENT = 3 DEFAULT CHARSET = utf8
dbutils包下代码如下:
package beyond. jdbc; import java. sql. Connection;
import java. sql. DriverManager;
import java. sql. SQLException;
import java. sql. Statement; public class JDBCDemo { public static void main ( String[ ] args) { Connection conn = null; try { Class. forName ( "com.mysql.jdbc.Driver" ) ; conn = DriverManager. getConnection ( "jdbc:mysql:///web19" , "root" , "wsq" ) ; conn. setAutoCommit ( false ) ; Statement stmt = conn. createStatement ( ) ; int executeUpdate = stmt. executeUpdate ( "update account set money=5000 where name='sq'" ) ; conn. commit ( ) ; stmt. close ( ) ; conn. close ( ) ; } catch ( Exception e) { try { conn. rollback ( ) ; } catch ( SQLException e1) { e1. printStackTrace ( ) ; } e. printStackTrace ( ) ; } }
}
jdbc包下代码如下:
package beyond. jdbc; import java. sql. Connection;
import java. sql. DriverManager;
import java. sql. SQLException;
import java. sql. Statement; public class JDBCDemo { public static void main ( String[ ] args) { Connection conn = null; try { Class. forName ( "com.mysql.jdbc.Driver" ) ; conn = DriverManager. getConnection ( "jdbc:mysql:///web19" , "root" , "wsq" ) ; conn. setAutoCommit ( false ) ; Statement stmt = conn. createStatement ( ) ; int executeUpdate = stmt. executeUpdate ( "update account set money=5000 where name='sq'" ) ; conn. commit ( ) ; stmt. close ( ) ; conn. close ( ) ; } catch ( Exception e) { try { conn. rollback ( ) ; } catch ( SQLException e1) { e1. printStackTrace ( ) ; } e. printStackTrace ( ) ; } }
}
utils包下代码如下:
package beyond. utils; 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 DataSourceUtils { private static DataSource dataSource = new ComboPooledDataSource ( ) ; private static ThreadLocal< Connection> tl = new ThreadLocal < Connection> ( ) ; public static DataSource getDataSource ( ) { return dataSource; } public static Connection getConnection ( ) throws SQLException{ return dataSource. getConnection ( ) ; } public static Connection getCuruentConnection ( ) throws SQLException { Connection con = tl. get ( ) ; if ( con == null) { con = dataSource. getConnection ( ) ; tl. set ( con) ; } return con; } public static void startTransaction ( ) throws SQLException { Connection con = getCuruentConnection ( ) ; if ( con != null) { con. setAutoCommit ( false ) ; } } public static void rollback ( ) throws SQLException { Connection con = getCuruentConnection ( ) ; if ( con != null) { con. rollback ( ) ; } } public static void commitAndRelease ( ) throws SQLException { Connection con = getCuruentConnection ( ) ; if ( con != null) { con. commit ( ) ; con. close ( ) ; tl. remove ( ) ; } } public static void closeConnection ( ) throws SQLException { Connection con = getCuruentConnection ( ) ; if ( con != null) { con. close ( ) ; } } public static void closeStatement ( Statement st) throws SQLException { if ( st != null) { st. close ( ) ; } } public static void closeResultSet ( ResultSet rs) throws SQLException { if ( rs != null) { rs. close ( ) ; } } }
c3p0-config.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config><default-config><property name="user">root</property><property name="password">wsq</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///web19</property></default-config>
</c3p0-config>
需要的一系列的jar包如下: