package com.addbook.dao.impl;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.addbook.dao.AddressBookDAO;
import com.addbook.dao.ConnectDBHandler;
import com.addbook.vo.AddressBookBean;
/**
* 数据访问层
*
* @author PGJ
*
*/
public class AddressBookDAOImpl extends ConnectDBHandler implements
AddressBookDAO {
/**
* 添加联系人信息
*/
public void insertAddressBookInfo(AddressBookBean addressBook)
throws Exception {
try {
String sql = "insert into MyAddrBook(id,firstName,lastName,jobTitle,department,offPh,mobile,email)values(seq_book.nextval,?,?,?,?,?,?,?)";
pstmt = openConn().prepareStatement(sql);
pstmt.setString(1, addressBook.getFirstName());
pstmt.setString(2, addressBook.getLastName());
pstmt.setString(3, addressBook.getJobTitle());
pstmt.setString(4, addressBook.getDepartment());
pstmt.setString(5, addressBook.getOffPh());
pstmt.setString(6, addressBook.getMobile());
pstmt.setString(7, addressBook.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConn();
closePstmt();
}
}
/**
* 查询所有联系人信息
*/
public List findAddressBookInfo() throws Exception {
List list = new ArrayList();
try {
String sql = "select * from MyAddrBook";
pstmt = openConn().prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
AddressBookBean addressBook = new AddressBookBean();
addressBook.setId(rs.getLong(1));
addressBook.setFirstName(rs.getString(2));
addressBook.setLastName(rs.getString(3));
addressBook.setJobTitle(rs.getString(4));
addressBook.setDepartment(rs.getString(5));
addressBook.setOffPh(rs.getString(6));
addressBook.setMobile(rs.getString(7));
addressBook.setEmail(rs.getString(8));
list.add(addressBook);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConn();
closePstmt();
closeRs();
}
return list;
}
/**
* 根据ID删除联系人信息
*/
public void deleteAddressBookInfo(AddressBookBean addressBook)throws Exception {
try{
String sql = "delete from MyAddrBook where id = ?";
pstmt = openConn().prepareStatement(sql);
pstmt.setLong(1, addressBook.getId());
pstmt.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
closeConn();
closePstmt();
}
}
/**
* 根据ID查询联系人信息
*/
public AddressBookBean findAddressBookBeanById(Long id)
throws Exception {
AddressBookBean addressBook = new AddressBookBean();
try {
String sql = "select * from MyAddrBook where id=?";
pstmt = openConn().prepareStatement(sql);
pstmt.setLong(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
addressBook.setId(rs.getLong(1));
addressBook.setFirstName(rs.getString(2));
addressBook.setLastName(rs.getString(3));
addressBook.setJobTitle(rs.getString(4));
addressBook.setDepartment(rs.getString(5));
addressBook.setOffPh(rs.getString(6));
addressBook.setMobile(rs.getString(7));
addressBook.setEmail(rs.getString(8));
}
} catch (SQLException e) {
throw e;
} finally {
closeConn();
closePstmt();
closeRs();
}
return addressBook;
}
/**
*更新联系人信息
*/
public AddressBookBean updateAddressBookInfo(AddressBookBean addressBook)
throws Exception {
try {
String sql = "update MyAddrBook set firstName=?,lastName=?,jobTitle=?,department=?,offPh=?,mobile=?,email=? where id= ?";
pstmt = openConn().prepareStatement(sql);
pstmt.setString(1, addressBook.getFirstName());
pstmt.setString(2, addressBook.getLastName());
pstmt.setString(3, addressBook.getJobTitle());
pstmt.setString(4, addressBook.getDepartment());
pstmt.setString(5, addressBook.getOffPh());
pstmt.setString(6, addressBook.getMobile());
pstmt.setString(7, addressBook.getEmail());
pstmt.setLong(8, addressBook.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConn();
closePstmt();
}
return addressBook;
}
}