package com.pz.dao;
import java.sql.*;
import java.util.*;
import com.pz.bean.User;
import com.pz.business.user.UserActionForm;
import com.pz.util.Database;
public class UserDAO {
private Connection con=null;
public UserDAO(){
try {
con = Database.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 分頁相關代碼
*/
private int rowCount; //總行數
private int pageCount; //總頁數
private int length; //限定每頁顯示長度 (10行)
private String pagestr; //帶有超鏈接的頁數,.... (一個字符串)
private String conditionStr = ""; //翻頁時, 保存相同的查詢條件在conditionStr中
private String witchAction="";
public int getLength() {
return (this.length);
}
public void setLength(int length) {
this.length = length;
}
public String getWitchAction() {
return witchAction;
}
public void setWitchAction(String witchAction) {
this.witchAction = witchAction;
}
public void setConditionStr(String conditionStr) {
this.conditionStr = conditionStr;
}
public String getConditionStr() {
return conditionStr;
}
/**
* 分頁中下標字符串的獲得
* @param ipage
* @return
*/
public String getPagestr(int ipage) {
String strPage = "";
if (getLength() > 0) {
strPage += "共";
strPage += String.valueOf(rowCount);
strPage += "條記錄,共";
strPage += String.valueOf(pageCount);
strPage += "頁,當前是第";
strPage += String.valueOf(ipage);
strPage += "頁, ";
int istart, iend; //能看到的頁數 (如: 5,6,7,8,9)
istart = ipage - 5; //如果當前頁爲7, 則開始頁爲 2
if (istart < 0) {
istart = 0; //如果開始頁被計算出來小於0,就從0+1頁開始。 for...
}
iend = istart + 10; //總共能顯示10頁
if (iend > pageCount) {
iend = pageCount; //如果最後一頁計算出來,超過了總頁數,就把總頁數設爲最後一頁。
}
istart = iend - 10; //防止結束頁不是計算出來的頁,而是指定的最後頁 這是需要重設置開始頁
if (istart < 0) {
istart = 0;
}
for (int i = istart; i < iend; i++) { //從開始頁到結束頁,分別設置超鏈接
strPage +=
"<a href='"+witchAction+"?page="; //witchAction是指定在超鏈接中的Action
strPage += String.valueOf(i + 1);
strPage += conditionStr;
strPage += "'>";
strPage += String.valueOf(i + 1);
strPage += "</a>";
strPage += " ";
}
}
this.pagestr = strPage; //帶有超鏈接的頁數,...(一個字符串) 獲得後同時賦值給: ExamineeDAO private String pagestr;
return strPage;
}
/**
* 分頁查詢
* @param user_arg
* @param ipage
* @return
* @throws SQLException
*/ // 頁碼
public Collection paginationSearch(User user_arg, int ipage) throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
Collection list = new ArrayList();
String user_name=user_arg.getUser_name();
String user_dc = user_arg.getUser_dc();
String user_bm = user_arg.getUser_bm();
String user_qx = user_arg.getUser_qx();
String sqlstr=null;
sqlstr="select * from t_user where 1=1 ";
if (user_name!=null&&!user_name.equals("")){ //當查詢條件中用戶名有輸入時
sqlstr=sqlstr+"and user_name like '"+user_name+"%' "; //SQL語句頁加上這條件
conditionStr += ("&user_name=" + user_name); //超鏈接中的條件也加上
}
if (user_dc!=null&&!user_dc.equals("")){
sqlstr=sqlstr+"and user_dc = '"+user_dc+"' ";
conditionStr += ("&user_dc=" + user_dc);
}
if (user_bm!=null&&!user_bm.equals("")){
sqlstr=sqlstr+"and user_bm = '"+user_bm+"' ";
conditionStr += ("&user_bm=" + user_bm);
}
if (user_qx!=null&&!user_qx.equals("")){
sqlstr=sqlstr+"and user_qx = '"+user_qx+"' ";
conditionStr += ("&user_qx=" + user_qx);
}
sqlstr = sqlstr + "ORDER by user_bm";
try {
ps = con.prepareStatement(sqlstr,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs=ps.executeQuery();
if (false == rs.last()) { //如果結果集指針到了最後一行
rowCount = 0;
pageCount = 0;
ipage = 0;
return list;
}
this.rowCount = rs.getRow(); // private int rowCount; 檢索當前行編號。 (查詢後的最後一行)
int offset = 1;
int pagesize = getLength(); //examineeDAO.setLength(100); //ExamineeDAO屬性:private int length; (每頁長度)
if (getLength() < 1) {
pagesize = rowCount;
pageCount = 1;
}
else { //得到 總頁數 ---總行數/每頁行數 + ( (總行數%每頁行數 ) > 0 ? 1 : 0) //整除後 有餘的部分就 + 1
pageCount = rowCount / getLength() + ( (rowCount % getLength()) > 0 ? 1 : 0);
offset = (ipage - 1) * getLength() + 1; //記錄集指針定位
if (offset < 1) {
offset = 1;
}
if (offset > rowCount) {
offset = rowCount;
}
}
rs.absolute(offset);//記錄集指針定位
//每頁限定數 並 //總行數
for (int i = 0; i < pagesize && offset < rowCount + 1; i++, offset++) {
user = new User();
user.setUser_id(rs.getInt("user_id"));
user.setUser_name(rs.getString("user_name"));
user.setUser_dc(rs.getString("user_dc"));
user.setUser_pw(rs.getString("user_pw"));
user.setUser_bm(rs.getString("user_bm"));
user.setUser_qx(rs.getString("user_qx"));
rs.next();
list.add(user);
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
rs = null;
ps.close();
ps = null;
con.close();
con = null;
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
return list;
}
/**
* 添加用戶
* @param user
*/
public void addUser(User user){
PreparedStatement ps=null;
try {
ps = con.prepareStatement("insert into t_user (user_name,user_dc,user_bm,user_pw,user_qx) values (?,?,?,?,?)");
ps.setString(1, user.getUser_name());
ps.setString(2, user.getUser_dc());
ps.setString(3, user.getUser_bm());
ps.setString(4, user.getUser_pw());
ps.setString(5, user.getUser_qx());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close();
ps = null;
con.close();
con=null;
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 修改用戶
* @param user
*/
public void editUser(User user){
PreparedStatement ps=null;
try {
ps = con
.prepareStatement("update t_user set user_name=?,user_dc=?,user_bm=?,user_qx=? where user_id=?");
ps.setString(1, user.getUser_name());
ps.setString(2, user.getUser_dc());
ps.setString(3, user.getUser_bm());
ps.setString(4, user.getUser_qx());
ps.setLong(5, user.getUser_id());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
try {
ps.close();
ps = null;
con.close();
con = null;
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 單個用戶的刪除
* @param id
*/
public void deleteUserByKey(int id){
PreparedStatement ps=null;
try {
ps=con.prepareStatement("delete from t_user where user_id=?");
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close();
ps = null;
con.close();
con = null;
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 多個用戶的刪除
* @param userlist
* @throws SQLException
*/
public void deleteListUser(String [] userlist) throws SQLException{
String ids="";
for(int i=0;i<userlist.length;i++){
ids+="'"+userlist[i]+"'";
if (i<userlist.length-1){
ids+=",";
}
}
Statement st=null; //delete from t_user where user_id in ('44','45') ....
try {
st = con.createStatement();
st.executeUpdate("delete from t_user where user_id in ("+ids+")");
} catch (SQLException e) {
e.printStackTrace();
} finally{
con.close();
con=null;
st.close();
st=null;
}
}
/**
* 根據用戶ID得到用戶信息
* @return
*/
public User getUserByKey(int id){
PreparedStatement ps=null;
ResultSet rs=null;
User user=new User();
try {
ps=con.prepareStatement("select * from t_user where user_id=?");
ps.setInt(1, id);
rs=ps.executeQuery();
while(rs.next()){
user.setUser_id(rs.getInt("user_id"));
user.setUser_name(rs.getString("user_name"));
user.setUser_dc(rs.getString("user_dc"));
user.setUser_bm(rs.getString("user_bm"));
user.setUser_pw(rs.getString("user_pw"));
user.setUser_qx(rs.getString("user_qx"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
rs=null;
ps.close();
ps = null;
con.close();
con = null;
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return user;
}
/**
* 判斷用戶是否存在
* @return
*/
public boolean isLogin(String name,String password){
boolean result=false;
String pw=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps=con.prepareStatement("select user_pw from t_user where user_name=?");
ps.setString(1, name);
rs=ps.executeQuery();
while(rs.next()){
pw=rs.getString(1);
}
if ((pw!=null)&&pw.trim().equals(password)){
result=true;
}else{
result=false;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (rs!=null){
rs.close();
rs=null;
}
if (ps!=null) {
ps.close();
ps = null;
}
if (con!=null) {
con.close();
con = null;
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return result;
}
}