码迷,mamicode.com
首页 > 数据库 > 详细

保存 http request 的数据到数据库表

时间:2015-09-17 14:54:18      阅读:495      评论:0      收藏:0      [点我收藏+]

标签:

开发需求:把 http request 对象的数据保存到数据库中

第一步:编写 RequestInfoService 类,保存方法名是 saveRequestInfo

    // 保存request信息
    public void saveRequestInfo(HttpServletRequest request){
        Connection conn = null;
        PreparedStatement pstmt = null;    
        
        BaseDao baseDao = new BaseDao();
        try {
            conn = baseDao.dbConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.setLength(0);
        
        sqlBf.append("INSERT INTO REQUEST_INFO (REQUEST_INFO_SEQ               \n");
        sqlBf.append("                        , CHARACTER_ENCODING             \n");
        sqlBf.append("                        , CONTENT_TYPE                   \n");
        sqlBf.append("                        , CONTEXT_PATH                   \n");
        sqlBf.append("                        , LOCAL_ADDR                     \n");
        sqlBf.append("                        , LOCAL_NAME                     \n");
        sqlBf.append("                        , LOCAL_PORT                     \n");
        sqlBf.append("                        , METHOD                         \n");
        sqlBf.append("                        , REMOTE_ADDR                    \n");
        sqlBf.append("                        , REMOTE_HOST                    \n");
        sqlBf.append("                        , REMOTE_PORT                    \n");
        sqlBf.append("                        , REMOTE_USER                    \n");
        sqlBf.append("                        , REQUEST_URI                    \n");
        sqlBf.append("                        , REQUESTED_SESSION_ID           \n");
        sqlBf.append("                        , LOCALE                         \n");
        sqlBf.append("                        , REGI_DT)                       \n");
        sqlBf.append("VALUES(SEQ_REQUEST_INFO.NEXTVAL                          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");
        sqlBf.append("     , ?          \n");        
        sqlBf.append("     , SYSDATE)   \n");

        System.out.println(sqlBf.toString());
        
        try {
            pstmt = conn.prepareStatement(sqlBf.toString());
            idx = 1;
            pstmt.setString(idx++, request.getCharacterEncoding());
            pstmt.setString(idx++, request.getContentType());
            pstmt.setString(idx++, request.getContextPath());
            pstmt.setString(idx++, request.getLocalAddr());
            pstmt.setString(idx++, request.getLocalName());
            pstmt.setInt(idx++, request.getLocalPort());
            pstmt.setString(idx++, request.getMethod());
            pstmt.setString(idx++, request.getRemoteAddr());
            pstmt.setString(idx++, request.getRemoteHost());
            pstmt.setInt(idx++, request.getRemotePort());
            pstmt.setString(idx++, request.getRemoteUser());
            pstmt.setString(idx++, request.getRequestURI());
            pstmt.setString(idx++, request.getRequestedSessionId());
            pstmt.setString(idx++, request.getLocale().toString());
            
            int i = pstmt.executeUpdate();
            if (i == 1) {
                System.out.println("##### save request success \n");
            } else {
                System.out.println("##### save request fail \n");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        try {
            baseDao.dbDisconnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }        
    }

第二步:Oracle 建表语句

CREATE TABLE SCOTT.REQUEST_INFO 
(
    REQUEST_INFO_SEQ       NUMBER NOT NULL,
    CHARACTER_ENCODING     VARCHAR2 (10),
    CONTENT_TYPE           VARCHAR2 (10),
    CONTEXT_PATH           VARCHAR2 (50),
    LOCAL_ADDR             VARCHAR2 (50),
    LOCAL_NAME             VARCHAR2 (50),
    LOCAL_PORT             NUMBER,
    METHOD                 VARCHAR2 (10),
    REMOTE_ADDR            VARCHAR2 (20),
    REMOTE_HOST            VARCHAR2 (20),
    REMOTE_PORT            NUMBER,
    REMOTE_USER            VARCHAR2 (20),
    REGI_DT                DATE,
    REQUEST_URI            VARCHAR2 (50),
    REQUESTED_SESSION_ID   VARCHAR2 (100),
    LOCALE                 VARCHAR2 (20)
)

ALTER TABLE SCOTT.REQUEST_INFO ADD(
    CONSTRAINT PK_REQUEST_INFO_SEQ PRIMARY KEY (REQUEST_INFO_SEQ));

第三步:在访问页面的servlet中调用此service

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        RequestInfoService ris = new RequestInfoService();
        ris.saveRequestInfo(request);
        
        EmpService es = new EmpService();
        EmpBean eb = new EmpBean();
        
        eb.setEname(request.getParameter("searchTxt"));
        ArrayList<EmpBean> empBean = es.getEmpList(eb);
        
        request.setAttribute("empBean", empBean);
        request.getRequestDispatcher("/view/empList.jsp").forward(request, response);
    }

 第四步:测试

访问 http://localhost:8081/web01/view/empList.jsp 页面进行测试。正常时每次点击 search 按钮,插入一条记录到数据库中

保存 http request 的数据到数据库表

标签:

原文地址:http://www.cnblogs.com/seabird1979/p/4816122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!