package com.demo.javabean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
/**
*
* 通信录bean
* */
public class AddressBean {
/**
*
* 返回通信录列表
* */
public boolean list(HttpServletRequest request, String username,
String strPageSize, String strPageNo) {
Connection dbConnection = null;
PreparedStatement pStatement = null;
ResultSet res = null;
boolean flag = false;
try {
List<Hashtable<String, String>> list = new ArrayList<Hashtable<String, String>>();
int pageSize = Integer.parseInt(strPageSize);
int pageNo = Integer.parseInt(strPageNo);
int start = pageSize * (pageNo - 1);
dbConnection = ConnectionManager.getConnection();
String sql1 = "select count(*) as countall from address where username='"
+ username + "'";
pStatement = dbConnection.prepareStatement(sql1);
res = pStatement.executeQuery();
while (res.next()) {
String countall = res.getString("countall");
int rowCount = Integer.parseInt(countall);
request.setAttribute("rowCount", rowCount);
int pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
: rowCount / pageSize + 1;
request.setAttribute("pageCount", pageCount);
int pageFirstNo = 1;
int pageLastNo = pageCount;
int pagePreNo = pageNo > 1 ? pageNo - 1 : 1;
int pageNextNo = pageNo < pageCount ? pageNo + 1 : pageCount;
request.setAttribute("pageFirstNo", pageFirstNo);
request.setAttribute("pageLastNo", pageLastNo);
request.setAttribute("pagePreNo", pagePreNo);
request.setAttribute("pageNextNo", pageNextNo);
}
String sql2="select * from address where username='"+username+"' order by name";
pStatement = dbConnection.prepareStatement(sql2);
res = pStatement.executeQuery();
while (res.next()) {
if(start>0){
start--;
}else {
Hashtable<String, String> hash = new Hashtable<String, String>();
hash.put("id", String.valueOf(res.getInt("id")));
hash.put("username", res.getString("username"));
hash.put("name", res.getString("name"));
hash.put("sex", res.getString("sex"));
hash.put("mobile", res.getString("mobile"));
hash.put("email", res.getString("email"));
hash.put("qq", res.getString("qq"));
hash.put("company", res.getString("company"));
hash.put("address", res.getString("address"));
hash.put("postcode", res.getString("postcode"));
list.add(hash);
}
}
request.setAttribute("list", list);
} catch (SQLException sqlE) {
sqlE.printStackTrace();
} finally {
ConnectionManager.closeResultSet(res);
ConnectionManager.closeStatement(pStatement);
ConnectionManager.closeConnection(dbConnection);
}
return flag;
}
/**
*
* 删除通信录
* */
public boolean delete(HttpServletRequest request, String username) {
Connection dbConnection = null;
PreparedStatement pStatement = null;
try {
dbConnection = ConnectionManager.getConnection();
String id = request.getParameter("id");
String sql = "delete from address where id=" + id;
pStatement = dbConnection.prepareStatement(sql);
pStatement.executeUpdate();
} catch (SQLException sqlE) {
sqlE.printStackTrace();
} finally {
ConnectionManager.closeStatement(pStatement);
ConnectionManager.closeConnection(dbConnection);
}
return true;
}
/**
*
* 插入通信录
* */
public boolean insert(HttpServletRequest request, String username) {
Connection dbConnection = null;
PreparedStatement pStatement = null;
try {
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String mobile = request.getParameter("mobile");
String email = request.getParameter("email");
String qq = request.getParameter("qq");
String company = request.getParameter("company");
String address = request.getParameter("address");
String postcode = request.getParameter("postcode");
dbConnection = ConnectionManager.getConnection();
String sql = "insert into address (username, name, sex, mobile, email, qq, company, address, postcode) ";
sql += " values('" + username + "','" + name + "','" + sex + "','"
+ mobile + "','" + email + "','" + qq + "','" + company
+ "','" + address + "','" + postcode + "')";
pStatement = dbConnection.prepareStatement(sql);
pStatement.executeUpdate();
} catch (SQLException sqlE) {
sqlE.printStackTrace();
} finally {
ConnectionManager.closeStatement(pStatement);
ConnectionManager.closeConnection(dbConnection);
}
return true;
}
/**
*
* 返回单条通信录
* */
public boolean select(HttpServletRequest request, String username) {
Connection dbConnection = null;
PreparedStatement pStatement = null;
ResultSet res = null;
try {
String id = request.getParameter("id");
dbConnection = ConnectionManager.getConnection();
String sql = "select * from address where id=" + id;
pStatement = dbConnection.prepareStatement(sql);
res = pStatement.executeQuery();
while (res.next()){
request.setAttribute("id", res.getString("id"));
request.setAttribute("username", res.getString("username"));
request.setAttribute("name", res.getString("name"));
request.setAttribute("sex", res.getString("sex"));
request.setAttribute("mobile", res.getString("mobile"));
request.setAttribute("email", res.getString("email"));
request.setAttribute("qq", res.getString("qq"));
request.setAttribute("company", res.getString("company"));
request.setAttribute("address", res.getString("address"));
request.setAttribute("postcode", res.getString("postcode"));
}
} catch (SQLException sqlE) {
sqlE.printStackTrace();
} finally {
ConnectionManager.closeResultSet(res);
ConnectionManager.closeStatement(pStatement);
ConnectionManager.closeConnection(dbConnection);
}
return true;
}
/**
*
* 修改通信录
* */
public boolean update(HttpServletRequest request, String username) {
Connection dbConnection = null;
PreparedStatement pStatement = null;
try {
String id = request.getParameter("id");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String mobile = request.getParameter("mobile");
String email = request.getParameter("email");
String qq = request.getParameter("qq");
String company = request.getParameter("company");
String address = request.getParameter("address");
String postcode = request.getParameter("postcode");
dbConnection = ConnectionManager.getConnection();
String sql = "update address set name='" + name + "', sex='" + sex
+ "', mobile='" + mobile + "', email='" + email + "', qq='"
+ qq + "', company='" + company + "', address='" + address
+ "', postcode='" + postcode + "' where id=" + id;
pStatement = dbConnection.prepareStatement(sql);
pStatement.executeUpdate();
} catch (SQLException sqlE) {
sqlE.printStackTrace();
} finally {
ConnectionManager.closeStatement(pStatement);
ConnectionManager.closeConnection(dbConnection);
}
return true;
}
}