package com.neusoft.struts.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import com.neusoft.struts.util.ConnectionUtil;
import com.neusoft.struts.vo.ContactForm;
public class ContactDAO {
private int allPage;
private int nowPage;
private int pageSize = 5;
private DataSource ds;
public ContactDAO() {
super();
}
public ContactDAO(DataSource ds) {
super();
this.ds = ds;
}
public List<ContactForm> getAllConatct() {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from contact";
List<ContactForm> list = new ArrayList<ContactForm>();
try {
// ����rs�ɹ�
con = ds.getConnection();
stmt = con.prepareStatement(sql.toString(),
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
// ������ҳ��
rs.last();
int count = rs.getRow();
this.allPage = (count + this.pageSize - 1) / this.pageSize;
// ��ȡ��ǰҳ���
rs.beforeFirst();// rs归位
for (int i = 0; i < (this.nowPage - 1) * this.pageSize; i++) {
rs.next();// ��ָ��ŵ�ָ��λ��
}
// ������
for (int i = 0; i < this.pageSize; i++) {
if (!rs.next()) {
break;// ����ݲ���pagesizeʱ���ж�
}
ContactForm user = new ContactForm();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPhone(rs.getString("phone"));
user.setAddress(rs.getString("address"));
// user.setBirth(rs.getString("birth"));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
ConnectionUtil.close(con, stmt, rs);
}
return list;
}
// ���jϵ��
public boolean addContact(String name, String phone, String address) {
boolean flag = false;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
int _phone = Integer.parseInt(phone);
String sql = "INSERT INTO contact VALUES(auto_id.nextval,?,?,?)";
try {
con = ds.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, _phone);
ps.setString(3, address);
int i = ps.executeUpdate();
if (i != 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.close(con, ps, rs);
}
return flag;
}
// ɾ����Ŀ
public boolean delete(int id) {
boolean flag = false;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "delete from contact where id = ?";
try {
con = ds.getConnection();
ps = con.prepareStatement(sql);
ps.setInt(1, id);
int i = ps.executeUpdate();
if (i != 0) {
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionUtil.close(con, ps, rs);
}
return flag;
}
// ���ID�õ��û���Ϣ
public ContactForm selectById(int id) {
boolean flag = false;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from contact where id = ?";
ContactForm contact = new ContactForm();
try {
con = ds.getConnection();
ps = con.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
while (rs.next()) {
contact.setId(rs.getInt("id"));
contact.setName(rs.getString("name"));
contact.setPhone(rs.getString("phone"));
contact.setAddress(rs.getString("address"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.close(con, ps, rs);
}
return contact;
}
// ������Ŀ
public boolean update(int id, ContactForm contactForm) {
boolean flag = false;
int _phone = Integer.parseInt(contactForm.getPhone());
Connection con = null;
ResultSet rs = null;
PreparedStatement ps = null;
String sql = "update contact set name = ?,phone = ?, address = ? where id =?";
try {
con = ds.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, contactForm.getName());
ps.setInt(2, _phone);
ps.setString(3, contactForm.getAddress());
ps.setInt(4, id);
int i = ps.executeUpdate();
if (i != 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.close(con, ps, rs);
}
return flag;
}
// ��ѯ�û�
public List<ContactForm> queryContact(ContactForm contactForm) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
List<ContactForm> list = new ArrayList<ContactForm>();
StringBuffer sql = new StringBuffer("select * from contact where 1=1");
// ���ǰ̨����4����ݽ��������ѯ
// int id =contactForm.getId();
String _name = contactForm.getName();
String _address = contactForm.getAddress();
String _phone = contactForm.getPhone();
if (_name != null && !_name.equals("")) {
sql.append(" and name like '%" + _name + "%'");
}
if (_phone != null && !_phone.equals("")) {
sql.append(" and phone like '" + _phone + "%'");
}
if (_address != null && !_address.equals("")) {
sql.append(" and address like '" + _address + "%'");
}
try {
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(sql.toString());
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String phone = rs.getString("phone");
String address = rs.getString("address");
ContactForm c = new ContactForm(id, name, phone, address);
list.add(c);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionUtil.close(con, stmt, rs);
}
return list;
}
public int getAllPage() {
return allPage;
}
public void setAllPage(int allPage) {
this.allPage = allPage;
}
public int getNowPage() {
return nowPage;
}
public void setNowPage(int nowPage) {
this.nowPage = nowPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
评论0