《Struts开发实例》
ftp://61.155.65.187/upload/
用户:lhbup
密码:lhbup
--------------------
http://dev.csdn.net/user/kui
《如何在Struts 数据库应用程序中实现记录的删除、更新及链接 》______________________
UserEditDAO.java:
package  emptyprj;
import emptyprj.UserEdit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import java.util.Collection;
import java.util.ArrayList;public class UserEditDAO {    private Connection con;
    private int rowCount;
    private int pageCount;
    private int length;      
    private String pagestr;
    public int getLength()  {  return (this.length);   }
    public void setLength(int length)  { this.length=length; }  
    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;
            istart=ipage-5;
            if(istart<0) {istart=0;}
            iend=istart+10;
            if(iend>pageCount) {iend=pageCount;}
            istart=iend-10;
            if(istart<0) {istart=0;}
            for(int i=istart;i<iend;i++)
            {
                strPage+="<a href='UserEditAction.do?action=find&search=search&page=";
                strPage+=String.valueOf(i+1);
                strPage+="'>";
                strPage+=String.valueOf(i+1);
                strPage+="</a>"; 
                strPage+="  ";
            }
       }
       this.pagestr=strPage;
       return strPage;
  }  public UserEditDAO(Connection con) {
    this.con = con;
  }  public void create(UserEdit m_UserEdit) throws SQLException {
    PreparedStatement ps = null;
    //<createSQL>
    String sql = "INSERT INTO dbo.users VALUES (?,?,?)";
    //</createSQL>
    try {
      if (con.isClosed()) {
        throw new IllegalStateException("error.unexpected");
      }
       //用SQL Server请加入下行: 
      //con.setAutoCommit(true);
      //SQL Server
      ps = con.prepareStatement(sql);
      //<create>
      ps.setString(1,m_UserEdit.getUsername());
      ps.setString(2,m_UserEdit.getPassword());
      ps.setString(3,m_UserEdit.getRole());
      //</create>      if (ps.executeUpdate() != 1) {
        throw new SQLException ("error.create.UserEdit");
      }
    } catch (SQLException e) {
     
        e.printStackTrace();
        throw new RuntimeException("error.unexpected");
      
    } finally {
      try {
        if (ps != null)
          ps.close();
      } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("error.unexpected");
      }
    }
  }  public void update(UserEdit m_UserEdit,String keyID) {
    PreparedStatement ps = null;
    //<updateSQL>
   String sql = "UPDATE dbo.users SET username = ?,password = ?,role = ? WHERE username = ?";
      //</updateSQL>
    try {
      if (con.isClosed()) {
        throw new IllegalStateException("error.unexpected");
      }
       //用SQL Server请加入下行: 
      //con.setAutoCommit(true);
      //SQL Server
      ps = con.prepareStatement(sql);
      //<update>
      ps.setString(1,m_UserEdit.getUsername());
      ps.setString(2,m_UserEdit.getPassword());
      ps.setString(3,m_UserEdit.getRole());
      //</update>      ///////////////
      //<updatekeyID>
      ps.setString(4,keyID);
          //</updatekeyID>
      if (ps.executeUpdate() != 1) {
        throw new SQLException (
          "error.removed.UserEdit");
      }    } catch (SQLException e) {
      e.printStackTrace();
      throw new RuntimeException("error.unexpected");
    } finally {
      try {
        if (ps != null)
          ps.close();
      } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("error.unexpected");
      }
    }
  }
 
 public void removeID(String keyID) {
   
    //<removeIDSQL>
    String sql="DELETE FROM dbo.users WHERE ";
    sql+="username";
    sql+=" = ?";
    //</removeIDSQL>    PreparedStatement ps = null;
    try {      if (con.isClosed()) {
        throw new IllegalStateException("error.unexpected");
      }
       //用SQL Server请加入下行: 
      //con.setAutoCommit(true);
      //SQL Server
      ps = con.prepareStatement(sql);
      
      //<setremovekeyIDdata>
      ps.setString(1,keyID);
          //</setremovekeyIDdata>
   
      if (ps.executeUpdate() != 1) {
        throw new SQLException (
          "error.removed.UserEdit");
      }    } catch (SQLException e) {
      e.printStackTrace();
      throw new RuntimeException("error.unexpected");
    } finally {
      try {
        if (ps != null)
          ps.close();
      } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("error.unexpected");
      }
    }
  }
  public Collection findSQL(String sql,int ipage) {
    PreparedStatement ps = null;
    ResultSet rs = null;
    ArrayList list = new ArrayList();    try {
      if (con.isClosed()) {
        throw new IllegalStateException("error.unexpected");
      }
      ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
      rs = ps.executeQuery();
          rs.absolute(-1);
      rowCount=rs.getRow();
     
      int offset=1;
      int pagesize=getLength();
      if(getLength()<1)
      {
          pagesize=rowCount;
          pageCount=1;
      }
      else
      {
          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++) {           
        UserEdit m_UserEdit= new UserEdit(); 
        //<find>
      m_UserEdit.setUsername(rs.getString(1));
      m_UserEdit.setPassword(rs.getString(2));
      m_UserEdit.setRole(rs.getString(3));
      //</find>        rs.next();
        list.add(m_UserEdit);      }      return list;    } catch (SQLException e) {
      //e.printStackTrace();
      //throw new RuntimeException("error.unexpected");
       return list;
    } finally {
      try {
        if (ps != null)
          ps.close();
        if (rs != null)
          rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("error.unexpected");
      }
    }
  }}