package com.lhhc.persons.module;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class PersonInfoDAO {
public PersonInfoVO findPersonById(Connection conn, Integer id)
throws SQLException{
Statement stmt = null;
ResultSet rs = null;
PersonInfoVO personInfoVO = null;
stmt = conn.createStatement();
String sql =
new StringBuffer(
"SELECT id, name, sex, age, phone, address, description FROM persons WHERE id=")
.append(id).toString();
rs = stmt.executeQuery(sql);
if (rs.next()){
personInfoVO =
new PersonInfoVO(rs.getInt("id"), rs.getString("name"),
rs.getString("sex"), rs.getInt("age"),
rs.getString("phone"), rs.getString("address"),
rs.getString("description"));
}
if (rs != null){
rs.close();
rs = null;
}
if (stmt != null){
stmt.close();
stmt = null;
}
return personInfoVO;
}
public List findAllPersons(Connection conn) throws SQLException{
List persons = new ArrayList();
Statement stmt = null;
ResultSet rs = null;
PersonInfoVO personInfoVO = null;
stmt = conn.createStatement();
String sql = "SELECT id, name, sex, age, phone, address, description FROM persons";
rs = stmt.executeQuery(sql);
while (rs.next()){
personInfoVO =
new PersonInfoVO(rs.getInt("id"), rs.getString("name"),
rs.getString("sex"), rs.getInt("age"),
rs.getString("phone"), rs.getString("address"),
rs.getString("description"));
persons.add(personInfoVO);
}
if (rs != null){
rs.close();
rs = null;
}
if (stmt != null){
stmt.close();
stmt = null;
}
return persons;
}
public int deletePersons(Connection conn, String[] ids) throws SQLException{
StringBuffer personIds = new StringBuffer("");
for (int i = 0; i < ids.length; i++){
personIds.append(ids[i]).append(",");
}
personIds.append("0");
Statement stmt = null;
int count = 0;
stmt = conn.createStatement();
String sql = new StringBuffer("DELETE FROM persons WHERE id IN (")
.append(personIds).append(")").toString();
System.out.println("Delete SQL: ");
System.out.println(sql);
count = stmt.executeUpdate(sql);
if (stmt != null){
stmt.close();
stmt = null;
}
return count;
}
public Integer getMaxId(Connection conn) throws SQLException{
Integer maxid = new Integer(0);
Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement();
String sql =
new StringBuffer(
"SELECT max(id) FROM persons").toString();
rs = stmt.executeQuery(sql);
if (rs.next()){
maxid = rs.getInt(1);
}
if (rs != null){
rs.close();
rs = null;
}
if (stmt != null){
stmt.close();
stmt = null;
}
return maxid;
}
public int insertPerson(Connection conn, PersonInfoVO person) throws SQLException{
Statement stmt = null;
int count = 0;
stmt = conn.createStatement();
String sql =
new StringBuffer("INSERT INTO persons (id, name, sex, age, phone, address, description)")
.append(" VALUES(").append(person.getId()).append(",'")
.append(person.getName()).append("','").append(person.getSex())
.append("',").append(person.getAge()).append(",'")
.append(person.getPhone()).append("','")
.append(person.getAddress()).append("','")
.append(person.getDescription()).append("')").toString();
count = stmt.executeUpdate(sql);
if (stmt != null){
stmt.close();
stmt = null;
}
return count;
}
public int updatePerson(Connection conn, PersonInfoVO person) throws SQLException{
Statement stmt = null;
int count = 0;
stmt = conn.createStatement();
String sql =
new StringBuffer("UPDATE persons SET name='").append(person.getName())
.append("', sex='").append(person.getSex())
.append("', age=").append(person.getAge())
.append(", phone='").append(person.getPhone())
.append("', address='").append(person.getAddress())
.append("', description='").append(person.getDescription())
.append("' WHERE id=").append(person.getId()).toString();
count = stmt.executeUpdate(sql);
if (stmt != null){
stmt.close();
stmt = null;
}
return count;
}
}