JavaWeb 网页链接数据库&增删改查
今天是web的第五节课,上一节课只涉及到了查询和增加,这一节课我们学习网页链接数据库进行增删改查,今天的内容更多的也是代码,理论内容较少,现在小编带大家进入新的一课。目录1.登录2.判断用户是否存在数据库代码3.首页代码4.增加新闻界面代码5.获取数据,插入数据库代码(增加)6.阅读新闻代码7.删除新闻8.修改界面代码9.修改数据JabaWeb04文章给大家已经讲解,所就简单带过,大家直接去看Ja
今天是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.html" style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown"> 新闻管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="/JavaWeb05/news/add.jsp">新闻发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">首页</li>
</ol>
<form class="form-inline" style="margin: 0px auto 20px;">
<div class="form-group" style="display: block;text-align: center;">
<div class="input-group">
<div class="input-group-addon">新闻标题</div>
<input type="text" class="form-control" placeholder="请在此输入搜索的关键字">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">搜索🔍</button>
</span>
</div>
</div>
</form>
<%
//创建一个新闻数据库,把数据库中的新闻数据,拿出来
//获取驱动
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_news");
//获得结果集
ResultSet rs=ps.executeQuery();
while(rs.next()){
%>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="${pageContext.request.contextPath}/news/read.jsp?newId=<%=rs.getInt(1)%>" data-placement="bottom" data-toggle="tooltip" href="" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
<%=rs.getString(2)%>
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code><%=rs.getString(4)%></code></span>
<span class="glyphicon glyphicon-eye-open"><code>1000</code></span>
<span class="glyphicon glyphicon-tag"><code>1000</code></span>
<span class="glyphicon glyphicon-time"><code><%=rs.getString(5)%></code></span>
</p>
</li>
<%
}
if (!con.isClosed()) {
con.close();
}
ps.close();
rs.close();
%>
</ul>
<div class="container text-center">
<ul class="pagination" style="margin: 20px auto;">
<li>
<a href="#"><span>«</span></a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#"><span>»</span></a>
</li>
</ul>
</div>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip({
trigger: "hover"
})
})
</script>
</body>
</html>
增加新闻界面代码
点击新闻发布跳转到增加新闻的界面,在这里有一个下拉框选项,该下拉框选项的数据,由我们连接新闻表,拿到新闻类别的数据,赋值给下拉框。
大家要给输入框和下拉框定义名字,方便拿到数据
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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 content="width=device-width, initial-scale=1" name="viewport">
<link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0 !important;
margin-bottom: 0 !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
input,
select,
textarea,
.panel-heading {
border: none !important;
border-radius: 0 !important;
}
.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="${pageContext.request.contextPath}/news/index.jsp"
style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
新闻管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">新闻发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">新闻发布</li>
</ol>
<form action="doAdd.jsp" class="container" method="post">
<div class="panel panel-info">
<div class="panel-heading">新闻标题</div>
<input class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required>
<div class="panel-heading">新闻类别</div>
<select class=" form-control" name="topic">
<!-- 这一步是拿到数据库新闻类别的值,赋值到新闻类别 -->
<%
//获取驱动
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_topic");
//获得结果集
ResultSet rs=ps.executeQuery();
while(rs.next()){
%>
<option value=<%=rs.getInt(1) %>><%=rs.getString(2)%></option>
<%
}
%>
</select>
<div class="panel-heading">新闻作者</div>
<input class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required>
<div class="panel-heading">发布时间</div>
<input class="form-control" name="publisher" required type="date">
<div class="panel-heading">新闻内容</div>
<textarea class="form-control" name="content" placeholder="🙅达咩~~~~这是必填的" required rows="10"></textarea>
<div class="panel-footer">
<button class="btn btn-primary" >增加</button>
<button class="btn btn-danger">取消</button>
</div>
</div>
</form>
</body>
</html>
获取数据 插入数据库代码(增加)
该jsp文件中编写的代码,拿到增加界面的数据,通过输入框和下拉框的名字拿到数据,
连接数据库,将数据增加进数据库,在判断是否增加成功。
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//拿到数据
//拿到新闻标题
String title=request.getParameter("title");
String topic=request.getParameter("topic");
String author=request.getParameter("author");
String publisher=request.getParameter("publisher");
String content=request.getParameter("content");
//获取驱动
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 nvl(max(news_id),0) from jw05_news");
ResultSet rs=ps.executeQuery();
//拿到id
int id=0;
if(rs.next()){
id=rs.getInt(1);
}
id++;
ps=con.prepareStatement("insert into jw05_news vaules(news_id,news_title,news_topic, news_author, news_publisher, news_content) values(?,?,?,?,?,?)");
ps.setInt(1, id);
ps.setString(2, title);
ps.setString(3, topic);
ps.setString(4, author);
ps.setString(5, publisher);
ps.setString(6, content);
int i=ps.executeUpdate();
if(i>0){
out.print("<script>alert('增加成功');location.href='/JavaWeb05/news/index.jsp'</script>");
}else{
out.print("<script>alert('增加失败');location.href='/JavaWeb05/news/index.jsp'</script>");
}
//关闭资源
if(con!=null&&con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
%>
阅读新闻代码
该代码是我们在新闻首页时,点击新闻的标题跳转到该界面,该界面可以进行删除新闻,修改新闻操作。
我们跳转到该界面的时候,这个界面其实相当于,详细的看我们的新闻, 我们会在新闻首页跳转过来时,新闻首页有一串代码会将新闻的id传过来,在一串网址上面会携带一个xxx=xx,就是传过来的id,然后用request.getParameter拿到该id,在连接数据库,用id查询,赋值给到界面上面。
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0 !important;
margin-bottom: 0 !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
input,
select,
textarea,
.panel-heading {
border: none !important;
border-radius: 0 !important;
}
.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="${pageContext.request.contextPath}/news/index.jsp"
style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
新闻管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">新闻发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">新闻阅读</li>
</ol>
<%
//拿到传过来的id
String newId=request.getParameter("newId");
//1.获取驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.定义连接字符
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//3.获得连接
Connection con=DriverManager.getConnection(url,"scott","zking123");
//4.获得执行对象
PreparedStatement ps=con.prepareStatement("select *from jw05_news where news_id=? ");
ps.setInt(1,Integer.parseInt(newId));
ResultSet rs=ps.executeQuery();
//定义需要的值
String title="";
int count=0;
String author="";
String publisher="";
String content="";
if(rs.next()){
title=rs.getString(2);
count=rs.getInt(3);
author=rs.getString(4);
publisher=rs.getString(5);
content=rs.getString(6);
}
%>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;">
<h1><%=title%></h1>
<h3 class="text-right">
<small>
<span class="glyphicon glyphicon-user"><span class="label label-default"></span><%=author%></span>
<span class="glyphicon glyphicon-eye-open"><span class="label label-default"></span>1000</span>
<span class="glyphicon glyphicon-time"><span class="label label-info"></span>1000</span>
</small>
</h3>
<samp></samp>
<div class="btn-group btn-group-justified" style="margin-bottom: 20px;">
<div class="btn-group">
<a href="${pageContext.request.contextPath}/news/doDel.jsp?newId=<%=newId%>" class="btn btn-danger" type="button">删除</a>
</div>
<div class="btn-group">
<a href="${pageContext.request.contextPath}/news/upd.jsp?newId=<%=newId%>" class="btn btn-info" type="button">修改</a>
</div>
</div>
</div>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;">
<div class="panel panel-default" style="margin-top: 20px;">
<div class="panel-heading">
<span class="glyphicon glyphicon-user"><span class="label label-success">xxx</span></span>
<p style="margin-top: 10px;text-indent: 2em;">
<samp>我是一条非常好看的评论.</samp>
</p>
<p class="text-right">
<span class="glyphicon glyphicon-time"><span class="label label-info">2020/1/1 10:23:04</span></span>
</p>
</div>
</div>
<div class="panel panel-default" style="margin-top: 20px;">
<div class="panel-heading">
<span class="glyphicon glyphicon-user"><span class="label label-success">xxxx</span></span>
<p style="margin-top: 10px;text-indent: 2em;">
<samp>我是一条非常好看的评论.</samp>
</p>
<p class="text-right">
<span class="glyphicon glyphicon-time"><span class="label label-info">2020/1/1 10:23:04</span></span>
</p>
</div>
</div>
</div>
<form class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;padding: 30px;">
<div class="form-group">
<label for="name">Name</label>
<input id="name" class="form-control" placeholder="用户名称" required type="text">
</div>
<div class="form-group">
<label for="email">Email</label>
<input id="email" class="form-control" placeholder="评论内容" required type="email">
</div>
<button class="btn btn-default" type="submit">发布评论</button>
</form>
<div style="height: 50px;"></div>
</body>
</html>
三.删
删除新闻
点击删除按钮,跳转到该界面,进行删除新闻。
创建一个新的jsp文件,当点击删除按钮时会跳转到该界面,跳转到界面时,要携带新闻的id过来,根据id进行删除,在判断是否删除成功。
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//获得新闻的id
String newId = request.getParameter("newId");
//根据id去数据库做删除操作
//加载驱动
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("delete from jw05_news where news_id=?");
//占位符的设置
ps.setInt(1,Integer.parseInt(newId));
//执行并获得结果 【收到影响的行数】
int i = ps.executeUpdate();
if(i>0){ //删除成功
out.print("<script>alert('删除成功');location.href='index.jsp'</script>");
}else{ //删除失败
out.print("<script>alert('删除失败');history.go(-1)</script>");
}
%>
四.修
修改界面代码
该界面和增加界面是一摸一样的界面,我们在阅读新闻界面中点击修改会跳转到该界面,也要携带id过来,拿到id进行查询,把新闻赋值给到输入框上面。
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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;
}
input,
select,
textarea,
.panel-heading {
border: none !important;
border-radius: 0px !important;
}
.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="/news/index.html" style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
新闻管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">新闻发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">新闻修改</li>
</ol>
<%
String newId=request.getParameter("newId");
//创建一个新闻数据库,把数据库中的新闻数据,拿出来
//获取驱动
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_news where news_id=?");
ps.setInt(1,Integer.parseInt(newId));
ResultSet rs=ps.executeQuery();
String title="";
int topic=0;
String author="";
String publisher="";
String content="";
if(rs.next()){
title=rs.getString(2);
publisher=rs.getString(5);
author=rs.getString(4);
content=rs.getString(6);
topic=rs.getInt(3);
}
%>
<form class="container" action="doUpd.jsp">
<div class="panel panel-info">
<!-- hidden 是输入框隐藏 -->
<input type="hidden" value="<%=newId%>" name="newId">
<div class="panel-heading">新闻标题</div>
<input name="title" value="<%=title%>" class="form-control" maxlength="50" required placeholder="标题控制在30个字之内哦~~~">
<div class="panel-heading">新闻类别</div>
<select name="topic" class=" form-control">
<%
ps=con.prepareStatement("select * from jw05_topic");
rs=ps.executeQuery();
while(rs.next()){
%>
<option
<%=topic==rs.getInt(1)?"selected":""%>
value="<%=rs.getInt(1)%>"
>
<%=rs.getString(2)%>
</option>
<%
}
%>
</select>
<div class="panel-heading">新闻作者</div>
<input name="author" value="<%=author%>" class="form-control" maxlength="10" required placeholder="名字控制在10个字之内哦~~~">
<div class="panel-heading">发布时间</div>
<input name="publisher" value="<%=publisher%>" type="date" value="2020/1/1" class="form-control" required placeholder="名字控制在10个字之内哦~~~">
<div class="panel-heading">新闻内容</div>
<textarea name="content" class="form-control" rows="10" required placeholder="🙅达咩~~~~这是必填的">
<%=content%>
</textarea>
<div class="panel-footer">
<button class="btn btn-primary">修改</button>
<button type="reset" class="btn btn-danger">取消</button>
</div>
</div>
</form>
</body></html>
修改数据
从修改界面跳转到这里来,拿到修改界面的数据,连接数据库修改,在判断是否修改成功。
<%@page import="java.util.Date"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
//获取到新闻的所有信息
String newId = request.getParameter("newId");
String author = request.getParameter("author");
String publisher = request.getParameter("publisher");
String content = request.getParameter("content");
String topic = request.getParameter("topic");
String title = request.getParameter("title");
//获取驱动
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("update jw05_news set news_title=?,news_topic=?,news_author=?,news_publisher=?, news_content=? where news_id=?");
//占位符的设置
ps.setString(1,title);
ps.setInt(2,Integer.parseInt(topic));
ps.setString(3,author);
ps.setString(4,new Date()+"");
ps.setString(5,content);
ps.setInt(6,Integer.parseInt(newId));
//执行并获得结果 【收到影响的行数】
int i = ps.executeUpdate();
if(i>0){ //修改成功
out.print("<script>alert('修改成功');location.href='index.jsp'</script>");
}else{ //修改失败
out.print("<script>alert('修改失败');history.go(-1)</script>");
}
%>
五.数据库关系图
涉及到的一些表格,大家跟着该图片创建表。
今天的学习就到这里啦,大家可以把代码复制过去看看,只要有html基础会数据库和Java的人来说,这个只不过是结合了这三种而已,也是非常简单的,大家有什么 不懂的在下方评论。
更多推荐
所有评论(0)