CSDN博客

img kui

如何在Struts 数据库应用程序中实现记录的删除、更新及链接(续)

发表于2004/6/15 10:55:00  1629人阅读

如何在Struts 数据库应用程序中实现记录的删除、更新及链接(续)

《如何在Struts 数据库应用程序中实现记录的删除、更新及链接》http://dev.csdn.net/develop/article/29/29219.shtm

 

 

dbListDAO.java文件:

  这里定义一个DAO类,用于实现数据加的查找、删除、更新、添加及生成分页字符串等功能。getPagestr(int ipage)函数用于生成分页字符串,int ipage参数用于转递当前页码。findSQL(String sql,int ipage)函数用于查找数据库数据,String sql参数用于转递SQL查询语句字符串,int ipage参数用于转递当前页码。

。。。。。。。。。。(省略)

public class dbListDAO {

 

     记录更新、删除和添加无关的代码省略。

     public void create(dbList m_dbList) throws SQLException {

    PreparedStatement ps = null;

    //<createSQL>

    String sql = "INSERT INTO dbo.Test VALUES (?,?,?)";

    //</createSQL>

    try {

      if (con.isClosed()) {

        throw new IllegalStateException("error.unexpected");

      }

       //SQL Server请加入下行:

      con.setAutoCommit(true);

      //SQL Server

      ps = con.prepareStatement(sql);

      //<create>

      ps.setInt(1,m_dbList.get序号());

      ps.setString(2,m_dbList.get姓名());

      ps.setString(3,m_dbList.get备注());

      //</create>

 

      if (ps.executeUpdate() != 1) {

        throw new SQLException ("error.create.dbList");

      }

    } 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(dbList m_dbList,String keyID) {

    PreparedStatement ps = null;

    //<updateSQL>

   String sql = "UPDATE dbo.Test SET 序号 = ?,姓名 = ?,备注 = ? WHERE 序号 = ?";

      //</updateSQL>

    try {

      if (con.isClosed()) {

        throw new IllegalStateException("error.unexpected");

      }

       //SQL Server请加入下行:

      con.setAutoCommit(true);

      //SQL Server

      ps = con.prepareStatement(sql);

      //<update>

      ps.setInt(1,m_dbList.get序号());

      ps.setString(2,m_dbList.get姓名());

      ps.setString(3,m_dbList.get备注());

      //</update>

 

      ///////////////

      //<updatekeyID>

      ps.setInt(4,java.lang.Integer.parseInt(keyID,10));

          //</updatekeyID>

 

 

      if (ps.executeUpdate() != 1) {

        throw new SQLException (

          "error.removed.dbList");

      }

 

    } 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.Test WHERE ";

    sql+="序号";

    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.setInt(1,java.lang.Integer.parseInt(keyID,10));

          //</setremovekeyIDdata>

 

 

  

      if (ps.executeUpdate() != 1) {

        throw new SQLException (

          "error.removed.dbList");

      }

 

    } 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");

      }

    }

  }

 

  与记录更新、删除和添加无关的代码省略。

 

}

 

dbListAction.java文件:

。。。。。。。。。。(省略)

public final class dbListAction extends Action

{

    // 变量定义:

    private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");

 

    private ConnectionPool pool;

 

    public dbListAction() {

        pool = ConnectionPool.getInstance();

    }

 

 

    //函数定义:

    public ActionForward execute(ActionMapping mapping,

                             ActionForm form,

                             HttpServletRequest request,

                             HttpServletResponse response)

       throws Exception

    {

       // Extract attributes and parameters we will need

       Locale locale = getLocale(request);

       MessageResources messages = getResources(request);

       HttpSession session = request.getSession();

       dbListForm m_dbListForm = (dbListForm) form;

      

           Connection con = null;

 

     try

        {

                 con = pool.getConnection();

                 dbListDAO m_dbListDAO = new dbListDAO(con);  //定义DAO对象,用于实现数据库的各种操作

 

                 String action=request.getParameter("action");  //动作类型                 

                 String search=request.getParameter("search");  //数据库查找方式

                 String expression=request.getParameter("expression");    //参数,这里用于保存记录(行)的ID数据

                

                 if(action==null)action="find";

              if(search==null)search="UNsearch";

              if(expression==null)expression="";

              expression=toChinese(expression);  //处理中文问题,实现编码转换

              

              if("delete".equals(action))    //删除记录操作

            {

                m_dbListDAO.removeID(expression);

            }

            ////////////

            if("update".equals(action)||"insert".equals(action))    //更新、添加记录操作

            {

                 dbList m_dbList= new dbList();

                  //<update>

                  m_dbList.set序号(m_dbListForm.get序号());

                  m_dbList.set姓名(m_dbListForm.get姓名());

                  m_dbList.set备注(m_dbListForm.get备注());

                  //</update>

                   if("update".equals(action))    //调用DAO对象更新记录

                         m_dbListDAO.update(m_dbList,expression);

                   if("insert".equals(action))    //调用DAO对象添加记录

                         m_dbListDAO.create(m_dbList);

            }

                 ///////////////查找数据库

                 //<sqlstr>

                 String sql="SELECT * FROM dbo.Test";

                 //</sqlstr>

 

              ////SQL字符处理

                 if("search".equals(search))

                 {

                     //<search>

                        sql+=" where ";

                     sql+="序号";

                     sql+="=";

                     if("NULL".equals(expression)||"".equals(expression))

                    {

                            sql+=m_dbListForm .get序号();

                        }

                        else

                        {

                            sql+=expression;

                        }

                        

                        sql+="";

                     //</search>

                 }

 

             //<分页>

     (略)  

      //</分页>

 

 

                 return mapping.findForward("success");

     }

       catch (SQLException e)

        {

             (略) 

    }

 }

0 0

相关博文

我的热门文章

img
取 消
img