今天是web的第五节课,上一节课只涉及到了查询和增加,这一节课我们学习网页链接数据库进行增删改查,今天的内容更多的也是代码,理论内容较少,现在小编带大家进入新的一课。
目录
一.查
思路:
- 写一个登录界面。
- 我们建立一个新的表格,存放用户的数据。可自行往该表格中插入几条数据,方便测试
- 判断数据是否存在,我们需要一个新的jsp文件,在该文件中,拿到登录界面的数据, 在连接数据库,查询该用户是否存在。存在的话跳转到首页,不存在的话直接跳转回到 登录界面面。
登录界面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
html,
body {
background: #1abe9c;
}
form {
width: 300px;
background: #ebeff2;
box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);
border-radius: 5px;
padding: 20px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.btn-group {
width: 100%;
}
.btn-group button {
width: 50%;
}
</style>
</head>
<body>
<form action="doLogin.jsp" method="post">
<h3 class="text-center">欢迎使用🐖币新闻管理</h3>
<div class="form-group">
<input name="username" type="text" class="form-control" placeholder="请输入您的用户名">
</div>
<div class="form-group">
<input name="userpwd" type="password" class="form-control" placeholder="请输入您的密码">
</div>
<div class="btn-group">
<button type="submit" class="btn btn-primary">登录</button>
<button type="button" class="btn btn-danger" onclick='location=href="regiest.html"'>没有账号?</button>
</div>
</form>
</body>
</html>
判断用户是否存在数据库代码
该代码拿到登录界面的数据,在去数据库进行匹配,匹配该用户是否存在
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//拿到数据
String name=request.getParameter("username");
String pwd=request.getParameter("userpwd");
//获取驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//编写连接语句
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接对象
Connection con=DriverManager.getConnection(url,"scott","zking123");
//编写数据库语句
PreparedStatement ps=con.prepareStatement("select * from jw05_user where uname=? and upwd=?");
//给占位符赋值
ps.setString(1,name);
ps.setString(2, pwd);
//获得结果集
ResultSet rs=ps.executeQuery();
//判断该用户是否存在
if(rs.next()){
//存在的话跳转到新闻首页
//转发
request.getRequestDispatcher("/news/index.jsp").forward(request, response);
}else{
//用户不存在数据库中,返回登录界面
//重定向
response.sendRedirect("login.jsp");
}
//关闭资源
if(con!=null&&con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
ps.close();
}
%>
二.增
思路:
- 当登录成功用户会跳转到我们的首页,我们需要去发布新闻,点击新闻管理--->新闻发布。(点击这里跳转到增加新闻界面)
- 发布新闻其实相当于增加数据,我们拿到发布界面上我们填写的数据,点击增加按钮加入进数据库中,新发布的新闻,也会在新闻首页显示出来(拿到数据需要像登录界面一样,创建新的一个jsp文件,拿到数据,连接数据库,把数据插入数据库)
- 创建一张新闻表,存放新闻,上一节课04教大家是如何把数据增加进数据库,今天把数据加入数据库,在新闻首页界面新增的新闻会显示出来。
新闻首页代码
大家可以看到在新闻首页代码这里,我们也运用到了查,把新闻表中的数据拿出来,赋值给到页面上。
${pageContext.request.contextPath}/news/read.jsp?newId=<%=rs.getInt(1)%>
这串代码是当我们点击新闻时,跳转到新闻阅读界面,newId是一个名字 ,rs.getInt(1)是新闻表的id,在后面加上newid=xxxx是携带一个数据过去,上方代码就是携带新闻你点击的这一篇新闻的id跳转到阅读界面中。该代码在我们的阅读界面起到非常大的作用,阅读界面可以拿到该id,查询该新闻数据,把数据赋值在阅读界面上。
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/JavaWeb05/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="/JavaWeb05/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="/JavaWeb05/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0px !important;
margin-bottom: 0px !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
li h4 {
width: 300px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="index