测试5错误纠正
今天王老师又为我们带来了“死亡星期五”,其实,今天的题目,说实话并不难,因为我之前是有过一段时间的学习的嘛,然后就做成了今天这个“没用玩意儿”的样子,(是我本人都觉得无语的程度了)。就是连接数据库+查询数据库部分数据,怎么说呢,心情很复杂,有一种没来由的恐慌感,王老师期间还为我们找到了之前的学长的博客作为参考,只能说,王老师脾气真好,可以说是好极了。
废话不多说,还是来看一看今天的代码吧!(其中还涉及到一些模板的应用)
<!--查询界面-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html class="h-100" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>查询界面</title>
<!-- Favicon icon -->
<link rel="icon" type="image/png" sizes="16x16" href="theme1/images/favicon.png">
<!-- <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous"> -->
<link href="theme1/css/style.css" rel="stylesheet">
</head>
<body class="h-100">
<!--*******************
Preloader start
********************-->
<div id="preloader">
<div class="loader">
<svg class="circular" viewBox="25 25 50 50">
<circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="3" stroke-miterlimit="10" />
</svg>
</div>
</div>
<!--*******************
Preloader end
********************-->
<div class="login-form-bg h-100">
<div class="container h-100">
<div class="row justify-content-center h-100">
<div class="col-xl-6">
<div class="form-input-content">
<div class="card login-form mb-0">
<div class="card-body pt-5">
<a class="text-center"> <h4>查询界面</h4></a>
<form class="mt-5 mb-5 login-input" action="servlet">
<div class="form-group">
<input type="text" id="cla" name="cla" class="form-control" placeholder="课程名称">
</div>
<div class="form-group">
<input type="text" id="teacher" name="teacher" class="form-control" placeholder="任课教师">
</div>
<div class="form-group">
<input type="text" id="whe" name="whe" class="form-control" placeholder="上课地点">
</div>
<button type="submit" class="btn login-form__btn submit w-100" onclick='window.open("index.jsp")'>查询</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!--**********************************
Scripts
***********************************-->
<script src="theme1/plugins/common/common.min.js"></script>
<script src="theme1/js/custom.min.js"></script>
<script src="theme1/js/settings.js"></script>
<script src="theme1/js/gleek.js"></script>
<script src="theme1/js/styleSwitcher.js"></script>
</body>
</html>
<!--显示数据界面-->
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="org.example.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.ArrayList" %>
<%--
Created by IntelliJ IDEA.
User: 16475
Date: 2022/6/29
Time: 23:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Hugo 0.88.1">
<title>管理员界面</title>
<link rel="canonical" href="https://getbootstrap.com/docs/5.1/examples/dashboard/">
<!-- Bootstrap core CSS -->
<link href="bootstrap/dist/css/bootstrap.css" rel="stylesheet">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="css/dashboard.css" rel="stylesheet">
</head>
<body>
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<a class="navbar-brand col-md-3 col-lg-2 me-0 px-3" href="postEnd.jsp">管理界面</a>
<button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<input class="form-control form-control-dark w-100" type="text" id="phone" placeholder="搜索" aria-label="Search">
<div class="navbar-nav">
<div class="nav-item text-nowrap">
<a class="nav-link px-3" href="login.jsp">登出</a>
</div>
</div>
</header>
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">
<span data-feather="home"></span>
1界面
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="postEnd.jsp">
<span data-feather="file"></span>
2界面
</a>
</li>
</ul>
</div>
</nav>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">当前用户</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group me-2">
<a class="btn btn-sm btn-outline-secondary" href="main.jsp"><span>删除账号</span></a>
<a class="btn btn-sm btn-outline-secondary" href="update.jsp"><span>修改信息</span></a>
</div>
<button type="button" class="btn btn-sm btn-outline-secondary ">
修改密码
</button>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">课程名称</th>
<th scope="col">任课教师</th>
<th scope="col">授课地点</th>
</tr>
</thead>
<tbody>
<%-- <c:forEach items="${list}" var="list"> --%>
<%
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ad?useUnicode=true&characterEncoding=utf-8", "root", "20214063");
//使用Statement对象
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from super1");
String cla= (String) request.getAttribute("cla");
String teacher= (String) request.getAttribute("teacher");
String whe= (String) request.getAttribute("whe");
// String cla = request.getParameter("cla");
// String teacher = request.getParameter("teacher");
// String whe = request.getParameter("whe");
//String sql="select * from where cla=? and where teacher=? and where whe=?";
%>
<tr>
<td>${cla}</td>
<td>${teacher}</td>
<td>${whe}</td>
</tr>
<%
rs.close();
stmt.close();
con.close();
%>
</tbody>
</table>
</div>
</main>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js" integrity="sha384-uO3SXW5IuS1ZpFPKugNNWqTZRRglnUJK6UAZ/gxOX80nxEkN9NcGZTftn6RzhGWE" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js" integrity="sha384-zNy6FEbO50N+Cg5wap8IKA4M/ZnLJgzc6w2NqACZaK0u0FXfOWRRJOnQtpZun8ha" crossorigin="anonymous"></script><script src="js/dashboard.js"></script>
</body>
</html>
//用户类
package org.example;
public class User{
private String cla;
private String teacher;
private String whe;
public void setCla(String cla){
this.cla=cla;
}
public String getCla(){
return cla;
}
public void setTeacher(String teacher){
this.teacher=teacher;
}
public String getTeacher(){
return teacher;
}
public void setWhe(String whe){
this.whe=whe;
}
public String getWhe(){
return whe;
}
}
//servlet类
package org.example;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String cla = req.getParameter("cla");
String teacher = req.getParameter("teacher");
String whe = req.getParameter("whe");
//向页面传值
req.setAttribute("cla",cla);
req.setAttribute("teacher",teacher);
req.setAttribute("whe",whe);
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
//连接数据库
package org.example;
import java.sql.*;
public class JDBCUtils {
public static Connection getConnection() {
try {
//1 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = "root";
String password = "20214063";
String url="jdbc:mysql://localhost:3306/ad";
Connection connection = null;
try {
//2 创建链接对象connection
connection = DriverManager.getConnection(url,name,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
//关闭资源的方法
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}