Transfer example (1): Dao implementation (in this example, QueryRunner is used to execute SQL statements, and the data source is C3P0 ),
Disadvantage: the Dao layer completes Service-level operations, which is not conducive to later code modification and reconstruction.
1. Create C3P0Util on your own
Account Database
2. jar package
3. Dao Layer
Interface:
Package com. learning. dao; import com. learning. domain. account; public interface AccountDao {/*** transfer * @ param fromname transfer user * @ param toname transfer user * @ param money transfer amount */public void updateAccount (String fromname, string toname, double money) throws Exception ;}
Implementation class:
Package com. learning. dao. impl; import java. SQL. SQLException; import org. apache. commons. dbutils. queryRunner; import com. learning. dao. accountDao; import com. learning. util. c3P0Util; public class AccountDaoImpl implements AccountDao {public void updateAccount (String fromname, String toname, double money) throws Exception {// create a QueryRunner object QueryRunner qr = new QueryRunner (C3P0Util. getDataSource (); qr. u Pdate ("update account set money = money -? Where name =? ", Money, fromname); qr. update (" update account set money = money +? Where name =? ", Money, toname );}}