package com.accp.t02.bean;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import com.accp.t02.dao.CommonDBCon;
import com.accp.t02.form.UserForm;
public class UserBean extends CommonDBCon {
private UserForm userForm = null;
public UserBean(Connection connection) throws SQLException {
super(connection);
}
public ArrayList getUserList(int currentPage) {
ArrayList arrayList = new ArrayList();
int maxRecordNum = 10;
try {
rs = st.executeQuery("select top " + maxRecordNum
+ " * from users where userid not in(select top "
+ (currentPage - 1) * maxRecordNum + " userid from users)");
UserForm userForm = null;
while (rs.next()) {
userForm = new UserForm();
userForm.setUserid(rs.getString("userid"));
userForm.setPassword(rs.getString("password"));
userForm.setUsername(rs.getString("username"));
userForm.setUserright(rs.getString("userright"));
userForm.setName(rs.getString("name"));
userForm.setSeat(rs.getString("seat"));
userForm.setTelphone(rs.getString("telphone"));
userForm.setEmail(rs.getString("email"));
userForm.setIntroduce(rs.getString("introduce"));
userForm.setArea(rs.getString("area"));
arrayList.add(userForm);
}
} catch (SQLException e) {
e.printStackTrace();
}
return arrayList;
}
public UserForm getUserInfoByUserid(String userid) {
userForm = new UserForm();
try {
rs = st.executeQuery("select * from users where userid = '"
+ userid + "'");
while (rs.next()) {
userForm.setUserid(rs.getString("userid"));
userForm.setPassword(rs.getString("password"));
userForm.setUsername(rs.getString("username"));
userForm.setUserright(rs.getString("userright"));
userForm.setName(rs.getString("name"));
userForm.setSeat(rs.getString("seat"));
userForm.setTelphone(rs.getString("telphone"));
userForm.setEmail(rs.getString("email"));
userForm.setIntroduce(rs.getString("introduce"));
userForm.setArea(rs.getString("area"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return userForm;
}
public ArrayList updUserInfoByUserid(UserForm userForm) {
ArrayList arrayList = null;
int upd_success = 0;
try {
pstmt = conn
.prepareStatement("update users set username=?,userright=?,name=?,seat=?,telphone=?,email=?,introduce=?,area=? where userid='"
+ userForm.getUserid() + "'");
pstmt.setString(1, userForm.getUsername());
pstmt.setString(2, userForm.getUserright());
pstmt.setString(3, userForm.getName());
pstmt.setString(4, userForm.getSeat());
pstmt.setString(5, userForm.getTelphone());
pstmt.setString(6, userForm.getEmail());
pstmt.setString(7, userForm.getIntroduce());
pstmt.setString(8, userForm.getArea());
upd_success = pstmt.executeUpdate();
if (upd_success > 0) {
arrayList = this.getUserList(userForm.getCurrentPage());
}
} catch (SQLException e) {
e.printStackTrace();
}
return arrayList;
}
public ArrayList delUserInfoByUserid(String currentPage, String userid) {
ArrayList arrayList = null;
int del_success = 0;
try {
pstmt = conn.prepareStatement("delete from users where userid in("
+ userid + ")");
del_success = pstmt.executeUpdate();
if (del_success > 0) {
arrayList = this.getUserList(Integer.parseInt(currentPage));
}
} catch (SQLException e) {
e.printStackTrace();
}
return arrayList;
}
public ArrayList addUserInfo(UserForm userForm) {
ArrayList arrayList = null;
int add_success = 0;
try {
pstmt = conn
.prepareStatement("insert into users(username,userright,name,seat,telphone,email,introduce,area)"
+ " values(?,?,?,?,?,?,?,?)");
pstmt.setString(1, userForm.getUsername());
pstmt.setString(2, userForm.getUserright());
pstmt.setString(3, userForm.getName());
pstmt.setString(4, userForm.getSeat());
pstmt.setString(5, userForm.getTelphone());
pstmt.setString(6, userForm.getEmail());
pstmt.setString(7, userForm.getIntroduce());
pstmt.setString(8, userForm.getArea());
add_success = pstmt.executeUpdate();
if (add_success > 0) {
arrayList = this.getUserList(userForm.getCurrentPage());
}
} catch (SQLException e) {
e.printStackTrace();
}
return arrayList;
}
public ArrayList findUserInfo(UserForm userForm){
ArrayList arrayList = new ArrayList();
int maxRecordNum = 10;
int maxPage = 1;
String sqlWhere = "";
if(!userForm.getUsername().equals("")){
sqlWhere = sqlWhere + " and username like '%" + userForm.getUsername() + "%'";
}
if(!userForm.getUserright().equals("")){
sqlWhere = sqlWhere + " and userright like '%" + userForm.getUserright() + "%'";
}
if(!userForm.getName().equals("")){
sqlWhere = sqlWhere + " and username like '%" + userForm.getName() + "%'";
}
if(!userForm.getSeat().equals("")){
sqlWhere = sqlWhere + " and username like '%" + userForm.getSeat() + "%'";
}
if(!userForm.getTelphone().equals("")){
sqlWhere = sqlWhere + " and username like '%" + userForm.getTelphone() + "%'";
}
if(!userForm.getEmail().equals("")){
sqlWhere = sqlWhere + " and username like '%" + userForm.getEmail() + "%'";
}
if(!userForm.getArea().equals("")){
sqlWhere = sqlWhere + " and username like '%" + userForm.getArea() + "%'";
}
if(sqlWhere.length()>1){
sqlWhere = "where " + sqlWhere.substring(4);
}
try {
/*rs = st.executeQuery("select top " + maxRecordNum
+ " * from users where userid not in(select top "
+ (userForm.getCurrentPage() - 1) * maxRecordNum + " userid from users)"
+ sqlWhere);*/
rs = st.executeQuery("select * from users " + sqlWhere);
UserForm userF = null;
while (rs.next()) {
userF = new UserForm();
userF.setUserid(rs.getString("userid"));
userF.setPassword(rs.getString("password"));
userF.setUsername(rs.getString("username"));
userF.setUserright(rs.getString("userright"));
userF.setName(rs.getString("name"));
userF.setSeat(rs.getString("seat"));
userF.setTelphone(rs.getString("telphone"));
userF.setEmail(rs.getString("email"));
userF.setIntroduce(rs.getString("introduce"));
userF.setArea(rs.getString("area"));
arrayList.add(userF);
}
/*rs = st.executeQuery("select count(*) from users");
if (rs.next()) {
if (Integer.parseInt(rs.getString(1)) > 1) {
maxPage = (Integer.parseInt(rs.getString(1)) + 9) / 10;
userForm.setMaxPage(maxPage);
}
}*/
} catch (SQLException e) {
e.printStackTrace();
}
return arrayList;
}
public int getMaxPage() {
int maxPage = 1;
try {
rs = st.executeQuery("select count(*) from users");
if (rs.next()) {
if (Integer.parseInt(rs.getString(1)) > 1) {
maxPage = (Integer.parseInt(rs.getString(1)) + 9) / 10;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return maxPage;
}
}
评论0