zoukankan      html  css  js  c++  java
  • 保存 http request 的数据到数据库表

    开发需求:把 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               
    ");
            sqlBf.append("                        , CHARACTER_ENCODING             
    ");
            sqlBf.append("                        , CONTENT_TYPE                   
    ");
            sqlBf.append("                        , CONTEXT_PATH                   
    ");
            sqlBf.append("                        , LOCAL_ADDR                     
    ");
            sqlBf.append("                        , LOCAL_NAME                     
    ");
            sqlBf.append("                        , LOCAL_PORT                     
    ");
            sqlBf.append("                        , METHOD                         
    ");
            sqlBf.append("                        , REMOTE_ADDR                    
    ");
            sqlBf.append("                        , REMOTE_HOST                    
    ");
            sqlBf.append("                        , REMOTE_PORT                    
    ");
            sqlBf.append("                        , REMOTE_USER                    
    ");
            sqlBf.append("                        , REQUEST_URI                    
    ");
            sqlBf.append("                        , REQUESTED_SESSION_ID           
    ");
            sqlBf.append("                        , LOCALE                         
    ");
            sqlBf.append("                        , REGI_DT)                       
    ");
            sqlBf.append("VALUES(SEQ_REQUEST_INFO.NEXTVAL                          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");
            sqlBf.append("     , ?          
    ");        
            sqlBf.append("     , SYSDATE)   
    ");
    
            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 
    ");
                } else {
                    System.out.println("##### save request fail 
    ");
                }
            } 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 按钮,插入一条记录到数据库中

  • 相关阅读:
    python3 sorted()函数解析
    MySql 关键字
    python的 a,b=b,a+b 和 a=b b=a+b 的区别
    python3 all() 函数用于检查实参
    Python3 urllib模块
    Python3 shutil模块
    Python3 sys模块
    Python 异常处理和断言
    Python3 os模块
    Pytho3 file open方法
  • 原文地址:https://www.cnblogs.com/seabird1979/p/4816122.html
Copyright © 2011-2022 走看看