具体要求:
1、用JSP编写网页。实现一个查询单词的网页,能依据拼写、词性、难度范围等信息查出所需词汇的释义来。建立大学四级、六级词汇表放到MYSQL数据库中,至少包括:单词、词性、释义、难度(标记是4级的还是6级还是其他)。
2、反查:能依据释义内容,模糊查出对应的单词。
3、统计:实现对单词表中a,b,c到z开头单词个数的统计,结果以表格输出到网页上。
1、将英文生词做成一张表放入MySQL数据库中
将mysql数据库与navicat进行连接,设计一张英文生词表word,如下图所示。
2、通过JSP网页实现查询单词的功能
(1)确保tomcat和mysql是启动状态。(怎样启动可参照本文:https://blog.csdn.net/weixin_42014622/article/details/80683379)
(2)用Editplus编辑代码,保存在E:\tomcat\apache-tomcat-7.0.88\webapps\ROOT该目录下,如下图所示。
代码如下:
①practice.jsp
<%@ page contentType="text/html"%>
<%@page pageEncoding="GB2312"%>
<!DOCTYPE>
<html>
<head>
<title>zhl</title>
</head>
<body style="background:url(http://preview.quanjing.com/ul0839/ul0839-0907.jpg) center;background-size:cover"><%--设置背景图片--%>
</br></br></br>
<h2><%= (new java.util.Date()).toString()%></h2>
<form action="search.jsp" method="POST"><%-- 使用post方法跳转至查询界面--%>
<table align="center">
<h1 style="text-align:center;font-size:55px">单词查询</h1>
<tr>
<td>英文:</td>
<td><input type="text" name="english"></td>
</tr><tr>
<td>词性:</td>
<td><input type="text" name="pos"></td>
</tr><tr>
<td>中文:</td>
<td><input type="text" name="chinese"></td>
</tr><tr>
<td>难度:</td>
<td><input type="text" name="difficulty"></td>
</tr><tr>
<th></th>
<th><input type="submit" value=" 查 询 "></th>
<th><input type="reset" value=" 重 置 "></th>
</table>
</form>
<form action="count.jsp" method="POST"><%-- 使用post方法跳转至统计界面--%>
<table align="center">
<tr>
</br></br>
<td><input type="submit" value=" 统 计 A 至 Z 单 词 个 数 " ></td>
</tr>
</table>
</body>
</html>
②search.jsp
<%@page contentType="text/html" pageEncoding="GB2312" %>
<%@page import="java.sql.*" %> <%--导入java.sql包--%>
<html>
<head>
<title >单词查询</title>
</head>
<body style="background:url(http://www.pptbz.com/pptpic/UploadFiles_6909/201105/2011051707485717.jpg) center;background-size:cover">
<%
try {
Class.forName("com.mysql.jdbc.Driver"); ////驱动程序名
String url = "jdbc:mysql://localhost:3306/study"; //数据库名
String username = "root"; //数据库用户名
String password = "123456"; //数据库用户密码
Connection conn = DriverManager.getConnection(url, username, password); //连接状态
if(conn != null){
out.print("数据库连接成功!");
out.print("<br />");
out.print("<br />");
%>
<table align="center" border="2">
<tr>
<td width="100" english="title">英文</td>
<td width="100" pos="title">词性</td>
<td width="100" chinese="title">中文</td>
<td width="100" difficulty="title">难度</td>
</tr>
<%
Statement stmt = null;
ResultSet rs = null;
String english =request.getParameter("english");
String pos =request.getParameter("pos");
String ch =request.getParameter("chinese");
String chinese=new String(ch.getBytes("ISO-8859-1"),"gb2312");
String dif =request.getParameter("difficulty");
String difficulty=new String(dif.getBytes("ISO-8859-1"),"gb2312");
String sql = "SELECT * FROM word WHERE english like '%"+english+"%' and pos like '%"+pos+"%' and chinese like '%"+chinese+"%' and difficulty like '%"+difficulty+"%';"; //查询语句
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
out.print("查询结果:");
while (rs.next()) {%>
<tr>
<td width="100" ><%=rs.getString("english") %></td>
<td width="100" ><%=rs.getString("pos") %></td>
<td width="100"><%=rs.getString("chinese") %></td>
<td width="100"><%=rs.getString("difficulty") %></td>
</tr>
<%
}
}else{
out.print("连接失败!");
}
}catch (Exception e) {
e.printStackTrace();
out.print("数据库连接异常!");
}
%>
</table>
<h1 align="center" ><input type="button" name="Submit" onclick="javascript:history.back(-1);" value="返回上一页"> </h1><%--设置返回按钮--%>
</body>
</html>
③count.jsp
<%@ page contentType="text/html"%>
<%@page pageEncoding="GB2312"%>
<%@page import="java.sql.*" %> <%--导入java.sql包--%>
<html>
<head>
<title >单词统计</title>
</head>
<body style="background:url(http://www.pptbz.com/pptpic/UploadFiles_6909/201105/2011051707485717.jpg) center;background-size:cover">
<%
try {
Class.forName("com.mysql.jdbc.Driver"); ////驱动程序名
String url = "jdbc:mysql://localhost:3306/study"; //数据库名
String username = "root"; //数据库用户名
String password = "123456"; //数据库用户密码
Connection conn = DriverManager.getConnection(url, username, password); //连接状态
if(conn != null){
out.print("数据库连接成功!"); %>
<h2>A至Z开头单词个数分别为:</h2>
<table align="center" border="1">
<tr>
<td width="55" initial="title">首字母</td>
<td width="55" count(initial)="title">统计</td>
</tr>
<%
Statement stmt = null;
ResultSet rs = null;
String sql = "SELECT initial,count(initial) FROM word GROUP BY initial;"; //查询语句
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {%>
<tr>
<td width="55"><%=rs.getString("initial") %></td>
<td width="55"><%=rs.getString("count(initial)") %></td>
</tr>
<%}
}else{
out.print("连接失败!");
}
}catch (Exception e) {
e.printStackTrace();
out.print("数据库连接异常!");
}
%>
</table>
<h1 align="center" ><input type="button" name="Submit" onclick="javascript:history.back(-1);" value="返回上一页"> </h1><%--设置返回按钮--%>
</body>
</html>