在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ Servlet從數(shù)據(jù)庫讀取記錄性能優(yōu)化
Servlet web.xml welcome-file-list
Servlet從數(shù)據(jù)庫讀取記錄性能優(yōu)化
Servlet URL重寫帶參數(shù)
War文件
Web技術(shù)基礎(chǔ)
Servlet GenericServlet類
Servlet API
Servlet ServletInputStream類
使用MyEclipse創(chuàng)建Servlet
Servlet增刪改查
Servlet ServletConfig配置信息
Servlet Cookies
Servlet重定向
Servlet生命周期
Servlet HttpSession會話
Servlet HttpServlet類
Servlet注冊表單示例
Servlet表單隱藏字段
Servlet下載文件
Servlet教程
Servlet身份驗證過濾器
Servlet ServletOutputStream類
Servlet HttpSession登錄注銷實例
Servlet啟動時加載
Servlet事件和監(jiān)聽器
使用Eclipse創(chuàng)建Servlet
Servlet ServletContextEvent事件
Servlet HttpSessionEvent統(tǒng)計在線用戶數(shù)實例
Servlet RequestDispatcher請求轉(zhuǎn)發(fā)
Servlet使用注釋
Servlet過濾器示例
Servlet過慮器
Servlet ServletContext配置信息
Servlet登錄注銷Cookies實例
Servlet工作流程
Servlet會話跟蹤
Servlet登錄實例
Servlet ServletRequest接口
Servlet ServletRequestEvent類和接口
Servlet入門程序
Servlet查詢搜索數(shù)據(jù)示例
Servlet FilterConfig應(yīng)用示例
Servlet顯示所有頭信息
Servlet屬性設(shè)置
使用NetBeans創(chuàng)建Servlet
Servlet接口實現(xiàn)
Servlet上傳文件

Servlet從數(shù)據(jù)庫讀取記錄性能優(yōu)化

提高Servlet從數(shù)據(jù)庫中讀取記錄的性能

在這個例子中,我們將學(xué)習(xí)如何提高Web應(yīng)用程序從數(shù)據(jù)庫中讀取數(shù)據(jù)記錄的性能。要實現(xiàn)這個工作,我們將employess表的數(shù)據(jù)預(yù)先從數(shù)據(jù)庫中讀取出來并存儲在一個集合中,以在servlet中重用這個集合。因此,當(dāng)使用到這個employess表的數(shù)據(jù)時,只需要從ServletContext獲取即可,而不需要連接數(shù)據(jù)庫中查詢表的數(shù)據(jù)記錄。這樣就能提高數(shù)據(jù)的讀取性能。

要運行此應(yīng)用程序,需要創(chuàng)建具有一些記錄的表。完整的SQL語句如下 -

DROP TABLE IF EXISTS `employees`;
CREATE TABLE `employees` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL DEFAULT '',
  `age` int(3) unsigned NOT NULL DEFAULT '0',
  `address` varchar(254) DEFAULT NULL,
  `salary` float(8,2) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of employees
-- ----------------------------
INSERT INTO `employees` VALUES ('1', '李小春', '23', '??谑腥嗣翊蟮?800號', '8900.00');
INSERT INTO `employees` VALUES ('2', '張輝', '28', '廣州天河區(qū)珠村市場', '15800.00');
INSERT INTO `employees` VALUES ('3', '林賢弟', '25', '廣州白云區(qū)龍?zhí)链?20號', '18990.00');

提高servlet從數(shù)據(jù)庫中獲取記錄的性能的示例

在這個例子中,我們創(chuàng)建了6個代碼文件。它們分別如下 -

  1. index.html - 項目首頁
  2. Employees.java - 這是一個簡單的bean類,包含幾個屬性及其gettersetter方法,此類用于表示數(shù)據(jù)庫表:employees。
  3. MyListener.java - 監(jiān)聽器
  4. MyServlet1.java
  5. MyServlet2.java
  6. web.xml - 項目部署類

打開Eclipse,創(chuàng)建一個動態(tài)Web項目:ImprovingFetchRecords,其完整的目錄結(jié)構(gòu)如下所示 -

以下是這個項目中的幾個主要的代碼文件。

文件:index.html -

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Servlet從數(shù)據(jù)庫讀取記錄性能優(yōu)化</title>
</head>
<body style="text-algin: center;">
    <a href="servlet1">從數(shù)據(jù)庫讀取數(shù)據(jù)</a>|
    <a href="servlet2">讀取存儲的數(shù)據(jù)</a>
</body>
</html>

員工信息Bean類:Employees.java -

package com.yiibai;

public class Employees {
    private int id;
    private String name;
    private String address;
    private int age;
    private float salary;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

}

文件:MyListener.java -

這是是一個監(jiān)聽類。當(dāng)部署項目時,默認情況下會調(diào)用ServletContextListenercontextInitialized方法。 在這里,將查詢獲取employees表的記錄,并將數(shù)據(jù)記錄在添加存儲到ArrayList類對象中。 最后,表的所有記錄將存儲在ArrayList類對象(集合)。 最后,將ServletConext對象中的ArrayList對象作為屬性存儲,以便可以在Servlet中獲取并使用它。

package com.yiibai.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.yiibai.Employees;

import java.sql.*;
import java.util.ArrayList;

public class MyListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent e) {
        String jdbcDriver = "com.mysql.jdbc.Driver";
        String dbURL = "jdbc:mysql://localhost/testdb";

        // Database credentials
        String dbUser = "root";
        final String passwd = "123456";
        Connection con = null;
        ArrayList list = new ArrayList();
        try {
            Class.forName(jdbcDriver);
            con = DriverManager.getConnection(dbURL, dbUser, passwd);
            PreparedStatement ps = con.prepareStatement("SELECT * FROM `employees`");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Employees emp = new Employees();
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("name"));
                emp.setAddress(rs.getString("address"));
                emp.setAge(rs.getInt("age"));
                emp.setSalary(rs.getFloat("salary"));
                list.add(emp);
            }
            rs.close();
            ps.close();
            //con.close();

        } catch (Exception ex) {
            System.out.print(ex);
        }

        // storing the ArrayList object in ServletContext
        ServletContext context = e.getServletContext();
        context.setAttribute("con", con);
        context.setAttribute("datalist", list);

    }

    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("project undeployed...");
    }

}

文件:MyServlet1.java -

MyServlet1從servlet上下文對象獲取信息并打印它。

package com.yiibai;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet1 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        long before = System.currentTimeMillis();
        ServletContext context = getServletContext();
        try {
            Connection con = (Connection) context.getAttribute("con");
            PreparedStatement ps;
            ps = con.prepareStatement("SELECT * FROM `employees`");
            ResultSet rs = ps.executeQuery();
            out.print("員工數(shù)據(jù)信息如下所示:<hr/>");
            while (rs.next()) {
                out.print("" + rs.getInt("id") + ", " + rs.getString("name") + ", " + rs.getString("address"));
                out.println("<br/>");
            }
            //con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long after = System.currentTimeMillis();
        out.print("<br>總用時 :" + (after - before));
        out.close();
    }

}

文件:MyServlet2.java -

它與MyServlet1相同,從servlet上下文對象獲取信息并打印它。

package com.yiibai;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet2 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        long before = System.currentTimeMillis();

        ServletContext context = getServletContext();
        List list = (List) context.getAttribute("datalist");
        out.print("員工數(shù)據(jù)信息(從ServletContext中預(yù)存儲讀取)如下所示:<hr/>");
        Iterator itr = list.iterator();
        while (itr.hasNext()) {
            Employees e = (Employees) itr.next();
            out.print("" + e.getId() + ", " + e.getName() + ", " + e.getAddress());
            out.println("<br/>");
        }

        long after = System.currentTimeMillis();
        out.print("<br>總用時:" + (after - before));

        out.close();
    }

}

文件:web.xml -

這個文件中配置包含有關(guān)servlet和監(jiān)聽器的信息。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>ImprovingFetchRecords</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <listener>
        <listener-class>com.yiibai.listener.MyListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>MyServlet1</servlet-name>
        <servlet-class>com.yiibai.MyServlet1</servlet-class>

    </servlet>
    <servlet>
        <servlet-name>MyServlet2</servlet-name>
        <servlet-class>com.yiibai.MyServlet2</servlet-class>

    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet1</servlet-name>
        <url-pattern>/servlet1</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>MyServlet2</servlet-name>
        <url-pattern>/servlet2</url-pattern>
    </servlet-mapping>
</web-app>

在編寫上面代碼后,部署此Web應(yīng)用程序(在項目名稱上點擊右鍵->”Run On Server…”),打開瀏覽器訪問URL: http://localhost:8080/ImprovingFetchRecords/ ,如果沒有錯誤,應(yīng)該會看到以下結(jié)果 -

注意:將需要將MySQL驅(qū)動程序庫加到WEB-INFO/lib目錄下。

點擊“從數(shù)據(jù)庫讀取數(shù)據(jù)”鏈接,應(yīng)該會看到以下結(jié)果 -

點擊“讀取存儲的數(shù)據(jù)”鏈接,應(yīng)該會看到以下結(jié)果 -