以下是一个简单的班级查询 JSP 代码示例:
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>班级查询</title>
</head>
<body>
<h2>班级查询</h2>
<form action="classQuery.jsp" method="get">
班级名称:<input type="text" name="className">
<input type="submit" value="查询">
</form>
<%
String className = request.getParameter("className");
if(className!=null && !className.isEmpty()){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username", "password");
String sql = "SELECT * FROM classes WHERE name LIKE ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + className + "%");
rs = pstmt.executeQuery();
%>
<table border="1">
<tr>
<th>班级 ID</th>
<th>班级名称</th>
<th>班主任</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getString("teacher") %></td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs!=null) rs.close();
if (pstmt!=null) pstmt.close();
if (conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
</body>
</html>
基础概念:
优势:
类型:
应用场景:
可能出现的问题及原因:
解决方法:
希望以上内容对您有所帮助!如果您还有其他疑问,请随时提问。
领取专属 10元无门槛券
手把手带您无忧上云