⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 personinfoaction.java

📁 这份代码详细的介绍了如果构建struts的分页 如果使用等等 希望你快点通过
💻 JAVA
字号:
package pagination;

import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.servlet.http.*;
import javax.sql.*;

import org.apache.struts.action.*;

public final class PersoninfoAction extends org.apache.struts.action.Action
{
    private static String SUCCESS = "success";
    private static String INPUT = "input";
    private static String strSQLOrder = " order by sysdate desc";

    public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception
    {
        PersoninfoForm theForm = (PersoninfoForm)form;
        Connection conn = null;
        try
        {
            //数据库连接方式
            //1、直接JDBC过去
            Class.forName("org.gjt.mm.mysql.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/Pagination?autoReconnect=true&useUnicode=true&characterEncoding=GB2312","root","password");

            //2、连接池方式
            //Context initCtx = new InitialContext();
            //Context ctx = (Context)initCtx.lookup("java:comp/env");
            //Object obj = (Object)ctx.lookup("jdbc/Paging");//获取连接池对象
            //DataSource ds = (javax.sql.DataSource)obj;//类型转换
//            Context initCtx = new InitialContext();
//            DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/Pagination");
//            conn = ds.getConnection();

            PersoninfoDAO personInfotDAO = new PersoninfoDAO(conn); //定义DAO对象,用于实现数据库的各种操作
            String method = request.getParameter("method");
            String action = request.getParameter("action");
            String search = request.getParameter("search");
            String name = request.getParameter("name");
            String id = request.getParameter("id");
            name = toChinese(name); //处理中文问题,实现编码转换
            if(method == null)
            {
                method = "search";
            }
            if(search == null)
            {
                search = "search";
            }
            if(name == null)
            {
                name = "";
            }
            if("delete".equals(method)) //删除记录操作
            {
                personInfotDAO.removeID(id);
            }
            if("update".equals(method) || "insert".equals(method)) //更新、添加记录操作
            {
                if("update".equals(method)) //调用DAO对象更新记录
                {
                    if("update".equalsIgnoreCase(action))
                    {
                        Personinfo personInfo = new Personinfo();
                        personInfo.setName(toChinese(theForm.getName()));
                        personInfo.setSex(new Byte(theForm.getSex()).byteValue());
                        personInfo.setMobile(toChinese(theForm.getName()));
                        personInfo.setAddress(toChinese(theForm.getAddress()));
                        personInfo.setMemo(toChinese(theForm.getMemo()));
                        personInfotDAO.update(personInfo,id);
                    }
                    else
                    {
                        Personinfo personInfo = personInfotDAO.findByPrimaryKey(id);
                        theForm.setId(personInfo.getId());
                        theForm.setName(personInfo.getName());
                        theForm.setMobile(personInfo.getMobile());
                        theForm.setAddress(personInfo.getAddress());
                        theForm.setMemo(personInfo.getMemo());

                        theForm.setMethod("update");
                        theForm.setAction("update");
                        request.setAttribute("PersoninfoForm",personInfo);
                        return mapping.findForward(INPUT);
                    }
                }
                if("insert".equals(method)) //调用DAO对象添加记录
                {
                    Personinfo personInfo = new Personinfo();
                    personInfo.setName(toChinese(theForm.getName()));
                    personInfo.setSex(new Byte(theForm.getSex()).byteValue());
                    personInfo.setMobile(toChinese(theForm.getName()));
                    personInfo.setAddress(toChinese(theForm.getAddress()));
                    personInfo.setMemo(toChinese(theForm.getMemo()));
                    //类型要注意
                    java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
                    personInfo.setSysdate(date);
                    personInfotDAO.create(personInfo);
                }
            }
            String sql = "select * from Personinfo";
            if("search".equals(search))
            {
                if(name != null && !"".equals(name) && !"null".equalsIgnoreCase(name))
                {
                    sql += " where name='" + name + "'";
                }
            }
            personInfotDAO.setLength(10);
            int ipage;
            try
            {
                String page = request.getParameter("page");
                ipage = java.lang.Integer.parseInt(page,10);
            }
            catch(Exception e)
            {
                ipage = theForm.getPage();
            }
            if(ipage < 1)
            {
                ipage = 1;
            }
            System.out.println("SQL:" + sql);
            Collection collection = personInfotDAO.findSQL(sql + strSQLOrder,ipage);
            request.setAttribute("Personinfo",collection);
            String pagestr = personInfotDAO.getPagestr(ipage);
            String s_find,str;
            if(!"search".equals(search)) //查找和全部显示两个不同的操作,其分页字符串不同,在此进行处理。如果是全部显示则去掉"search=search&"
            {
                s_find = "search=search&";
                while(pagestr.indexOf(s_find) != -1)
                {
                    str = pagestr.substring(0,pagestr.indexOf(s_find));
                    str += pagestr.substring(pagestr.indexOf(s_find) + s_find.length(),pagestr.length());
                    pagestr = str;
                }
            }
            theForm.setPagestr(pagestr);
            theForm.setAction(method);
            request.setAttribute("PersoninfoForm",theForm);
            return mapping.findForward(SUCCESS);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            throw new RuntimeException("Unable to get connection.");
        }
        finally
        {
            try
            {
                if(conn != null)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                throw new RuntimeException(e.getMessage());
            }
        }
    }

    //处理中文问题,实现编码转换
    public String toChinese(String str)
    {
        if(str != null)
        {
            try
            {
                str = new String(str.getBytes("ISO8859-1"));
            }
            catch(Exception e)
            {
                System.err.println("toChinese exception:" + e.getMessage());
                System.err.println("The String is:" + str);
            }
        }
        return str;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -