如何用JDO开发数据库应用(7)
如何用JDO开发数据库应用(7)
· 4.5.1. 录入信用卡资料
为了放置业务逻辑方法,为简便起见,我们将在Main类中用静态方法来完成这些业务功能。首先,我们写一个方法来完成添加信用卡资料的功能:
/**
* 录入新信用卡,只需要录入必须的资料,其它的信息自动产生。
* @return 生成的新信用卡对象
* @throws IdCardDuplicatedException 身份证号重复,不允许创建新卡
*/
public static CreditCard inputCard(
String name,
String address,
String idcard,
String phone,
float initialBalance,
float allowOverDraft
) throws IdCardDuplicatedException {
CreditCard cc = new CreditCard();
cc.setName(name);
cc.setAddress(address);
cc.setIdcard(idcard);
cc.setPhone(phone);
cc.setInitialBalance(initialBalance);
cc.setAllowOverDraft(allowOverDraft);
//以下是自动产生的信息:
cc.setCreateTime(new Date());
cc.setBalance(initialBalance); //使刚创建后的余额等于初始余额,这是典型的业务逻辑
//下面将新信用卡保存到数据库,注意其中的JDO API。
PersistenceManager pm = getPersistenceManager();
//先检测是否已经有该身份证注册的信用卡存在:
Query q = pm.newQuery(CreditCard.class,"idcard==_newIdcard");
q.declareParameters("String _newIdcard");
Collection existCards = (Collection)q.execute(idcard);
if(existCards.iterator().hasNext()) {
throw new IdCardDuplicatedException(); //已经该身份证号存在
}
//身份证号没重复,以下保存该信用卡对象:
pm.currentTransaction().begin(); //每次对数据库的更新必须放到事务中
pm.makePersistent(cc);
pm.currentTransaction().commit(); //提交新对象
pm.close(); //释放JDO资源
return cc;
}
public static class IdCardDuplicatedException extends RuntimeException {}
下面我们运行一下这个程序,将Main.main()改写如下:
public static void main(String[] args) throws Exception {
System.out.println("开始测试功能……");
inputCard("张三","东风东路311号","223003433995431237","020-38864157",500.00f,5000.0f);
System.out.println("信用卡已创建!");
}
编译,并运行(也可以在build.xml所在目录下运行“ant run”),系统显示:
开始测试功能……
jdbc.con.connect jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GB2312
jdbc.stat.exec set session transaction isolation level read committed
jdbc.stat.execQuery select version()
jdbc.con.rollback
jdbc.con.close
jdbc.con.connect jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GB2312
jdbc.stat.exec set session transaction isolation level read committed
jdbc.stat.execUpdate update jdo_keygen set last_used_id = last_used_id + ? where table_name = transaction_record
jdbc.stat.execQuery select max(transaction_record_id) from transaction_record
jdbc.stat.exec insert into jdo_keygen (table_name, last_used_id) values (transaction_record, 0)
jdbc.con.commit
jdbc.stat.execUpdate update jdo_keygen set last_used_id = last_used_id + ? where table_name = credit_card
jdbc.stat.execQuery select max(credit_card_id) from credit_card
jdbc.stat.exec insert into jdo_keygen (table_name, last_used_id) values (credit_card, 0)
jdbc.con.commit
jdbc.con.commit
jdbc.con.rollback
JDO Genie: Created RMI registry on port 2388
JDO Genie: Bound to jdogenie.jdogenie1
pm.created
jdoql.compile credit.system.CreditCard
idcard==_newIdcard
jdbc.stat.execQuery select credit_card_id, address, allow_over_draft, balance, create_time, idcard, initial_balance, last_transaction_time, nme, phone, jdo_version from credit_card where idcard = ?
jdbc.con.commit
tx.begin
tx.commit
jdbc.con.connect jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GB2312
jdbc.stat.exec set session transaction isolation level read committed
jdbc.stat.execUpdate update jdo_keygen set last_used_id = last_used_id + ? where table_name = credit_card
jdbc.stat.execQuery select last_used_id from jdo_keygen where table_name = credit_card
jdbc.con.commit
jdbc.stat.exec insert into credit_card (credit_card_id, address, allow_over_draft, balance, create_time, idcard, initial_balance, last_transaction_time, nme, phone, jdo_version) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
jdbc.con.commit
jdbc.con.commit
pm.closed
信用卡已创建!
以上信息说明我们的第一个功能已经正常完成,一条信用卡记录已经生成到数据库中。你可以通过mysql的数据库查询工具验证。我们发现Main函数返回后,系统并未退出运行,为什么呢?原来JDOGenie在默认的状态下打开了远程控制的监听服务,这对以后跟踪WebApp的
|
|