JSP学习(六)——统计单词个数

一、单词个数统计页面

     博主下载的单词表为CET6核心词汇表,一共601个单词,即导入数据库中的表“cet6”,统计表中以“a、b······z”开头的单词个数,并以表格的形式显示在网页上。程序主要通过SQL语句SELECT 进行查询,将符合条件的单词提取,再利用ResultSet函数中的next方法对提取出来的单词遍历,每遍历一遍,整数型变量“rscount”+1,最终的计数便是单词个数。

cet6:
图一
count.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>  
<%@ page import="com.mysql.jdbc.Driver" %>   
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>count</title>
</head>
<body style="background: repeat url(image/2.jpg)">
<tr>&nbsp;</tr>
<tr>&nbsp;</tr>
<tr>&nbsp;</tr>
<tr>&nbsp;</tr>
<h1 align="center" id="biaotoucolor">统计结果</h1>
<%  
    //加载驱动程序   
    String driverName="com.mysql.jdbc.Driver";   
    //数据库信息  
    String userName="root";   
    //密码   
    String userPasswd="yle30268";   
    //数据库名   
    String dbName="tables";   
    //表名    

    String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;   
    Class.forName("com.mysql.jdbc.Driver").newInstance();   
    Connection conn=DriverManager.getConnection(url);   
    Statement stmt = conn.createStatement();
    String initial[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    for(int i=0;i<initial.length;i++){
        int rscount = 0;
        String sql="SELECT * FROM cet6 WHERE english like '"+initial[i]+"%'" ; 
        //通过模糊查询SELECT检索以“a、b······”开头的单次个数
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
            rscount++;
        }

%>

<table align="center" border="2">
<tr>
<td width="180" a="title"><%=initial[i]%>为首的单词数:<%=rscount %></td>
<!-- 循环输出统计结果 -->
</tr>
<%
}
%>
</table>
</body>
</html>
二、统计演示

     根据上图“cet6”可以看出一共有601条单词记录,所以统计出来的结果之和也应该是601个单词,其中以“z”开头的单词只有一个。
图二

猜你喜欢

转载自blog.csdn.net/yga_airspace/article/details/80946741