package com.dinglin.service;
import java.util.ArrayList;
import com.dinglin.javabean.UserBean;
import com.dinglin.model.Dao;
public class UserService extends Dao {
 ArrayList<UserBean> al = null;
 public String login(String username, String password) {
  String strUserlevel = "";
  try {
   this.lianJie();
   ps = ct.prepareStatement("select userlevel from userinfo where username = ? and password = ?");
   ps.setString(1, username);
   ps.setString(2, password);
   rs = ps.executeQuery();
   if (rs.next()) {
    strUserlevel = rs.getString(1);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   this.guanBi();
  }
  return strUserlevel;
 }
 public int pageCount(int pageSize) {
  int rowCount = 0;
  int pageCount = 0;
  try {
   this.lianJie();
   ps = ct.prepareStatement("select count(*) from userinfo");
   rs = ps.executeQuery();
   if (rs.next()) {
    rowCount = rs.getInt(1);
    pageCount = (rowCount + pageSize - 1) / pageSize;
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   this.guanBi();
  }
  return pageCount;
 }
 public ArrayList<UserBean> userinfoAL(int pageNow, int pageSize) {
  al = new ArrayList<UserBean>();
  try {
   this.lianJie();
   ps = ct.prepareStatement("select * from userinfo limit ?, ?");
   ps.setInt(1, pageSize * pageNow - pageSize);
   ps.setInt(2, pageSize);
   rs = ps.executeQuery();
   while (rs.next()) {
    UserBean ub = new UserBean();
    ub.setUserid(rs.getInt(1));
    ub.setUsername(rs.getString(2));
    ub.setPassword(rs.getString(3));
    al.add(ub);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   this.guanBi();
  }
  return al;
 }
 public int addUserinfo(String username, String password) {
  int i = 0;
  try {
   this.lianJie();
   ps = ct.prepareStatement("insert into userinfo (username, password) values (?, ?)");
   ps.setString(1, username);
   ps.setString(2, password);
   i = ps.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   this.guanBi();
  }
  return i;
 }
 public int addUserinfo(UserBean ub) {
  int i = 0;
  try {
   this.lianJie();
   ps = ct.prepareStatement("insert into userinfo (username, password) values (?, ?)");
   ps.setString(1, ub.getUsername());
   ps.setString(2, ub.getPassword());
   i = ps.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   this.guanBi();
  }
  return i;
 }
 public int editUserinfo(String username, String password, int userid) {
  int i = 0;
  try {
   this.lianJie();
   ps = ct.prepareStatement("update userinfo set username = ?, password = ? where userid = ?");
   ps.setString(1, username);
   ps.setString(2, password);
   ps.setInt(3, userid);
   i = ps.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   this.guanBi();
  }
  return i;
 }
 public int editUserinfo(UserBean ub) {
  int i = 0;
  try {
   this.lianJie();
   ps = ct.prepareStatement("update userinfo set username = ?, password = ? where userid = ?");
   ps.setString(1, ub.getUsername());
   ps.setString(2, ub.getPassword());
   ps.setInt(3, ub.getUserid());
   i = ps.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   this.guanBi();
  }
  return i;
 }
 public int deleteUserinfo(int userid) {
  int i = 0;
  try {
   this.lianJie();
   ps = ct.prepareStatement("delete from userinfo where userid = ?");
   ps.setInt(1, userid);
   i = ps.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   this.guanBi();
  }
  return i;
 }
}

解决方案 »

  1.   

    1.sql最好提取出去,放到同意的配置文件或者java文件都可以
    2.参数设置,提取出去,最好能够写成通用的,避免高耦合
    3.resultset,封装bean,提取出去,写成统一的
    4.e.printStackTrace();改成日志打印
    5. this.lianJie();   this.guanBi();改连接池
    6.  ps = ct.prepareStatement("update userinfo set username = ?, password = ? where userid = ?");
       ps.setString(1, ub.getUsername());
       ps.setString(2, ub.getPassword());
       ps.setInt(3, ub.getUserid());
       i = ps.executeUpdate();
    这些代码重复了很多次,考虑抽象出来===========================
    暂且就这些,如果真想该,就慢慢思考吧