实现一个用JSP写的查询单词的网页
网页要求:
(1)用JSP写的网页,实现一个查询单词的网页,能依据拼写、词性、难度范围等信息查出所需词汇的释义来。网页可参照金山词霸、汉典、有道等的风格。要求建立大学四级、六级词汇表(可从网上找来)放到MYSQL数据库中,至少包括:单词、词性、释义、难度(标记是4级的还是6级还是其他)。
(2)反查:能依据释义内容,模糊查出对应的单词。
(3)统计:实现对单词表中a,b,c到z开头单词个数的统计,结果以表格输出到网页上。
1、将英文生词做成表放在MySQL数据库中,命名为dictionary,如图所示:
2、启动tomcat和mysql,如图所示:
3、用Editplus编辑代码,对网页的界面进行设计,命名为interface.jsp,代码如下:
interface.jsp:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<form action="search.jsp" method="POST">
<center>
<%
out.println("<font color=\"break\" size=\"18\" face=\"Arial\">单词查询系统</font>"); //输出一个标题
%>
</br></br>
english:<br>
<input type="text" name="english" > <% /* 建立一个录入格 */ %>
<br><br>
type:<br>
<input type="text" name="type" >
<br><br>
chinese:<br>
<input type="text" name="chinese">
<br><br>
difficulty:<br>
<input type="text" name="difficulty" >
<br><br>
initials:<br>
<input type="text" name="initials" >
<br><br>
<button>查询</button>
</center>
5、具体实现的代码命名为search.jsp,代码如下:
search.jsp:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="java.sql.*" %> <%--导入java.sql包--%>
<!DOCTYPE>
<html>
<head>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序
String url = "jdbc:mysql://localhost:3306/person";//URL指向要访问的数据库名mydata
String user = "root";//MySQL配置时的用户名
String password = ""; //MySQL配置时的密码
//1.getConnection()方法,连接MySQL数据库!!
Connection con = (Connection) DriverManager.getConnection(url,user,password);
Statement stmt = null;
ResultSet rs = null;
String english =request.getParameter("english");
String type =request.getParameter("type");
String ch =request.getParameter("chinese");
String chinese=new String(ch.getBytes("ISO-8859-1"),"UTF-8");
String dif =request.getParameter("difficulty");
String difficulty=new String(dif.getBytes("ISO-8859-1"),"UTF-8");
String initials=request.getParameter("initials");
if(english != null || type != null || chinese != null || difficulty != null){
if(initials.equals("")){
%>
<table align="center" border="2">
<tr>
<td width="100" english="title">英文</td>
<td width="100" type="title">词性</td>
<td width="100" chinese="title">中文</td>
<td width="100" difficulty="title">难度</td>
</tr>
<%
String sql = "SELECT * FROM dictionary WHERE english like '%"+english+"%' and type like '%"+type+"%' and chinese like '%"+chinese+"%' and difficulty like '%"+difficulty+"%';";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
out.print("查询结果:");
while (rs.next()) {
%>
<tr>
<td width="100" ><%=rs.getString("english") %></td>
<td width="100" ><%=rs.getString("type") %></td>
<td width="100"><%=rs.getString("chinese") %></td>
<td width="100"><%=rs.getString("difficulty") %></td>
</tr>
<%
}
}
}
if(initials != null && !"".equals(initials)){%>
<h2 align="center">单词个数统计</h2>
<table align="center" width="50%" border="1">
<tr>
<th>首字母</th>
<th>单词个数</th>
</tr>
<%
String sql = "SELECT * FROM dictionary WHERE initials like '"+initials+"%';";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
int i=0;
while (rs.next())
{
i++;
}
%>
<tr align="center">
<td><%=initials%></td>
<td><%=i %></td>
</tr>
<% }
}catch (Exception e) {
e.printStackTrace();
out.print("数据库连接异常!");
}
%>
</table>
</body>
</html>