请帮我写一个query2.jsp页面,用于局域网内的浏览器访问mysql数据库,并将查询结果显示在浏览器中(代码也在query2.jsp中)。数据库名称mysql,表名称table1,IP 192.168.5.100,密码mypassword,端口号默认。表table1中含有如下字段SN,modelCode,lotNum,sterDate,productModel,分别对应界面上的“序列号,型号代码,批号,灭菌日期,产品型号。具体需求如下:当从局域网内的浏览器打开时 (1)允许用户分别输入SN,modelCode,lotNum,sterDate,productModel,根据用户输入的信息(未输入的部分不参与查询),模糊查询符合条件的数据库所有记录,并显示在query2.jsp中的table中。 结果显示时,将数据库的字段名按照下列对应关系展示在浏览器中。SN对应'序列号',modelCode对应'型号代码',logNum对应'批号',sterDate对应'灭菌日期',productModel对应'产品型号'. 背景:web服务器为tomcat;使用jdbc数据连接,不要使用Class.forName方法,而要使用DriverManager.getConnection方法。不要使用其他的标签库或者框架,使用标准的、原始代码即可。
时间: 2024-02-28 08:55:55 浏览: 88
以下是query2.jsp的代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询结果</title>
</head>
<body>
<h2>查询结果</h2>
<table border="1">
<tr>
<th>序列号</th>
<th>型号代码</th>
<th>批号</th>
<th>灭菌日期</th>
<th>产品型号</th>
</tr>
<%
String SN = request.getParameter("SN");
String modelCode = request.getParameter("modelCode");
String lotNum = request.getParameter("lotNum");
String sterDate = request.getParameter("sterDate");
String productModel = request.getParameter("productModel");
String url = "jdbc:mysql://192.168.5.100:3306/mysql?useSSL=false";
String user = "root";
String password = "mypassword";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
String sql = "SELECT SN, modelCode, lotNum, sterDate, productModel FROM table1 WHERE 1=1 ";
if (SN != null && !SN.equals("")) {
sql += "AND SN LIKE '%" + SN + "%' ";
}
if (modelCode != null && !modelCode.equals("")) {
sql += "AND modelCode LIKE '%" + modelCode + "%' ";
}
if (lotNum != null && !lotNum.equals("")) {
sql += "AND lotNum LIKE '%" + lotNum + "%' ";
}
if (sterDate != null && !sterDate.equals("")) {
sql += "AND sterDate LIKE '%" + sterDate + "%' ";
}
if (productModel != null && !productModel.equals("")) {
sql += "AND productModel LIKE '%" + productModel + "%' ";
}
rs = stmt.executeQuery(sql);
while (rs.next()) {
String sSN = rs.getString("SN");
String sModelCode = rs.getString("modelCode");
String sLotNum = rs.getString("lotNum");
String sSterDate = rs.getString("sterDate");
String sProductModel = rs.getString("productModel");
out.println("<tr>");
out.println("<td>" + sSN + "</td>");
out.println("<td>" + sModelCode + "</td>");
out.println("<td>" + sLotNum + "</td>");
out.println("<td>" + sSterDate + "</td>");
out.println("<td>" + sProductModel + "</td>");
out.println("</tr>");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
</table>
</body>
</html>
```
该页面中使用了JDBC连接MySQL数据库,根据用户输入的条件进行模糊查询,将查询结果展示在表格中。用户可以输入SN、modelCode、lotNum、sterDate、productModel中的任意一个或多个条件,未输入的条件不参与查询。在表格中展示的字段名与数据库中的字段名不同,需要进行对应关系的处理。该页面仅使用了标准的、原始的JSP代码,没有使用其他的标签库或框架。
阅读全文
相关推荐



















