把以前的笔记整理一下,做个备份方便以后查阅:
要测试的代码如下:
package dao.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import
java.sql.ResultSet;
import util.DBUtil;
import dao.IAccountDAO;
import entity.Account;
public class AccountDAOImpl implements IAccountDAO{
@Override
public Account
findByAccountNo(
String accountNo) throws Exception
{
Account a = null;
Connection conn =
null;
PreparedStatement stat = null;
ResultSet rst
= null;
try{
conn =
DBUtil.getConnection();
String sql = "select * from
t_account " +
"where
accountNo=?";
stat =
conn.prepareStatement(sql);
stat.setString(1,
accountNo);
rst =
stat.executeQuery();
if(rst.next()){
a
= new
Account();
a.setAccountNo(accountNo);
a.setBalance(rst.getInt("balance"));
a.setId(rst.getInt("id"));
}
}catch(Exception
e){
e.printStackTrace();
throw
e;
}finally{
if(rst !=
null){
rst.close();
}
if(stat
!=
null){
stat.close();
}
DBUtil.close(conn);
}
return
a;
}
@Override
public void modify(Account a) throws Exception
{
Connection conn = null;
PreparedStatement stat =
null;
try{
conn =
DBUtil.getConnection();
String sql = "update t_account set
" +
"balance=? where
accountNo=?";
stat =
conn.prepareStatement(sql);
stat.setInt(1,
a.getBalance());
stat.setString(2,
a.getAccountNo());
stat.executeUpdate();
}catch(Exception
e){
e.printStackTrace();
throw
e;
}finally{
if(stat !=
null){
stat.close();
}
DBUtil.close(conn);
}
}
}
测试代码:
package dao.jdbc;
import org.junit.Test;
import util.Factory;
import dao.IAccountDAO;
import entity.Account;
public class AccountDAOImplTest {
@Test
public void testFindByAccountNo() throws Exception
{
IAccountDAO dao =
(IAccountDAO)
Factory.getInstance(
"IAccountDAO");
Account
a =
dao.findByAccountNo(
"6225881003192000");
System.out.println(a);
}
@Test
public
void testModify() throws Exception{
IAccountDAO dao =
(IAccountDAO)
Factory.getInstance(
"IAccountDAO");
Account
a =
dao.findByAccountNo(
"6225881003192000");
a.setBalance(a.getBalance()
- 800);
dao.modify(a);
}
}
原文地址:http://www.cnblogs.com/mxyhws/p/3752409.html